Your Guide to Adding Google Tag Manager to WordPress

Adding new things to your website can feel like a big deal. But what if I told you that adding Google Tag Manager to your WordPress site is super easy? With a few simple steps, you can get it done. Let’s walk through it together.

Finding Your GTM Code

First, you need to grab your Google Tag Manager (GTM) code. You can find this in your GTM account. It comes in two parts: one for the head and one for the body of your site. It’s like a secret handshake between your website and Google.

Now, let’s put that code where it belongs. The best way to do this is by adding a little snippet to your WordPress theme’s functions.php file. This might sound scary, but it’s just a copy-and-paste job.

Here’s the code you’ll need. Just drop this at the end of your functions.php file:

<?php

// Add the Google Tag Manager script to the <head>
add_action('wp_head', 'add_gtm_head_script');
function add_gtm_head_script() {
    ?>
    <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-YOURCODE');</script>
    <?php
}

// Add the Google Tag Manager noscript to the <body>
add_action('wp_body_open', 'add_gtm_body_noscript');
function add_gtm_body_noscript() {
    ?>
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-YOURCODE"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <?php
}

?>

Don’t forget to swap out GTM-YOURCODE with your own GTM ID. That’s it!

That’s it

See? That wasn’t so bad. You’ve just unlocked the power of Google Tag Manager on your site. Now you can add all sorts of tracking tags without ever touching your code again.

Happy tagging!

Scroll to Top