[GA4] Set up Google Analytics session unification for AMP

In this article:

AMP pages are served from an AMP cache domain which is different from your site domain. To track a user journey across an AMP cache and your site pages, you can use an AMP Linker; an amp-analytics feature that joins user sessions using AMP Client ID as a user identifier.

AMP Linkers decorate outbound links from AMP cache pages with an AMP Client ID. Pages tagged with Google Analytics then retrieve the AMP Client ID from the URL query parameter and join the user session using the AMP Client ID as user identifier.

This article describes the steps needed to set up an AMP Linker with Google Analytics.

How the AMP Client ID affects your data

Synchronization of AMP Client IDs between an AMP Cache and your original site domain results in more accurate user metrics, e.g. Session Duration, Bounce Rate, and Pages per Session. Note that for the AMP Client ID to be useful, data should be collected within the same Google Analytics property.

Note: Synchronization of AMP Client IDs between AMP Cache and your origin will result in a one time reset of existing GA User Identifiers that are used for features such as Cohort Analysis, Lifetime Value and Audience Targeting. This may result in a noticeable temporary fluctuation in New User metrics and related reports.

Set up the AMP Linker

You will set up the AMP Linker with Google Analytics in two steps:

  1. Tag your AMP pages
  2. Tag your non-AMP pages

Step 1. Tag your AMP pages to pass AMP Linker parameters

Add the default Google tag for AMP on your AMP landing pages. See example snippet below and make the following changes:

Replace MEASUREMENT_ID with the tag ID of the Google Analytics property to which you want to send data

Optionally, if your AMP pages are hosted on a different top-level domain from outgoing links to your site or if AMP pages are not on a well-known subdomain (ie. not on www., amp., m.), include a linker configuration as shown below. Add in the domain that the AMP page is hosted on as well as domains for any outgoing links.

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars": {
    "gtag_id": "MEASUREMENT_ID",
    "linker": {
      "domains": ["example.com", "example2.com"]
    }
    ,
    "config": {
      "MEASUREMENT_ID": {
        "groups": "default"
      }
    }
  }
}
</script>
</amp-analytics>
Note: For gtag_id, you can specify the ID from any Google product (e.g. Google Ads or Search Ads 360) that you're using on your AMP,  but only use an ID from one Google product in the gtag_id field.
Important: Google Tag Manager for AMP doesn't currently support Google Analytics 4. If you wish to use AMP with Google Analytics 4 you must manually add the AMP snippet. Learn more.

Step 2. Tag your non-AMP pages to read AMP Linker parameters

Include the gtag.js snippet on your non-AMP pages. See example snippet below:

  • Replace MEASUREMENT_ID with the tag ID of the Google Analytics property to which you want to send data
  • Optionally, if your AMP pages are hosted on different top-level domain from outgoing links to your site or if AMP pages are not on a well-known subdomain (ie. not on www., amp., m.,) include a linker config as shown below. Add in the domain that your AMP page is hosted on, as well as domains for any outgoing links.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('set', 'linker', {
    'domains': ['example.com', 'example2.com']
  });
  gtag('js', new Date());
  gtag('config', 'MEASUREMENT_ID');
</script>

Setup verification

Verify that the Google AMP Client ID matches between AMP and non-AMP pages.

Option 1: Verify via the AMP Tag Test tool

Use the AMP Tag Test tool and enter AMP URL to test.

Note: The AMP Tag test tool is an automated testing tool that simulates a flow from AMP page served via viewer to a link to the next page. This tool should be used as a guideline and may not capture all test scenarios (e.g if the AMP page has links to different domains or subdomains). Please use the option below for manual testing. 

Option 2: Verify via Chrome Developer Tools

  1. Open the Google Chrome browser in incognito mode. Enable the mobile emulator in Chrome Developer Tools.
  2. Enter a search query on google.com that will return an AMP page for your site and click a search result for your AMP page, which should be served via the Google AMP Cache and displayed within the Google Search AMP viewer.
    • Note: If your page is not yet available via Google Search results, you can still test to see if session unification is working by going to the AMP Test tool to preview Search Results and following the steps mentioned below as long as the page can be indexed by Google.
  3. Find the Google Analytics network request for the AMP pageview: Go to the Network tab in Chrome Developer Tools and enter "collect" in the Filter field.
  4. Locate and select the network request that goes to www.google-analytics.com. On the Headers tab for the request, find the client ID by scrolling to Query String Parameters. Note the cid parameter.
  5. When AMP page is served by AMP viewer, the cid format should be a 64 characters long base64 string:
  6. Click Clear to clear the network requests.
  7. To verify that you have opted-in your non-AMP pages, confirm that the same cid parameter persists when navigating to a non-AMP page. To do this, click any link on your AMP page that goes to a non-AMP page served from your domain. To find the client ID, again filter for the string "collect". Select any network request that goes to www.google-analytics.com. Check that the cid query-parameter value matches the value you noted from Step 5.

Limitations

  • Session unification via AMP linker will not happen if a first-time user closes out the AMP page without navigating to next page since the session unification works using link decoration.

Was this helpful?

How can we improve it?
Search
Clear search
Close search
Main menu
12721754086389559314
true
Search Help Center
true
true
true
true
true
69256
false
false