WordPress X Theme: How To Setup Google Tag Manager

Written by | Date Updated: September 25, 2020

google-tag-manager-wordpress-xIf you are a user of the WordPress X Theme, it can be a little bit complicated to setup Google Tag Manager if you have never done it. But fear not, I shall make it very easy.

The reason that it can be complicated to setup Google Tag Manager with the WordPress X theme is because the Google Tag Manager code must be added in two places — the head section and directly below the opening body tag, as you see in the photo below:


NOTE:  Google Tag Manager shows the information above only when you first install it.  However, if you need to view it again, click the “GTM…” button within your Tag Manager workspace.


So, let’s get to how to add this code to the WordPress X Theme.

Adding the Google Tag Manager Code in X Theme

In WordPress, we are often called upon to add snippets of code to the header file.  Those not programatically inclined often use the go-to plugin called Insert Headers and Footers.

However, in this instance, we also need to add that bit of code below the opening body tag so Insert Headers and Footers won’t get the job done.

Fortunately, there is an easier (and dare I say cleaner!) way to do this than installing a plugin.

We are going to modify a file called Functions.php.

Those of you who have been around the block know how to locate and modify this file.

For those new to WordPress, you can find your functions.php file by clicking Appearance | Theme Editor.


Before going any further with this, I recommend that you have installed the WordPress X Child theme.  That way, your Google Tag Manager installation will never get overwritten.  Also, before proceeding with the next step, I recommend that you have a good backup of your functions.php file and know how to replace it.

So, let’s get the Tag Manager code installed.

First, open the functions.php file — Click Theme Functions on the right to do that as you see in the image below.


Next, copy/paste the exact text that you see below, adding your Google Tag Manager code where it says to do so.

add_action('wp_head', 'my_gtm_code', 9999);

function my_gtm_code () { ?>

<!--  Add your GTM Head Code below this line -->

<?php }

add_action('x_before_site_begin', 'gtm_body', 9999);

function gtm_body() { ?>

<!--  Add your GTM Body Code below this line -->

<?php }

The code above is discussed in this X Support ticket.

Make sure to click the “Update File” button at the bottom to save your changes to this file and you’re all set — Google Tag Manager has been installed on your WordPress X site.

Not too bad, huh?

But, as you may or may not know, Google Tag Manager on it’s own does nothing.

It simply provides us an easy way to now install tracking codes for many situations.

The first tracking code that is usually setup is Google Analytics. Shall we do that now?

Ok, let’s move on to how Google Analytics and Google Tag Manager Code work together and ask: Do I still need Google Analytics tracking code if I have Google Tag Manager tracking code installed?

Richard Cummings

Director of SEO, Social Media, and Web Content Development at The SEO System
Richard Cummings has been practicing online marketing for many years and has setup and optimized hundreds of WordPress sites.He founded The SEO System to provide SEO, social media, and online marketing services and software to businesses.
Richard CummingsWordPress X Theme: How To Setup Google Tag Manager