Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Install and Setup Google Tag Manager in WordPress

Are you looking to setup Google Tag Manager in WordPress? Not sure if you need Google Tag Manager on your WordPress site? Don’t worry. In this article, we will show you how to properly install and setup Google Tag Manager in WordPress and explain the benefits.

What is Google Tag Manager?

Google Tag Manager

Often website owners need to add multiple code snippets to track their conversions, traffic, and gather other analytical data.

With Google Tag manager you can easily add and manage all of these scripts from a single dashboard.

There are built-in tag templates for Google Analytics, Adwords, DoubleClick, etc. Tag manager also works with several third party analytics and tracking platforms.

Apart from that, you can use custom HTML to add your own tracking or any other code that you want to add.

This saves you the time of adding and removing tags from your code and rather manage it from an easy dashboard. Not to mention, all of these scripts are loaded in one script.

Having said that, lets get started with Google Tag Manager.

For the sake of this article, we will show you how to add Google Analytics to your site using Google Tag Manager.

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions, then continue reading.

Getting Started

It’s important to note that this guide is for self hosted WordPress.org sites (see the difference between WordPress.com and WordPress.org). If you don’t have a WordPress site, then please see our WordPress installation guide to create one.

First you will need to create a Google Analytics account for your website if you haven’t already done so. You can follow the instructions in our how to install Google Analytics in WordPress tutorial.

Follow the steps mentioned in the “how to signup for Google Analytics” section. Stop when you get the tracking code. Instead of adding tracking code directly in WordPress, we will be using the tag manager to do that.

Setting Up Google Tag Manager

Now that we are ready to add Google Analytics, the next step is to setup Google Tag Manager.

Visit the Google Tag Manager website and sign up using the same Google account you used with Google Analytics.

After login you will come to the add new account page. Simply provide a name for your account and click on the continue button.

Creating Google Tag Manager account

Now you need to add a container to your account. A container could be a website or an app on iOS or Android platforms. In this case we will use the web.

You can use your site’s domain for the container name. You can add multiple containers for each account. Once you’re done click on the create button to continue.

Creating a container

Google Tag Manager will now create your account with the container, and you will see your container dashboard. It would look something like this:

Container dashboard

Now your Google Tag Manager account is ready, so the next step is to add a new tag. You can do this by clicking on create new tag.

First you will have to choose a tag product. Currently there are 20+ integration templates available.

We will choose Google Analytics for now, but you can always add more tags and choose different products according to your needs. You can even click on custom HTML to add any other custom code snippets.

Choose Google Analytics as product for your tag

Once you choose Google Analytics, you will be asked to select between Universal Analytics or Classic Analytics. Choose universal analytics, and you will be asked to provide your analytics tracking ID.

This tracking ID is a special code that allows Google to identify your site. You can obtain this ID by logging into your Google Analytics account and visiting the Admin tab.

Select the account and property for your site and then click on tracking info to expand it. You will see the tracking code link.

Finding your Google Analytics tracking ID

Clicking on tracking code will bring you to the tracking code page and will also reveal your UA tracking ID.

UA tracking id in Google Analytics

Copy your UA tracking ID and paste it in Google Tag Manager. Under track type choose Page View and then click on the continue button.

Add tracking id

The tag configuration wizard will now ask you when to fire this tag. Select all pages and then click on the create button.

When to fire on the tag

Google Tag Manager will now show you a tracking code that you need to add into your WordPress site. You need to copy this code.

Tag manager code

Adding Tag Manager Code in WordPress

First thing you will need to do is install and activate Insert Headers and Footers plugin. Upon activation, you need to visit Settings » Insert Headers and Footers.

Add your Google Tag Manager code in WordPress
Paste your tag manager code in the header section and click on the save button to store your settings.

If you do not wish to install a plugin, then you can also add the Google Tag Manager code in your WordPress theme or Child theme. Simply edit the header.php template and paste the code right after <body> tag.

Publish Your Container

Tags in Google tag manager will not go live until you publish the container. Visit Google Tag Manager dashboard and then click on the publish button.

Publish your changes in Google Tag Manager

You can also preview your website and Google Tag Manager will show you the tags fired on each page view.

Previewing and debugging

That’s all, you have successfully installed and setup Google Tag Manager for your WordPress site. You may also want to see our guide on 7 best analytics solutions for WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

51 CommentsLeave a Reply

  1. as a beginner, this post was given to me from siteground guy to help me and thanks to you as well .thankyou

  2. Hi there,

    This solution works as a charm, thanks a lot. I have 1 issue though: All the way at the bottom of all my site pages there is now a white bar with the text “Javascript” written inside it on the left side of the screen.

    How can I remove that?

    • You may want to make sure you copied the entire code as that normally means javascript code you’ve added to your site may have an error.

      Admin

  3. I’d rather not install more plugins than I need. My theme allows me to paste the GTM code with the GA tag in my header. However, how do I get the body code in my page? Do i just use a text field high up on my HOME page and paste it in there? Thanks!

    • Hey Ann,

      I know I’m late to reply but I’d highly recommend you install the plugin.

      Without it, you won’t be able to send detailed information to GA. I.e. If it’s an eCommerce – you won’t be able to receive product information, GTM on it’s own will give you extremely basic data and you’ll need to configure it entirely yourself which believe me takes weeks!

  4. Google Tag manager specifically says to post a portion of the code in the body of the webpage. Nothing I find on implementing Google Tag manager addresses this. Why is that?

    • The video hasn’t been updated in a while. What you have to do is put the 2nd half of the code in the footer section of the plugin, which actually places it in the body section. If you look at the plugin in wordpress it says “These scripts will be printed above the tag.”

      Anything above ” is going to be in the body section.

  5. Hi Admin,

    I don’t have good knowledge on WordPress.
    I am using WordPress theme for my website. And want to add google tag manager in my website. Can you please tell me how can I add tag manager code in WordPress Child theme. I am waiting for your positive response.

  6. Where shoud I put “Additionally, paste this code immediately after the opening tag” on Header and Footer Plugin ?

    • Th newer version of GTM uses two codes. Place the first one in the Head and the second one just after the opening Body tag.

  7. Hi i want to ask when i have saved i am not able to find my code so where i can find my code

  8. Wonderful guide. I got one major confusion cleared now – whether to add google analytics code first and then add google tag manager tags. Thanks a lot for this awesome help. A big time saver.

  9. Hi,

    There is two elements to Google Tag Manager install

    1) in Header
    2) in Body

    Will you update your explanation to show the solution for this?

  10. Also, there’s another piece of code besides the code you mention here. They ask you to “Paste this code as high in the of the page as possible:”, and the code is:


    (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’);

    Can I install both pieces of code in the header, right next to each other?

    Thanks!

    • Yes, Google has changed this very recently. I want this also up-to-date and by Function.php instead of a plugin. Anybody???

  11. Hi, can I have both GA and GTM in the code at the same time? Or I have to remove GA if I’m installing GTM?

    Thanks!

    • Hi AJ,

      you can have both (GA + GTM) in the code at the same time. No worries. Although it is more convenient to fire the GA snippet also through GTM. Google provides good instructions how to do that:

  12. Am I wrong or there is a bit of lack of support for your plugin on wordpress forum? It’s a pity… :(

  13. Great tutorial – I love you guys. I read it through and then read all the comments. Got my answers. Installed it etc and then checked my site by browsing from my smartphone.
    Everything is working great!

    No questions, just a big thankyou.
    Well done :)

  14. I got This completely, But I have a confusion about there is my analytics code exist to how can I both through plugin insert header & Footer.

    • just place the google tag manager code below your google analytics code using plugin recommended.

  15. I’m very new to Google Tag Manager, this is the easiest tutorial I’ve found so far without the confusion.
    However I’m wondering if there is a conflict with having both GA code and GTM code on my website?
    There is a place on my wordpress theme to insert the Google Analytics code for tracking which I have always had.
    I have now inserted the Google Tag Manager code to the header php on my site so should I remove the original Google Analytics code or does that stay?
    Many Thanks,
    Catherine

  16. Thank you for making the plugin Multisite compatible!

    I see Google Tag Manager says to place the code immediately after the opening tag, but the plugin places it inside the tag. Will it still work if placed in the head rather than body?

  17. Thank you so much for this excellent tutorial.

    I am confused about a potential conflict with this method. Can you please help?

    Google Tag Manager says that the code can not be in the header. They want it right after the tag. But, if I use the Insert Headers and Footers plugin and paste the code there, won’t it put the Tag Manager code in the Header? Won’t this cause a conflict?

    In the paragraph, you wrote: “If you do not wish to install a plugin, then you can also add the Google Tag Manager code in your WordPress theme or Child theme. Simply edit the header.php template and paste the code right after tag.” Is there a step-by-step on how to do this?

    • You might have wondered why Google so forcefully recommends that you place the container snippet just after the opening tag. There are two good reasons to do so.

      First, if you add the tag as well (as you should), this should always be in the body of the document. The tag (which is shown for browsers without JavaScript enabled) contains an iFrame which loads the GTM library. If you add the tag into the head of your document, it can perform pretty wildly with some browsers. You could experiment with leaving the in the body, and placing the JavaScript in the , but I haven’t tested it and certainly don’t recommend it.

      The second reason is simple: to maximize data collection, the snippet should be the first thing loaded when the body of the page is rendered. Because the library is loaded asynchronously, there’s no reason to delay it at all. So have it load as the first thing when the body is rendered, so that you don’t risk losing any data due to sluggish DOM elements delaying the page load.

      • So, if you want it to load right after the tag, then is seems like you can’t use the Insert Headers and Footers plugin because the only options are in the (nope) and right before the tag (so at the end of loading, nope).

        Am I reading this right?

  18. Curious about duel installs, of both, on WordPress — I know you have to have both accounts setup in google, but does the tag manager, in effect, *replace* the GA plugin we’ve been using for a few years?

    Or, does one still install both the GA plugin AND the tag manager plugin? Seems redundant, or perhaps I’m not understanding things clearly.

    Just trying to know the most efficient way to get all the analytics I enjoy now, and all the tag manager analytics as well.

    Thanks,
    S

    • Tag Manager is not an analytics solution in itself. It just provides an easier way to add analytics tracking in your WordPress site. If you add Google Analytics by adding it through a tag in tag manager than you can safely uninstall Google Analytics plugin on your WordPress site.

      Admin

  19. Sorry i might be slower to understand, but what is the difference between google analytic and tag manager ? I mean, what am i missing using analytic only ?

    • Google Tag Manager simply makes it easier to add analytics, manage and track events, and add third party scripts to your website. Tag Manager is not an alternate to analytics.

      Admin

  20. What a detailed and step by step guide on GTM. I just completed my training on this and now I am completely clear on this.
    I believe it has nothing to do with SEO and ranking, it is just for managing all the tags we could us on our website.

    Regards
    Susan Meko

  21. Videos please to go with the written instructions. Videos I watch, the written stuff, almost never.

  22. Hi, may I ask. I have two wordpress installed in one domain. eg: http://www.wp1.com/wp2.

    Will tracking wp1 be able to give me the stats of w2. If not, how may I achieve that?

    Looking forward to your response.

    Thank you in advance.

  23. That is great , simple tutorial with details. A friend told me about it but didn’t explain to this extent … Please apart from managing all script in a single dashboard does it in anyway improve the site SEO…. ??

  24. Brother your article is very informative. Can we used google analytics and google tag manager code together?

Leave a Reply to Naji Boutros Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.