AMP in WordPress: Google Tag Manager

This post is the first in a series of posts capturing knowledge gems gathered from the Official AMP plugin Support forums. The community asks interesting and important questions in the forum, and the engineering team at Google driving the development of the plugin, led by Weston Ruter, has provided equally cool answers worth spreading.

Support Question

I’d like connect your plugin with Google Tag Manager.
How Can I do it? Do I need to setup the code into the <head> and <body>? I need to set it into functions.php? (link)

@mrjuri

WP.org AMP plugin support request

Background

Google Tag Manager is a tag management system (TMS) that allows you to quickly and easily update measurement codes and related code fragments collectively known as tags on your website or mobile app. Once the small segment of Tag Manager code has been added to your project, you can safely and easily deploy analytics and measurement tag configurations from a web-based user interface.

GTM can be used for retargeting, site analytics, and tracking conversions and many other aspects of your site.

The Right Way

The proper way to add GTM to your AMP WordPress site would be to use a plugin providing GTM injection capabilities in an AMP-compatible way. Currently there may not be a readily available plugin doing so, but surely there will be some possibilities available in the market soon. For example, Google is working on a new plugin called the Google Site Kit, which will provide AMP compatible GTM integration. If you are considering using a specific GTM plugin in your site but it does not offer AMP compatibility, we encourage you to request the plugin makers to support AMP, and to reach out to us for help if needed.

An Alternative

The good news is that despite of not having a plugin that does the injection of the GTM functionality via an easy UI, the AMP plugin for WordPress provides developers all the support needed to enable AMP-first content creation in WordPress. Thus, you can add the elements you need to your site and the AMP plugin will validate the code is AMP compatible, help you understand what is wrong if it is not, and get your pages ready to be served as AMP.

Specifically, the following plugin contains code to inject GTM to WordPress AMP pages:

You can use this gist code directly on your site, and you just have to replace the line defining the GTM_CONTAINER_ID with the data associated with your container.

Leave a Reply

Your email address will not be published. Required fields are marked *