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.
Set up the AMP Linker
You will set up the AMP Linker with Google Analytics in two steps:
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 , 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.
<script type="application/json">
{
"vars": {
"gtag_id": "MEASUREMENT_ID",
"linker": {
"domains": ["example.com", "example2.com"]
}
,
"config": {
"MEASUREMENT_ID": {
"groups": "default"
}
}
}
}
</script>
</amp-analytics>
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.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 , 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.
<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>
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
- Open the Google Chrome browser in incognito mode. Enable the mobile emulator in Chrome Developer Tools.
- 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.
- 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.
- 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. - When AMP page is served by AMP viewer, the
cid
format should be a 64 characters long base64 string: - Click Clear to clear the network requests.
- 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.