Press "Enter" to skip to content

Tracking #text fragments from Google – This is SEO Gold

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:

Perl
#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

# Create a new CGI object
my $q = CGI->new;

# Retrieve the 'fragment' parameter
my $fragment = $q->param('fragment') || '';

# Log the fragment with a timestamp
my $logfile = '/path/to/your/logfile.log';
if (open my $log, '>>', $logfile) {
  print $log scalar(localtime) . " - Fragment: $fragment\n";
  close $log;
} else {
  warn "Could not open log file: $!";
}

# Output a 1x1 transparent GIF
print $q->header(-type => 'image/gif');
binmode STDOUT;
print pack('C*',
  71,73,70,56,57,97,1,0,1,0,128,0,0,255,255,255,0,0,0,
  33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59
);
Python
#!/usr/bin/env python3
import cgi
import sys

print("Content-Type: image/gif\r\n\r\n", end='')

# Output a 1x1 transparent GIF
transparent_gif = (
  b'GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00'
  b'\xFF\xFF\xFF!\xF9\x04\x01\x00\x00\x00\x00,\x00'
  b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01'
  b'\x00;'
)
sys.stdout.buffer.write(transparent_gif)

# Log the fragment
form = cgi.FieldStorage()
fragment = form.getvalue('fragment', '')
with open('/path/to/your/logfile.log', 'a') as log_file:
  log_file.write(f"Fragment: {fragment}\n")
PHP
<?php
// Set the Content-Type header to indicate that the response is a GIF image
header('Content-Type: image/gif');

// Retrieve the 'fragment' parameter from the URL query string
$fragment = isset($_GET['fragment']) ? $_GET['fragment'] : '';

// Define the path to your log file
$logfile = '/path/to/your/logfile.log';

// Log the fragment with a timestamp
$timestamp = date('r'); // RFC 2822 formatted date
$log_entry = "$timestamp - Fragment: $fragment\n";

// Attempt to open the log file and write the log entry
if ($fp = fopen($logfile, 'a')) {
fwrite($fp, $log_entry);
fclose($fp);
} else {
// If the log file cannot be opened, log the error to the PHP error log
error_log("Could not open log file: $logfile");
}

// Output a 1x1 transparent GIF
// This is the binary representation of a minimal transparent GIF
echo base64_decode('R0lGODlhAQABAPAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==');
?>

Viewing Logged Text Fragments

  1. Access the Log File: Navigate to the log file specified in your script (e.g., /path/to/your/logfile.log).
  2. 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
  1. 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:

  1. In GTM, navigate to Variables > New.
  2. Name the variable (e.g., Text Fragment).
  3. Choose Variable Type: Custom JavaScript.
  4. 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]) : "";
    }

     

  5. 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:

  1. In GTM, go to Tags and select your GA4 Configuration tag.
  2. Under Fields to Set, click Add Row.
  3. Set Field Name to page_location.
  4. Set Value to {{Page URL}}#{{Text Fragment}}.
  5. 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:

  1. In GA4, navigate to Admin > Custom Definitions > Create Custom Dimension.
  2. Set Dimension Name to Text Fragment.
  3. Set Scope to Event.
  4. Set Event Parameter to page_location.
  5. Click Save.

This custom dimension will capture the full page URL, including any text fragments, for each event.

Step 4: Test and Publish

  1. In GTM, click Preview to enter debug mode.
  2. Navigate to a page on your site with a text fragment in the URL (e.g., https://example.com/page#:~:text=highlighted%20text).
  3. Ensure that the GA4 tag fires and that the page_location includes the text fragment.
  4. In GA4, use DebugView to verify that events are being recorded with the correct page_location.
  5. 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:

Copy and © Copyright 1997-2024 SearchEngineWorld . all rights reserved
All trademarks and copyrights held by respective owners.
WebmasterWorld and SearchEngineWorld are owned by Pubcon