Tracking AMP Sessions Using Google’s AMP Client ID API

Matt Mapplebeck
Dotdash Meredith Tech Blog
3 min readDec 14, 2017

--

Tracking sessions from AMP pages to non-AMP pages in Google Analytics can be difficult to set up properly. To ensure proper tracking, you’ll need to use Google’s AMP Client ID API but you’ll also need to configure it properly. I’ll show you step by step how to get this working so you end up with the most accurate results.

1. Check Your Privacy Policy

Realize that by completing the following steps, you’re opting into Google’s AMP Client ID Policy. Basically, make sure you provide clear notice to your users (via privacy policy or some other accepted means) that you collect third-party cookies on your site. If you already do this, then you can move on to the next step.

2. Opt in on AMP Pages

With the privacy policy out of the way, now add the following meta tag to the head of your AMP pages.

<meta name="amp-google-client-id-api" content="googleanalytics">

3. Opt in on non-AMP Pages

Next, complete the following steps in Google Tag Manager for your existing published tag container(s).

  1. Navigate to Tag Configuration > Fields to Set.
  2. Set useAmpClientId to true.
  3. Save the new tag configuration.
  4. Submit the tag.
  5. Publish the container.

If you use analytics.js instead of Google Tag Manager, include the following in your analytics tracking code.

ga('create', 'UA-XXXXX-Y', 'auto', {'useAmpClientId': true});

4. Update Referral Exclusions

This part is important. Without updating your referral exclusions, your tracking will be inaccurate, so pay attention!

When Google Analytics tracks a page view and the referral URL defers from the current domain, it will start a new session. So, since Google serves cached AMP pages from ampproject.org, when users visit a non-AMP page from a referring AMP page, Google Analytics will count two sessions instead of one. This will seriously skew your tracking metrics.

To fix this, you need to set a referral exclusion for ampproject.org in Google Analytics admin settings.

Once you add this referral exclusion, Google Analytics will no longer start a new session for users arriving from ampproject.org, which is exactly what you want.

5. Validate

We used a process in production similar to the following to validate that Google Analytics was tracking everything properly. Feel free to validate your implementation the same way.

  1. Create a very specific segment in Google Analytics to track yourself (e.g. your location, your test device, your test device version, etc.)
  2. On your test device, start by visiting a cached AMP page. Then click through to a non-AMP page on your site, then click through to a couple more non-AMP pages, keeping a list of the pages and the order of pages as you go.
  3. In Google Analytics, filter by your new segment and find your page view on the starting AMP page. Once you find your page view, Google Analytics should show you the next page path. If you can see all the non-AMP pages you viewed in the next page path, you will know that Google Analytics is correctly tracking users from AMP pages to non-AMP pages.

That should be everything you need to track AMP sessions properly! For more information, check out Google’s own AMP Client ID setup documentation.

--

--