When it comes to adding scripts to WordPress, most people recommend that you use a plugin like Code Snippets or your own custom plugin (or, if you’re wild and crazy, you can stick them right in your child theme’s template files).
But lately on some of the Facebook WordPress groups, I’ve been seeing people talk about using Google Tag Manager to manage the scripts that they insert into their WordPress site.
I was already familiar with Google Tag Manager from an event and conversion tracking perspective, but this got me interested in just how easy it would be to use Google Tag Manager to add code snippets to WordPress.
So…I wrote this post! I’ll start with how to add Google Tag Manager to WordPress. Then, I’ll build up to how to add other third-party scripts via Google Tag Manager.
In the end, I think it’s a neat implementation that allows you to easily manage all of your code snippets in one place.
What Is Google Tag Manager?
Google Tag Manager is, as the name suggests, an official product from Google that helps you add “tags” to your website.
Next question – what is a tag?
The main thing you can’t put into a tag is PHP because PHP is executed on your server.
But anything else? That’s fair game. That means you can use Google Manager to add stuff like:
- Tracking scripts for things like heatmaps, analytics, etc.
Why Use Google Tag Manager Instead Of Adding Those Scripts Directly To WordPress?
The main answer is convenience (depending on your workflow). The entire point of Google Tag Manager is that it makes it easy to quickly roll out new tags as needed.
Now, that doesn’t necessarily mean it’s a more convenient method for everyone. If you’re not planning to use Google Tag Manager to also track events and conversions, you might not find it very helpful to need to go to the Tag Manager interface just to manage scripts.
But if you are planning to use Tag Manager, the benefits are that:
- All of your scripts are collected in one place
- It’s easy to only run scripts on specific pages, whereas most WordPress plugins force you to run scripts site-wide. With Tag Manager, you have powerful triggering options to determine exactly when to run scripts.
- Tag Manager has strict version control, which is helpful if you’re working on a large site
In a nutshell, this method isn’t for everyone. But if you’re already interested in event tracking and conversion tracking (which I recommend), you might prefer this method because you can do everything from the Google Tag Manager interface.
How To Add Google Tag Manager to WordPress And Insert Other Tracking Scripts
Alright, let’s actually get into the “how-to” part now.
Unfortunately, to start using Google Tag Manager, you’ll have to…add the Google Tag Manager code snippet to WordPress manually.
But once you do that process one time, you’ll be able to add all future code snippets from the Google Tag Manager interface.
Step 1: Sign Up For Google Tag Manager
To get started, sign in to Google Tag Manager with your Google account. Once you sign in, you should be prompted to Add a New Account.
First, enter your Account Name. It can be whatever you want.
Then, enter the URL to your site in the Container name, choose Web under Where to Use Container, and click CREATE:
Step 2: Add Google Tag Manager Scripts To WordPress
This is the part where you need to add the Google Tag Manager tracking scripts directly to your WordPress site…so that you can more easily add other tracking scripts later.
I’ll try to make it as painless as possible for you – you’ll need to add these two code snippets to the functions.php file of your child theme.
Here’s the first snippet:
And here’s the second snippet:
You can add this code by going to Appearance → Edit and selecting the functions.php file of your child theme. Here’s what it should look like when you replace the filler text with your tracking codes:
And that’s it! Now you have Google Tag Manager installed on your WordPress site.
You can also use the Code Snippets plugin if you’d prefer.
Step 3: Create New Tag In Google Tag Manager Interface
Now that you’ve added the Google Tag Manager code to WordPress, you’re ready to add your first script.
To do that, head back to the Google Tag Manager interface.
You should see something like this (after closing the box that displayed the code snippets):
To get started, click that big New Tag button.
Then, you’ll be prompted to Choose a tag type to begin setup:
You’ll see a slide-out with a number of options. Google actually already includes pre-built integrations for a number of popular tracking services. But if you want to add your own script, you should choose the Custom HTML option:
Then, all you need to do is paste the script into the box. I’ll use the Get Site Control widget script as an example:
Once you add your script, scroll down to the Triggering section and click on Choose a trigger to make this tag fire:
Your script will only fire when this trigger condition is met.
The default option is All Pages which makes your script trigger on every single page. Most of the time, you’ll want to choose this option because it lets you run your script site-wide.
But, if you only want to run your script on certain pages of your site, you can also use the Plus sign to configure a custom trigger:
Once you’ve added both your script and your trigger, go ahead and click SAVE:
At this point, your tag is finished…but it’s not yet live on your site. First, you need to….
Step 4: Publish Your Workspace
Before your script is live, you need to publish your workspace. To do that, click the SUBMIT button:
Then, give it a name and a description to help you remember what changes you made. And once you do that, all you need to do is click the PUBLISH button:
And that’s it! Your script is now live on your WordPress site.
If you want to add more scripts, all you need to do is repeat the process.
Managing Existing Tags And Scripts
If you ever want to manage your existing tags, you can quickly pull up a full list of your tags by clicking on the Tags option in your workspace:
There, you’ll see all of your tags as well as their triggers:
Wrapping Things Up
Using Google Tag Manager to manage the scripts that you add to WordPress is a slightly advanced, but very neat way to do things.
While beginners should probably steer clear of this method, intermediate users might prefer it for its flexibility and the fact that you can also use Tag Manager to track events and conversions.
To get up and running, you’ll just need to complete four simple steps:
- Sign up for a Google Tag Manager account
- Add Google Tag Manager scripts to WordPress
- Create a new tag
- Publish that new tag
If you have any questions, don’t hesitate to ask! And if you know any neat tricks, I’d love to hear them as I’m still unlocking the full power of Google Tag Manager myself.