GTM and AMP in WordPress

Editor’s note

The purpose of the gist in this and other posts is to show examples of how things would work if you would like to use a given functionality in an AMP-compatible way, but don’t have a plugin providing that functionality in AMP yet. Make sure to check if there is a plugin that supports AMP for your functional or visual needs. Note also that these gists are not supported plugins, and that you should take advantage of them in the same way that you use code as you learn new technologies.

This post captures a knowledge aspect gathered from the Official AMP plugin Support forum. The community asks interesting and important questions in the forum, and the engineering team driving the development of the plugin provides 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

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. One example is Google Site Kit, which will enables an 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.

See the installation instructions at the end of the gist for guidance. Thank you Weston Ruter!

Leave a Reply

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