Monday, March 3, 2025
HomeAnalyticsAccelerated Mobile Pages Via Google Tag Manager

Accelerated Mobile Pages Via Google Tag Manager


Google Tag Manager recently published support for Accelerated Mobile Pages (AMP). This support comes in the form of a new Container type in Google Tag Manager.

When you create an AMP container in GTM, you are actually setting up an external configuration for AMP, which leverages AMP’s own analytics module. As befits Google Tag Manager, creating the configuration is done in the familiar Google Tag Manager user interface, and you have (almost) all the tools of regular Google Tag Manager at your disposal.

In this article, I want to go over how AMP and GTM mesh together to provide your mobile pages with improved tracking capabilities.


X


The Simmer Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava into your email inbox!

1. AMP Overview

Accelerated Mobile Pages is an open-source project, and you can read all about it at https://www.ampproject.org/. In a nutshell, it’s a set of structural instructions for building web pages, where focus is on speed and performance, without sacrificing too much UI/UX along the way.

AMP is based on a number of design principles that might ring true to you if you’ve ever pondered about website performance. Things like asynchronous resource loading, inline CSS styling, web font optimization and optimized pre-rendering of pages are some of the features that AMP relies on to provide users with content super fast.

Creating an AMP site

Creating an AMP version of your site isn’t just a plug-and-play affair. You need to rewrite the HTML, JavaScript, and CSS styles to match the AMP design principles. If you’re using a platform like WordPress, there are plugins available that do most of the configuration for you. And here’s a great tip for AMP development work: anytime you are browsing an AMP page, you can add the URL hash #development=1 to the URL to validate your AMP page, outputting the result of the validation into your browser’s JavaScript Console.

Finally, if you’ve setup your site with AMP pages, it’s a good idea to follow the Google Search Guidelines for informing the search engine about your site’s new mobile structure. Google will attempt to direct mobile searches of your site to the corresponding AMP pages, thus providing mobile visitors with fast, optimized access to your precious content.

If you want to see what AMP pages look like, you can visit any article on this site, and add /amp/ to the URL (e.g. https://www.simoahava.com/amp/analytics/accelerated-mobile-pages-via-google-tag-manager/.

2. AMP Analytics

The Google Tag Manager AMP container leverages the amp-analytics component. This component, developed within the AMP project, provides a light-weight framework for analytics requests sent either via a number of built-in vendor templates, or to a custom endpoint of your choosing.

The amp-analytics framework is managed by a JSON configuration object, where you specify details of the endpoint you want to send the data to, as well as variables and triggers (sound familiar?) that govern what analytics requests are sent and when.

Even though the amp-analytics documentation isn’t particularly long or complex, there’s still many things to consider when configuring a custom tracking scheme. That’s why it’s very useful to have pre-built templates for a number of analytics vendors (e.g. Google Analytics, Adobe Analytics, Snowplow Analytics).

Add amp-analytics to your website

To add support for the analytics component on your site, you need to add the following line of code into the of your document:

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js">script>

This loads the AMP analytics library, and instructs the page to look for an element named . This custom element is where you introduce the JSON configuration object that will eventually govern how the site is tracked.

Google Analytics has a great developer guide for setting up the JSON configuration object manually. For example, to send a Page View hit to Google Analytics when the page is loaded, you’d add the following element to the of the site:

<amp-analytics type="googleanalytics" id="analytics1">
<script type="application/json">
{
  "vars": {
    "account": "UA-XXXXX-Y"
  },
  "triggers": {
    "trackPageview": {
      "on": "visible",
      "request": "pageview"
    }
  }
}
script>
amp-analytics>

As you can see, you’re using the custom element that you specified when adding the initial script loader to the of your page. The type parameter of the element specifies that you are using the built-in googleanalytics vendor template.

Be sure to check out Bounteous’ excellent guide for Google Analytics and AMP integration!

Load the JSON configuration object as an external resource

You can also load the JSON configuration object from an external source (as long as the request adheres to AMP CORS security guidelines). For example, the Google Tag Manager “container” is actually a request to an external config file that you specify like this:

<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-XXXXXX&gtm.url=SOURCE_URL" data-credentials="include">
  <script type="application/json">
  {
    "vars": {
      "someCustomAmpVar": "someValue"
    }
  }
  script>
amp-analytics>

This downloads a file named amp.json, specified by using your container ID, and the data-credentials attribute allows the resource request to read and write cookies as needed. The

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments