Hack: Placing the Google Tag Manager in WordPress After the body Tag

In this post, I will show how to insert the Google Tag Manager in WordPress immediately after the opening <body> tag. The solution isn’t based on a child theme or JavaScript.

To implement this solution you will need to create a snippet using the Code Snippets plugin.

I call this solution “GTM Injection” (please replace GTM-XXXXXX with your Google Tag Manager container ID):

This solution takes two fair assumption:

  1. The theme’s opening <body> tag ends with <?php body_class(); ?>.
    For most scenarios this should be the case as WordPress asks to implement the body_class() template tag when developing a theme, and from my experience and looking at WordPress twentytwelve – twentyfifteen themes – the opening body tag is <body <?php body_class(); ?>>.

  2. There isn’t any other plugin which filters the body_class hook and has a lower priority than ours (the default priority is 10 – ours is arbitrary set to 10000).
    Unlike the first assumption – perhaps we can eliminate this assumption by using the PHP_INT_MAX priority, $wp_filter global variable, or the plugins_loaded hook – these methods should be further investigated.

Therefore, please make sure that your Google Tag Manager is located and works properly after applying this solution. View your website source code and use the Google Tag Assistant Chrome extension accordingly.

Let me know if this solution works for you.

p.s. Iv’e reached Thomas Geiger, the creator of the popular DuracellTomi’s Google Tag Manager for WordPress regarding adding this solution to the plugin. Hopefully this feature could be implemented soon so that more people could easily place their Google Tag Manager in WordPress in the correct place.

Update: As of version 1.1, the DuracellTomi’s Google Tag Manager for WordPress plugin contains my solution!

Published by

Yaniv Friedensohn

Yaniv Friedensohn

Yaniv Friedensohn is a marketing developer specializing in paid advertising (especially Facebook advertising). Yaniv tries to merge his programming capabilities and online marketing knowledge towards maximizing marketing performance. You can contact him at [email protected]

  • Worked like a charm! Thanks so much.

  • René Rancourt

    Save me hours. Thank you a lot !

  • Awesome, this worked for me!

  • neufsky

    Hi, how do I undo this codeless injection?

    I used the codeless injection option in the DuracellTomi’s Google Tag Manager, but it broke my website front end. Now even if I switch to the traditional method (the “custom” option in the DuracellTomi’s Google Tag Manager), my site is still broken. Only option is to disable the DuracellTomi’s Google Tag Manager plugin.

    This plugin works very well with a test site, using the “custom” option, without ever enabling the “codeless injection” option.

    So Yaniv, can you help?

  • Hi neufsky, I suspect that you may have a conflicting plugins issue. Please try to use the codeless injection on your test site and see if it works.

  • neufsky

    Hi, Yaniv, samething happened after I enabled codless injection on the test site. Does your method leave any codes behind in WordPress after I tried it? How do I undo the changes manually?

  • I don’t know if the solution leaves some code behind as I’m not the author of the DuracellTomi’s Google Tag Manager for WordPress plugin. I suggest that you create a support ticket and ask the author if so.

  • Daniël Mulder

    Hi Great stuff!

    I was wondering if there is a plugin that does the things discussed in this post? A hook that places the snippet directly after the body open tag? I’m new to wp plugin programming and done 2 small projects that both add a script to the website with options. It’s for a live chat and the other to place normal Analytics snippet in the head tag like required. I would like to give it a try if none have been created already.

    PS If you’re interested here you can find then plugins I mentioned https://profiles.wordpress.org/danielmuldernl#content-plugins

    Gr Daniël

  • Daniël Mulder

    Well I tried and I think I have a working proof of concept code for this. I took a different approach: I created a action hook tp wp_loaded event which is called just before the final static output buffer is going to be send to the browser. I change the final html in the output buffer just before it is send to the browser.
    This is the zip file with the code if you’re interested. https://drive.google.com/file/d/0B9rasXyJ1qOibnVQeDRkSFBNV3c/view?usp=sharing

  • fanartdottv

    This doesn’t seem to work in all server setups, it worked fine on our dev server but caused the page to 403 on the live server, this was using DuracellTomi’s Google Tag Manager which I know you aren’t the author of, but the method is the same, commenting out line 630 of /wp-content/plugins/duracelltomi-google-tag-manager/public/frontend.php works

  • DanBangWTFRajib

    thanks… works like charm !!!