How to Add Google Analytics to WordPress With a Simple Plugin
Adding Google Analytics to your WordPress site will help you track who’s been visiting your site, what they’ve been looking at and how they got there.
The easiest way to do it is with a plugin, such as our Google Analytics+, which lets you quickly access a post, page, site or network performance overview without ever leaving your WordPress or Multisite dashboard.
But sometimes you might want to keep things a bit simpler and insert the analytics tracking code into your site yourself. And other times you might want to roll up your sleeves and play with code.
Google tells you to insert the code in the
<head> section of each page on your site, but the great news is that with WordPress you don’t need to do this. You can create a simple plugin that will automatically insert it into every new page that’s created in your site.
What You’ll Need
To create this plugin, you’ll need just a few things:
- A live WordPress installation running the theme of your choice.
- Access to your site’s files via FTP.
- A code editor or text editor.
Note: you can find the source code for this post on Github, so if you’re having any problems, check that out.
If you haven’t created a plugin before, don’t worry. It’s much simpler than you might think. To create this plugin, you’ll follow five steps:
- Check your active theme’s
header.phpfile to ensure it includes the
- Create a new file in your plugins folder.
- Get your analytics tracking code from the Google analytics site.
- Add some code including the tracking code to your plugin.
- Activate the plugin.
So let’s get started!
Checking Your Theme File for the wp_head Hook
Plugins insert code into your site by attaching code to a hook which is already written into your theme. Any well-written theme will have the
wp_head hook in its
header.php file. This hook is used by a multitude of plugins to inserts scripts, styles and more.
If you’ve downloaded your theme from the WordPress theme repository then it should have the
wp_head hook in the right place, but let’s check just in case.
In your FTP program, access the
wp-content folder in your WordPress installation. Inside that, you’ll find a themes folder and then a folder for your active theme. For example, the folder containing the theme files for the twenty fifteen would be at
Note: It’s only the active theme you need to be concerned with. Ignore any other themes you have installed in your site.
In your theme folder, you’ll find a file called
header.php. Open it.
header.php file includes all of the code at the beginning of each page, including the
<head> section of the page. You should find the
wp_head hook at the end of the
<head> section. So for example in twenty fifteen the full
<head> section looks like this:
The last line before the closing
</head> tag includes the
wp_head hook. You can use this in your plugin to insert code at this point without having to edit your theme files.
Why shouldn’t you edit the theme files, you might ask? Well, it’s a bad idea for two reasons:
- If the theme is updated in future, you’ll lose any changes you’ve made to the theme files.
- A Google analytics plugin is about functionality (i.e. what’s going on under the hood of your site) and not display (i.e. how your site looks and displays content), so it should be added via a plugin and not a theme.
So now you know your theme is ready, let’s get hold of the Google analytics tracking code.
Note: If your theme doesn’t include this code, then I’d recommend getting another theme. A well-written theme will always include this, and if it doesn’t, who’s to say where else there may be problems?
Getting the Tracking Code
When you add your site to your Google analytics account, you’re given a tracking code that you need to insert into your site’s pages so that Google can track visitors to each page.
If you have a Google account you can access analytics data using your existing account, or if you’re one of those rare remaining people who don’t have an account with Google, you’ll need to set one up.
Once you’ve done that, you can copy your tracking code from the Admin section of the analytics site.
At the top of the screen, click on Admin to access the admin screen shown in the screenshot.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
Your account will automatically be selected on the left and you’ll need to add your website to the PROPERTY column in the center.
If you haven’t added your site to Google analytics yet, click the PROPERTY drop-down box and select Create New Property. If you’ve already added your site, select that from the PROPERTY drop-down box and then click the Tracking Info button beneath it. Skip the next step.
Adding Your Website to Google Analytics
After clicking the Create New Property option, you’ll be prompted to provide details of your site in the New Property screen:
Fill out the fields with your site’s details and click the Get Tracking ID button to access your tracking code.
Accessing Your Tracking Code
Whether you’ve created a new property or clicked the button to access the tracking code for an existing property, you’ll now be presented with the code to insert into your site:
Copy everything including the opening and closing
<script> tags. You’ll paste all of this code into your plugin shortly.
Note: I’ve blurred out my tracking codes and replaced them with XXX in the code so you can’t track visitors to my site! You need to copy the unique code for your site.
Creating the Plugin
Next, let’s create the plugin which we’ll add the tracking code to.
In your site’s
wp-content/plugins folder, create a new PHP file. Give it a memorable name: I’m calling mine
In your code editor, open that file. Add the following to it:
This tells WordPress that this is a plugin, who it’s by and what it does. Edit the details of the plugin and author as this is your plugin, not mine.
Now below that code add the function which will contain the analytics tracking code:
At the moment this is an empty function called
wpmudev_google_analytics() and it’s attached to the
wp_head hook which means that its content will be output whenever WordPress encounters that hook, i.e. at the end of the
<head> section in each page.
The next step is to copy the tracking code to your function. Inside the function’s braces (and also inside the opening and closing PHP tags inside the braces), paste the tracking code you copied earlier.
Your function will now look something like this:
Activating the Plugin
Now save your plugin file and go to your site admin screens. In the Plugins screen, you’ll see your new plugin listed:
Activate the plugin and go back to the Google analytics site to verify your tracking code. You may have to wait a short while until Google picks up the new code, and then your code will be reported as working.
If you want to manually add a Google tracking code to your WordPress site, then you shouldn’t be adding it to your theme. Instead, the better way is to write a simple plugin like the one we’ve just created.
This plugin includes a function containing the tracking code provided by Google. You then attach that function to the
wp_head hook. This hook is present in the
<head> section of all well-written themes and lets you insert code without having to hack your theme.
Once you’ve done this, you’ll be able to track visitors to your site as part of your SEO strategy.
Of course, if you’ve come this far and don’t want to mess around with code and just want a straightforward solution for viewing Google Analytics for your site, check out Google Analytics +. It’s not only a cinch to set up, but it includes Multisite integration and graphs and stats so you can get a quick overview of site activity.
Do you use Google Analytics? What do you think is the easiest way to add analytics to your site? Let us know in the comments below.