Views: 217
Our ability to track user interactions has become increasingly obfuscated and complex. Obviously SEO’s used to use referral data as a way to provide insights into how users arrived at our websites. Several developments over the last 10 years have crushed our ability to track them:
- HTTPS Adoption: the shift to HTTPS resulted in the loss of referral data when users navigate from secure to secure sites.
- Referrer Policies: The implementation of
rel="noreferrer"
attributes in links prevents the transmission of referral info.. - Browser and Privacy Changes: increasingly restrict the sharing of referral data to protect user privacy, as well as via ad and cookie blockers.
With these challenges, Google’s introduction of text fragments in Chrome with URLs containing #:~:text=
– offers a fresh way to track interactions. These fragments allow direct linking to specific text within a page, highlighting the exact content relevant to a user’s search query. By tracking these text fragments, we can gain valuable info about which parts of their content are resonating with users.
Lets look at 3 easy ways to track fragments.
As we continue this unwelcome path from Querying based search engines to Prompting based search engine, we seem to be loosing more and more SEO data. We have to find new ways to track visitors. Googles adoption of text fragments gives us an opportunity to do that. Especially in the ‘snippet’ AIO era where Google is linking to #textfragments more than they every have.
Often, you can get #text fragment counter hits, and never even see a server level referral passed from Google or see a keyword in GA4 (Raise eyebrows – yes I know – this is what we do – by Seo’s for Seo’s.) So this can be your gateway back into getting more Google referrals logged.
<aside>I created highlight kw fragments (that still work) on WebmasterWorld in 2003. Matt Cutts (GoogleGuy) thought it cool.</aside>
Unfortunately, text fragments are 100% a browser side construct. They are not passed directly to the server. So a simple .htaccess hack isn’t going to work. We need to come at this from javascript.
Custom Tracking
JS to grab a text fragment – add to your header:
<script>
(function() {
var fragment = window.location.hash;
if (fragment) {
var img = new Image();
img.src = '/YourTracker.cgi?fragment=' + encodeURIComponent(fragment);
}
})();
</script>
From there, you need to link to that “YourTracker.cgi” script. That can be any counter script you have to print an image to the screen. Here is one in Perl, PHP, and one in Python:
Viewing Logged Text Fragments
- Access the Log File: Navigate to the log file specified in your script (e.g.,
/path/to/your/logfile.log
). - Review Entries: Each entry typically includes a timestamp and the captured fragment. For example:
Wed Jun 05 10:15:30 2025 - Fragment: #:~:text=benefits%20of%20regular%20exercise
- Analyze Patterns: Use text editors or command-line tools to search for recurring fragments or patterns that indicate popular content sections.
Utilizing Fragment Data Opportunities
- Content Optimization: Identify which text snippets are frequently highlighted or linked to, suggesting high interest. Consider refactoring, updating or expanding these sections to enhance user engagement.
- SEO Insights: Fragments can reveal kws or phrases that users are using to find you. Reinforcing that content with links can go even further to making your content show up on Google.
- User Behavior Analysis: Understanding which parts of your content are directly accessed via fragments can help you make informed decisions about content layout, internal linking, and navigation improvements.
GA4 : Google Analytics
Google stopped showing any data in GA about fragments in May of 21. However, we can track some of them with a custom implementation. The short comings of GA are highly documented through the web and in presentations at SEO conferences. If you don’t have a secondary tracker on your website – you should.
Finding new ways of getting anything usable out of GA4 is a bit of a challenge – that we are up for:
Step 1: Create a Custom JavaScript Variable in GTM
This variable will extract the text fragment from the URL:
- In GTM, navigate to Variables > New.
- Name the variable (e.g.,
Text Fragment
). - Choose Variable Type: Custom JavaScript.
- Enter the following code:
function() { var url = performance.getEntriesByType("navigation")[0]?.name || document.location.href; var match = url.match(/#:~:text=([^&]*)/); return match ? decodeURIComponent(match[1]) : ""; }
- Click Save.
This script attempts to extract the text fragment from the URL. Note that browser behavior will vary, and some browsers might not expose the full fragment to JavaScript manipulation due to privacy considerations. We also don’t know if #text fragments are 100% secure.
Step 2: Add the Variable to Your GA4 Configuration Tag
To send the captured fragment to GA4:
- In GTM, go to Tags and select your GA4 Configuration tag.
- Under Fields to Set, click Add Row.
- Set Field Name to
page_location
. - Set Value to
{{Page URL}}#{{Text Fragment}}
. - Click Save.
This configuration appends the text fragment to the page URL, allowing GA4 to record the full URL including the fragment.
Step 3: Create a Custom Dimension in GA4
To analyze the text fragments in GA4 reports:
- In GA4, navigate to Admin > Custom Definitions > Create Custom Dimension.
- Set Dimension Name to
Text Fragment
. - Set Scope to Event.
- Set Event Parameter to
page_location
. - Click Save.
This custom dimension will capture the full page URL, including any text fragments, for each event.
Step 4: Test and Publish
- In GTM, click Preview to enter debug mode.
- Navigate to a page on your site with a text fragment in the URL (e.g.,
https://example.com/page#:~:text=highlighted%20text
). - Ensure that the GA4 tag fires and that the
page_location
includes the text fragment. - In GA4, use DebugView to verify that events are being recorded with the correct
page_location
. - Once confirmed, return to GTM and click Submit to publish your changes.
Considerations
- Browser Support: Not all browsers support text fragments. Chrome and Edge have implemented this feature, but others may not.
- Privacy: Be extra cautious when tracking these text fragments, as they could possibly have embedded sensitive information.
- Data Volume: If your site receives a high volume of traffic with diverse text fragments, consider implementing filters or limits to manage the data effectively.
Worth a Read for More:
- How to track Scroll-to-text fragments in Google Analytics 4 with GTM
- What exactly is the `#:~:text=` location hash in an URL? (StackOverflow)
- Boldly link where no one has linked before: Text Fragments (web.dev)
- How to Use Text Fragments to Boost Your Website’s SEO Rankings
- How to detect text fragments `#:~:text=` with JS?
- Web’s Most Underutilized API: Text Fragments

As the CEO and founder of Pubcon Inc., Brett Tabke has been instrumental in shaping the landscape of online marketing and search engine optimization. His journey in the computer industry has spanned over three decades and has made him a pioneering force behind digital evolution. Full Bio
Visit Pubcon.com