Tracking traffic from AI-generated overviews is essential for understanding how users engage with your website. When visitors click on your site from these AI summaries, the URL contains a specific pattern that can be identified and tracked in Google Analytics 4 (GA4). This guide walks you through setting up tracking to capture this data effectively.
Understanding the URL Structure
When someone clicks from an AI overview to your site, the URL contains a distinctive pattern:
- It includes
#:~:text=
followed by the text being highlighted. - Example:
yourwebsite.com/article#:~:text=beginning_of_highlight...ending_of_highlight
- This pattern applies to AI overviews, enhanced snippets, and question boxes.
Implementation process
Step 1: Set Up Custom JavaScript Variables in Google Tag Manager (GTM)
- Launch Google Tag Manager
- Navigate to Variables → New
- Select “Custom JavaScript” as the variable type
- Create two JavaScript variables:
First Variable (Capturing Highlight Beginning):
- Name:
js_ai_highlight_begin
function() { var pageUrl = document.URL; if (pageUrl.includes('#:~:text=')) { var highlightText = decodeURIComponent(pageUrl.split('#:~:text=')[1]); var beginText = highlightText.split('...')[0]; return beginText; } return undefined; }
Second Variable (Capturing Highlight Ending):
- Name:
js_ai_highlight_end
function() { var pageUrl = document.URL; if (pageUrl.includes('#:~:text=')) { var highlightText = decodeURIComponent(pageUrl.split('#:~:text=')[1]); var segments = highlightText.split('...'); return segments.length > 1 ? segments[1] : ""; } return undefined; }
Step 2: Create a Trigger for Highlight Detection
- Go to GTM → Triggers → New
- Choose “Page View” as the trigger type
- Select “Some Page Views”
- Set the condition:
js_ai_highlight_begin is not null
- Name the trigger:
pageview_ai_highlight_detected
- Save the trigger
Step 3: Build a GA4 Event Tag
- Go to GTM → Tags → New
- Choose “Google Analytics: GA4 Event”
- Enter your GA4 Measurement ID
- Set the event name:
ai_highlight_click
- Add event parameters:
- Parameter 1:
highlight_begin
→ Selectjs_ai_highlight_begin
variable - Parameter 2:
highlight_end
→ Selectjs_ai_highlight_end
variable
- Parameter 1:
- Assign the trigger created in Step 2
- Name the tag:
GA4 - event - ai_highlight_click
- Save the tag
Step 4: Configure Custom Dimensions in GA4
- Go to GA4 → Admin → Custom Definitions
- Select “Create Custom Dimension”
- For the first dimension:
- Event parameter:
highlight_begin
- Dimension name:
AI Highlight Beginning
- Scope: Event
- Save
- Event parameter:
- Repeat for the second dimension:
- Event parameter:
highlight_end
- Dimension name:
AI Highlight Ending
- Scope: Event
- Save
- Event parameter:
Step 5: Validate and Deploy
- Preview your GTM container
- Test by visiting a URL containing the
#:~:text=
parameter - Check if the event triggers properly in GTM’s Debug Mode
- Submit and publish your changes
Conclusion
This tracking method helps you understand not only which pages appear in AI-generated overviews but also how visitors interact with your site after clicking through. With this data, you can optimise your content strategy accordingly.