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

When you implement the steps described in this article,, you agree that you have read and acknowledged the AMP Client ID and Google AMP Client ID API Policy, and have implemented any requirements therein of this service in conjunction with any Google Analytics property with which it is used.

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

  1. Tag your AMP pages
  2. Tag your non-AMP pages
  3. Configure referral exclusions

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

Install a Google Analytics tag, either via a global site tag or via Google Tag Manager. Follow instructions below based on your implementation:

Option 1: Global site tag

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

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

Optionally, if your AMP pages are hosted on different domain/subdomain from outgoing links to your site, include a linker config as shown in italics below. Add in the domain your AMP page is hosted on as well as domains for any outgoing links. Make sure to include the exact domain including ‘www.’ if applicable in the config as shown below.

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars": {
    "gtag_id": "MEASUREMENT_ID",
    "linker": {
      "domains": ["amp.example.com", "www.example.com", "www.example-b.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.

Option 2: Google Tag Manager

Create an AMP container in Google Tag Manager.

Add a Google Analytics tag to your AMP Container.

Add a Conversion Linker tag to your AMP Container and select ‘Enable Linking across domains’ and ‘Enable linking to Google Analytics Client ID’. If your AMP landing pages are hosted on the same domain/subdomain as outgoing links to your site, you can leave the domains field empty.

Optionally, if your AMP pages are hosted on different domain/subdomain from outgoing links to your site, add the domain your AMP page is hosted on as well as domains for any outgoing links as a comma delimited list. Make sure to include the exact domain including ‘www.’ if applicable in the config as shown below.

 

Use the default Tag Manager code snippet on your AMP page - See the example below and replace GTM_CONTAINER_ID with your Tag Manager container id.

<!-- Google Tag Manager →
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=<   GTM_CONTAINER_ID>&gtm.url=SOURCE_URL" data-credentials="include">
</amp-analytics>

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

There are several ways to implement tag your non-AMP pages to read AMP Linker parameters, so use the instructions that are most appropriate for your configuration.

Option 1: Global site tag

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

  • Replace MEASUREMENT_ID with the tracking ID of the Google Analytics property to which you want to send data
  • Optionally, if your AMP pages are hosted on different domain/subdomain from outgoing links to your site, include a linker config as shown below. Add in the domain your AMP page is hosted on as well as domains for any outgoing links.
<!-- Global site tag (gtag.js) - Google Analytics -->
<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': ['amp.example.com', 'www.example.com, www.example-b.com']
  });
&nbsp; gtag('js', new Date());
  gtag('config', 'MEASUREMENT_ID');
</script>

Option 2: Google Tag Manager

For the GTM Web Container, make sure the Google Analytics has been added.

If your AMP pages are hosted on different domain/subdomain from outgoing links to your site, add in the following configuration: In the Google Analytics tag and add a field ‘allowLinker’ with value true using steps below:

  1. Open the Google Analytics Settings variable you want to edit, then click the Variable Configuration card.
  2. Navigate to More Settings > Fields to Set.
  3. Click + ADD FIELD.
  4. Set the Field Name to allowLinker, and set Value to true.
  5. Save the new variable configuration.
  6. Repeat the previous steps for all relevant Google Analytics Settings variables (if you are using more than one variable in your container).
  7. Publish the container.

Use the default Tag Manager code on your non-AMP pages

<!-- Google Tag Manager -->
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=<GTM_CONTAINER_ID>&gtm.url=SOURCE_URL" data-credentials="include">
</amp-analytics>

Option 3: analytics.js

For your non-AMP pages, you can use the default Analytics js code snippet to read AMP linker parameters.

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'GA_TRACKING_ID', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

Optionally, if your AMP pages are hosted on different domain/subdomain from outgoing links to your site, add in the configuration highlighted below, as described in cross-domain configuration for analytics.js:

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'GA_TRACKING_ID', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['amp.example.com', 'www.example.com', 'www.example-b.com'] );

ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

Step 3. Add a referral exclusion to the Google Analytics admin console

When Google serves AMP content to users, it uses the Google AMP Cache. To prevent cached AMP subdomains from breaking sessions, you must add a referral exclusion for the domain cdn.ampproject.org.

If you serve AMP pages from multiple subdomains, then you may want to treat one AMP subdomain differently than another. In this case, enter the cached versions of any existing subdomains used in referral exclusion for your website so you can maintain specific referral exclusions across AMP and non-AMP sites. For example, if you have an existing referral exclusion for your subdomain subdomain.example.com, then add a referral exclusion for subdomain-example-com.cdn.ampproject.org. Learn more about AMP Cache URL format.

Setup verification

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

Option 1: Verify via the Google Tag Assistant Chrome Extension

  1. Download and install the Google Tag Assistant Chrome extension
  2. Enter a search query on google.com that will return an AMP page for your site.
  3. Start Tag Assistant Recording.
  4. Refresh the search results in browser after recording has started.
  5. Click the search result that leads to your AMP page.
  6. Click a link that leads from the AMP page to a non-AMP page.
  7. Stop the Tag Assistant recording.
  8. Make sure there is only one session with consistent IDs as seen below (i.e Page Load 2 does not show Session Start.)

 

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 session unification is working by going to AMP Test tool to preview Search Result 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.
  • Session unification is currently not supported if the AMP cache page links to another AMP page hosted on your domain
Was this helpful?
How can we improve it?