[UA] Set up Google Analytics session unification for AMP [Legacy]

You are viewing a legacy article about Universal Analytics. Learn more about Google Analytics 4 replacing Universal Analytics.
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 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 the Google tag (gtag.js) or via Google Tag Manager. Follow instructions below based on your implementation:

Option 1: Google tag

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 tracking 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 , 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.

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 top-level domain as outgoing links to your site, you can leave the domains field empty.

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 , amp., m.), add the top-level domain your AMP page is hosted on as well as domains for any outgoing links as a comma delimited list.

 

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://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 tag your non-AMP pages to read AMP Linker parameters, so use the instructions that are most appropriate for your configuration.

Option 1: Google tag

Include the gtag.js 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 top-level domain from outgoing links to your site or if AMP pages are not on a well-known subdomain (ie. not on , 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://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>

Option 2: Google Tag Manager

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

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 , amp., m.), add in the following configuration: In the Google Analytics tag, 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.

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://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 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 , amp., m.), 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://google-analytics.com/analytics.js','ga');
ga('create', 'GA_TRACKING_ID', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['subdomain1.example.com', 'subdomain2.example.com','example2.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 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 options below for manual testing options.

Option 2: 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 Recordings.
  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 Tag Assistant Recordings.
  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 3: 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 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 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?
true
Choose your own learning path

Check out google.com/analytics/learn, a new resource to help you get the most out of Google Analytics 4. The new website includes videos, articles, and guided flows, and provides links to the Google Analytics Discord, Blog, YouTube channel, and GitHub repository.

Start learning today!

Search
Clear search
Close search
Google apps
Main menu
17616371459738573678
true
Search Help Center
true
true
true
true
true
69256
true
false