Google Tag Manager

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.

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.

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.

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.

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.

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.

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.

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.

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.

Recent Posts

Absolute Beginners Guide to Google Ads

Free Google Ads Tutorial for an absolute beginner covering campaigns and their types, networks, keywords,…

2 years ago

How Freshers Can Make Money Programming in Java

Do you want to make money programming in Java? Here are 6 creative ways for…

3 years ago

Financial Freedom for Freelancers – 9 Smart Ways

9 smart ways for freelancers to achieve financial freedom. First, you have to believe it…

3 years ago

How To Become A Self-Taught Developer

With so many programming languages and resources available, it can be information overall to learn…

3 years ago

Wise Review – Best Exchange Rates For Indian Freelancers

Wise review from an Indian freelancers point of view to receive payments in India. How…

3 years ago

How to Tell a Client you Can’t Lower the Price?

There are 3 types of clients who want you to lower the price. How to…

3 years ago