Categories
Web analytics

GA4 Guide – Measuring citations from Google Ai overviews and AI mode

Is Google bringing keywords back to GA4 thanks to AI?

Almost yes—only now they’re full sentences. You might think the era of “not provided” in analytics is finally behind us. Maybe not.
But then Google AI Overviews shows up and starts adding something like #:~:text=some%20text… to URLs, and suddenly there’s hope: what if AI is finally showing us exactly which part of our text it considers the answer?

So let’s break down what’s actually happening, why you should care, and how you can measure it in GA4.


What exactly are these Fragment Text Directives?

Simply put: it’s a way to link directly to a specific portion of text within a URL.
If you add something like this to a link:

https://mareklecian.cz/ga4-mereni-google-ai-overviews-a-ai-mode/#:~:text=prohl%C3%AD%C5%BEe%C4%8D%20zascrolluje%20na%20tuhle%20v%C4%9Btu%20a%20zv%C3%BDrazn%C3%AD%20ji

the browser will scroll to that sentence and highlight it.
Magic? Nope. Just good old HTML5 with a dash of user-friendly sparkle.

Google has been using this trick since 2020, but it only started to get interesting with the arrival of AI Overviews (GAIO) and AI Mode. When AI cites websites, it uses this mechanism to point users precisely to the passage it deems relevant.

Why does it matter?

Because this is something like the return of “keywords” to GA4.
Instead of a search query, we get the exact snippet of text that triggered the click.
For an analyst, that’s a dream—you learn exactly which part of the text brought the visit.

We used to have “keyword/phrase” → today we can have #:~:text=.
That gives you an answer to the question:

“Which sentence from my site/content did AI respond to in the search results?”

And that’s no small thing. It’s a new analytical signal that can be very helpful for SEO.

Measuring AI citations = a new SEO insight

From my tests (and a few experiments with clients where we’ve been closely monitoring AI traffic for quite some time), it appears that GAIO uses fragments sparingly, while AI Mode uses them much more.
It’s likely trying to increase precision when sending a user from an AI answer to a website.

In practice, this means that if a user clicks from an AI result, you can capture which snippet they landed on—i.e., what Google AI considers the “best answer.”

What to expect in the numbers?

From my tests across several projects, ~1% of organic Google traffic had a detectable :~:text= at the time of testing, with the trend generally upward. The AI Mode rollout in the Czech Republic may accelerate this curve.

Watch out—these are the most common pitfalls…

  • Attributing queries: a fragment ≠ the user’s query. It’s an answer snippet selected by AI.
  • Anecdotally, this fragment seems to be used for about 50% of clicks; I have no way to measure that precisely. I’ll leave that to others.
  • Google used fragment text directives before AI Overviews and AI Mode, so it’s not guaranteed—just a higher probability.

How does it work technically?

Fragment Text Directives (FTD) are added to the URL using #:~:text=text.
The catch is that this fragment does not appear in document.location or window.location.
So you won’t see it in a GTM variable.

Simple? Not at all.
But there’s a hack.

Use the Performance API—specifically performance.getEntriesByType('navigation')[0]["name"]
This trick returns the full URL, including the fragment that you normally won’t see in GTM even if you look at the URL hash.

Simple version of measuring the AI fragment #:~:text

A simple version implemented via a GTM tag that only captures the :~:text= fragment; it has no fallbacks and the data isn’t as detailed.

When would I use this solution?

You have a normal website without super-complex analytics. Suitable in 98% of cases.

Code

How-to for the simple version

  1. Create an HTML tag with the script
    1. GTM → Tags → New → Custom HTML
    2. Name it something like “HTML url_fragment_text”
    3. Paste the code above. * (between <script></script>)
    4. Set Additional consent checks → “No additional consent required”
    5. Set Trigger: All Pages (Page View).
    6. Save
      url fragment text lite. jpg
  2. Create the GTM variables you want to use.
    1. GTM → Variables → New → Data Layer Variable
    2. Name dl.url_fragment_text
    3. Data Layer Variable Name url_fragment_text
    4. Save
      gtm variable
  3. Send the data you want to GA4 via an Event.
    1. Create GTM → Tags → New → GA4 event
    2. Set your Measurement ID
    3. Set the event name “url_fragment_text”
    4. Add parameter → url_fragment_text = {{dl.url_fragment_text}}
    5. Consent settings → no additional consent required.
    6. Create the trigger
      1. Select Custom Event
      2. Event Name: url_fragment_text
      3. Event name: url_fragment_text
      4. Save
    7. Save
      g4 tag scaled
  4. Create a custom dimension
    1. GA4 Admin → Custom definitions → Create custom dimension
    2. Dimension name → url_fragment_text
    3. Event parameter → url_fragment_text
    4. Save dimenze scaled
  5. Test
    1. When debugging in GTM, add #:~:text=test to the URL
    2. Reload the page → This will send the GA4 event
    3. Verify everything works
      1. Via a browser extension such as Analytics Debugger
      2. GA4 → Reports → Realtime overview
  6. Publish the Google Tag Manager container version.
  7. Monitor and report

Comprehensive version of measuring the AI fragment #:~:text

I use this template for generic GA4 events to send data. It handles everything directly from the script, so with the template set up properly you don’t need extra configuration in GTM—i.e., fewer custom settings in GTM. Suitable for large projects where you dig into data in detail.

This version supports more fragment types and has fallbacks for older browsers, etc.

Minified comprehensive version

Guide for the comprehensive version

A brief guide for advanced users 🙂

  1. GTM → Tags → New → Custom HTML and paste the code above
  2. * (between <script></script>)
  3. Set Trigger: All Pages (Page View).
  4. Create the GTM variables you want to use.
  5. Send the data you want to GA4 via an Event.
  6. GA4 Admin → Custom definitions → Create everything needed
  7. Test, then publish GTM.
  8. Monitor and report
  9. Build a report from GA4 data in Google BigQuery.

What will the output look like in the dataLayer?
Input:
#:~:prefix=Podle%20vědců%20%2C&textStart=černé%20díry&textEnd=jsou%20stále%20záhadou&suffix=%2C%20říká%20profesor
Output:

Implementation nuances

  • Coverage of supported browsers for Fragment Text Directives is about 93.2% of users, but reading this value can be problematic because some browsers restrict it. Chromium-based browsers are fine.
  • Firefox supports the feature but blocks reading this value from the Performance API. (Tested)
  • Safari behaves similarly to Firefox; it worked in older versions, but newer versions of Safari block it. (Tested)
  • Some JavaScript-heavy sites remove #:~:text= from the URL, but not quickly enough—so you’ll usually still measure it.
  • Will this work forever? No. It’s quite possible that, for privacy reasons, other browsers will also block the ability to read this data.
  • This value is not present in headers or server requests, so ServerGTM and the web server won’t have access to it.

GA4 limits

Consent & PII

  • The fragment may contain text you don’t want to send to GA (a username in the UI, a comment).
  • Consider a deny-list of words, max length, or hashing/local-only analysis for sensitive sections—or in those sections, don’t send the text at all, only that it happened.

What still needs solving?

  • Adoption rate of AI fragments by country and vertical. Practically unknown. It depends a lot on Google.
  • Correlation of fragments with conversions vs. traditional SERP. That will be interesting to evaluate.
  • Stability of citations over time (drift in AI answers).

Summary

  • Text Fragments from AI results in URLs are a new analytical signal for evaluating SEO.
  • With the Performance API you can extract them into a visible URL and into GA4.
  • Don’t treat it like a “keyword”—it’s an AI-selected snippet.
  • Use it to prioritize content, make UX tweaks, and report “what AI considers the answer.”

What’s your take? Which sentence from your site should AI cite first?

Last code update: 20 Oct 2025 11:23:36 — I increased compatibility with older browsers.

.