• Skip to main content
  • Skip to primary sidebar

How to Add Google Tag Manager (GTM) to WordPress?

Reading Time: 5 mins. Posted on December 23, 2019, last updated on March 25, 2020 .

3 ways to add Google tag manager or GTM code to a WordPress Blog. Edit header and footer files, or edit functions.php. Finally, use a plugin

How to Add Google Tag Manager (GTM) to WordPress?

There are three ways to add Google tag manager (GTM) to a WordPress blog. The first method is editing the header and footer PHP files within the theme. Next is to edit the functions.php theme file, and finally, if you don’t have any programming experience, use a simple header footer plugin.

We will look into each of them, but let me first share a bit about GTM.

What is Google Tag Manager or GTM?

The Google Tag Manager or GTM cuts down the need for writing codes to track different visitors’ activity. As an example, what is the average page scroll for the user, and how many pages user visits from individual entry pages?

Instead of writing code for tracking each such event, one can use Google Tag Manager.

For instance, you can create a tag that will tell you how many visitors have read a particular blog or say how in-depth they scrolled to read it. Google Tags will send relevant information on an exact activity to Google Analytics or Google Adwords or any other third party tools.

A non-technical user can create tags and track events. Here are a few examples of what one can track in GTM.

  • Track External Link Clicks
  • Tracking Form Submissions

Generate Google Tag Manager Code

Sign in to Tag Manager. Do input your account name, country, website URL, and select the container – Web, iOS, Android, or an AMP website. Click create.

New account setup in google tag manager

The following example image shows the tag manager dashboard. Look at the unique ID starting with GTM. For example, my ID here is GTM-M7V6H9N. Click on the ID.

When you click on your ID, the Google tag manager shortcode window opens up. Copy the codes in a notepad file. You will need them in a while to insert them in your WordPress backend.

Google tag manager code

Now we will head over to your site or blog’s WordPress backend to install the GTM code.

Inserting Google Tag Manager Code in WordPress

Now we will go through all the three ways to enter the GTM code in WordPress.

  • Modifying themes Header and Footer Files (Edit HTML)
  • Changing themes Functions File (PHP Programming)
  • Using a Simple Plugin (No Programming Needed)

Method 1: Insert GTM code to WordPress via Themes header and footer File (header.php & Footer.php)

In the WordPress admin, Click the theme editor under appearance option.

Now you’re on the edit themes page. On the right side, select the theme you want the code to insert the code into. Generally, your theme is chosen as default. In our example, I have taken the twenty-twenty theme.

google analytics code in header dot php

Within the “theme files” section, locate the theme header (header.php). Click on it, and the code of the header.php file will load for you to edit, as shown in the example image above.

Find the closing head HTML tag </head>. Paste your GTM header code above the HTML </head> tag. Finally, click the update file button.

Now locate the theme footer (footer.php). Click on it, and the code of the footer.php file will load for you to edit, as shown in the example image below.

gtm code in the wordpress footer

Find the closing body HTML tag </body>. Paste your GTM footer code above the HTML </body> tag. Finally, click the update file button.

Pro tip: Though google suggests adding the code at the opening of the body tag, having it higher at the start of the body tag isn’t something I recommend. Google’s page speed will report it is slowing down the page loading. If you still prefer adding at the opening body tag, you can insert this code in the header.php just after the opening <body> HTML tag and before the wp_open_body function as well.

Your Google Tag Manager code is installed. Once installed, you can connect GTM with Google Analytics.

Method 2: Insert GTM code to WordPress via Themes functions File (functions.php)

At times, in many themes, the header file doesn’t have the HTML head elements. They may be in some other files. Genesis is one such theme where you won’t find the </head> tag in the theme’s header file.

So, in that case, we opt to edit the theme’s functions.php file.

The step remains the same as in the above method of editing the header.php except we modify the theme’s functions.php file.

Scroll down to the end. Copy the code and insert your tracking code between the lines, as shown as comments.

<?php
//* Copy the above PHP opening tag if and only if your functions.php has a closing PHP tag
function add_gtm_head() { ?>
<!-- Paste Your GTM Header Code Here -->
<?php } add_action('wp_head','add_gtm_head',10);
function add_gtm_footer() { ?>
<!-- Paste Your GTM footer Code Here -->
<?php } add_action('wp_footer','add_gtm_footer',10);
view raw functions.php hosted with ❤ by GitHub

Then place the code at the end of the functions.php file.

Method 3: Insert GTM code to WordPress via a Plugin

If you don’t want to be dealing with HTML or PHP, we can use WordPress Plugin Insert Headers & Footers by WPBeginners to install the tracking code. Now installing this tracking code manually into a site’s HTML is not something I would suggest you do unless you know how to do it accurately.

To do this, you will first need to install the plugin in WordPress. Follow the steps below:

  • Log into your WordPress account.
  • Hover mouse on the plugin tab & click the add new option on the left panel.
  • Search for the plugin Insert headers and footers in the search bar. Top right.
  • Click install now and activate the plugin after that.

Example image for reference purpose.

Add insert headers & footers plugins in wordpress

However, you can download this plugin and upload it here using the upload plugin function. There’s no difference in it. Additionally, you can use other similar plugins compatible with your WordPress version. Once the plugin is installed, locate it and insert the Google tag manager code. Here’s how you will do it.

Hover mouse over the settings tab and click insert headers and footers plugin. Now copy and paste the code in header and footer space provided. Finally, click save when done.

google tag manager code in wordpress using plugin

You are Here: Home / Tools / Google Tag Manager / How to Add Google Tag Manager (GTM) to WordPress?

About Shabbir Bhimani

Blogging Since 2009. If I can leave my high paying C# job in an MNC in the midst of global financial crisis of 2008, anybody can do it. @BizTips I guide programmers and developers to Start and Grow an Online Business. Read more about me here.

May I help You With ...

Upwork Proposal
Finding Clients
Start a Store
Start a Blog
 

Or Help Yourself ..

Primary Sidebar

About Shabbir Bhimani

Blogging Since 2009. If I can leave my high paying C# job in an MNC in the midst of global financial crisis of 2008, anybody can do it. @BizTips I guide programmers and developers to Start and Grow an Online Business.

Get in touch with me on LinkedIn or read more about me here.

Let me Guide You to Start and Grow your Online Business

Download my
FREE eBook NOW
to win more clients.
And it is not an annoying pop-up either

Additional menu

  • Twitter
  • Linkedin

BizTips

Shabbir Bhimani: Start and Grow an Online Business

  • Freelancers Start Here
  • Start An eCommerce Store
  • Start A Blog
  • About
  • Archive
  • Disclaimer
  • Contact
  • Glossary

2009 - 2025 All my content & images are licensed as Creative Commons.

WebTurtles LLP. LLPIN: AAL-5288. Hosted with Linode.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT