Adding 2 part Google Tag Manager snippet

Hi,

Is there a way to easily add both parts of the google tag manager script to all my pages - one close to the opening head tag and the other right below the opening body tag - using X’s page builder?

Also, what could be the correct way to add the same using X’s child theme and functions.php?

Thank you.

Hi Arif,

Thanks for reaching out.
To add the Google Tag Manager code you can use the plugin Insert Headers and Footers. Also, you can use the wp_head and wp_body_open hooks to inject the code through the functions.php of your child theme, the code will look like the following.

add_action('wp_head', 'google_tag_manager_head', 20);
function google_tag_manager_head() 
{ 
?>
    <!-- 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-XXXXXX');
    </script>
    <!-- End Google Tag Manager -->
<?php 
}

add_action('wp_body_open', 'google_tag_manager_body', 100);
function google_tag_manager_body() 
{ 
?>
    <!-- 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) -->
<?php 
}

NOTE: Please replace the sample Google Tag Manager code with your code.

Hope it helps.
Thanks

Hi tristup ,

  1. I have Header Footer Code Manager - which probably does the same thing. But both of the plugins probably cannot insert GTM’s body code in the right place, ie right below the opening body tag.

  2. I also have the plugin Google Tag Manager for Wordpress . Problem is, I have too many plugins and I want to reduce them. That’s why I wanted to inject the code directly.

  3. Do you think this approach will help me reduce some fluff / bloat (or not)?

  4. What’s this 20 in (‘wp_head’, ‘google_tag_manager_head’, 20) and 100 in (‘wp_body_open’, ‘google_tag_manager_body’, 100) by the way?

Hi Arif,

It is not possible tell why this plugins are not working, but you can use the Google Tag Manager plugin to avoid adding the custom code. The number 20 and the 100 is the priority of the function within the hook.

Thanks

Hi tristup ,

Sorry for the confusion and the repeat question. I know “Google Tag Manager for Wordpress” plugin works. I just wanted to know that whether the manual injection of the GTM code (the way you explained above) will be better than the use of plugins or at least as good as they are. That’s all. Thanks.

Hello Arif,

You can use any method to add Google tag manager code as mentioned by my colleague @tristup in the above post that is how you add or you need to use the plugin.

Thanks

Thanks prakash_s !

We are delighted to assist you with this.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.