How to Implement Google Tag Manager With PHP

Check out the two ways to implement a Google Tag Manager container in a PHP website or web application.

Published Categorized as Analytics

You’re here because you’re a PHP developer, and you’re working on a user story for implementing a Google Tag Manager container on the website or web application that you and your team are building.

I’m sure you’re also wondering how exactly you can do this. So let’s not waste any more time with introductions and help you get to the bottom of it.

This guide will show you two ways to implement a GTM container with PHP. One is a static, hardcoded way that works well for individual sites or apps. The other is a dynamic way suited for platforms and Content Management Systems (CMS).

The Static Way

The simplest way to implement Google Tag Manager with PHP is to echo or print the <script> and <noscript> tags in the source code of your website or web application.

The <script> tag should be implemented as early as possible in the <head> section of the HTML markup, and the <noscript> tag should go immediately after the opening of the <body> section. To learn why, head on over to Google Tag Manager: In the Header or Footer?

(As a side note, echo and print are PHP language constructs that do pretty much the same thing—they output expressions. The key difference between them is that echo accepts multiple arguments and has no return value.)

Here’s how to output the <script> tag for a GTM container:

<?php echo "<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->" ?>

And here’s how to output a GTM container’s <noscript> tag:

<?php
echo "<!-- Google Tag Manager (noscript) -->
<noscript><iframe src='https://www.googletagmanager.com/ns.html?id='GTM-XXXXXXX'
height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->"
?>

Note that GTM-XXXXXXX in the two code snippets above needs to be replaced with your GTM container’s ID.

Also, pay attention to the type of quotes used in your code (single quotes vs. double quotes). The quotes in the <script> and <noscript> tag should be different from those in your PHP code, or your logic will break because the interpreter will end the echo or print output prematurely.

The Dynamic Way

If you’re the developer of a Content Management System (CMS) rather than an individual website or web application, then chances are your users will want to implement Google Tag Manager containers with different IDs.

In such a case, you will need a more dynamic implementation of Google Tag Manager. The simplest way to approach this is to store the container ID in a variable, then add the value of that variable to your echo or print statement.

So your source code for the <script> tag will look like this:

<?php
// Store container ID in variable
$container_id = "GTM-XXXXXXX";
// Output the GTM <script> tag to the front end
echo "<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','" . $container_id . "');</script>
<!-- End Google Tag Manager -->"
?>

And your source code for the <noscript> tag will look like this:

<?php
// Store container ID in variable
$container_id = "GTM-XXXXXXX";
// Output the GTM <noscript> tag to the front end
echo "<!-- Google Tag Manager (noscript) -->
<noscript><iframe src='https://www.googletagmanager.com/ns.html?id='" . $container_id . "'
height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->"
?>

Note that the container ID is one the same, so, in the source code for your real-world website or web application, you will probably want to store its value in the $container_id variable once, then reuse it for the outputs of both the <script> and the <noscript> tags.

The Bottom Line

Thanks for reading this far, and I hope this guide helped you figure out a good way forward.

Of course, these are simple examples that you’ll probably need to adapt and expand until they meet your needs. But that’s what they’re meant for—as inspiration to get you unstuck so you can start coding and iterating.

By Dim Nikov

Content creator with marketing and web development skills to pay the bills.

Leave a comment

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