How To Create a Google Analytics WordPress Plugin To Start Using Today

How To Create a Google Analytics WordPress Plugin To Start Using Today

Want to create a plugin for Google Analytics tracking in WordPress? Here’s how to get your plugin created today!

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 simplest and easiest way to do this is to install a Google Analytics WordPress plugin, such as Beehive, which lets you quickly access a post, page, site or network performance overview without 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, play with code, and create analytics plugins of your own.

Google tells you to insert the code in the <head> section of every page on your site, but 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 on your site.

The good thing about adding Google Analytics to WordPress using a plugin is that if you update your theme, it will not remove the analytics code from your site.

In this tutorial, we’ll show you how to create a simple Google Analytics plugin that will add the code to every page of your WordPress site.

What You’ll Need

To create a Google Analytics plugin for WordPress, 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.

I’m using Coda, which gives me code editing functionality and FTP all in one place, but you could use any code or text editor along with a free FTP client such as Filezilla.

Note: you can find the source code for this post on Github, so if you’re having any problems, check that out.

The Process

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:

  1. Check your active theme’s header.php file to ensure it includes the wp_head hook.
  2. Create a new file in your plugins folder.
  3. Get your analytics tracking code from the Google Analytics site.
  4. Add some code including the tracking code to your plugin.
  5. 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 that 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 Nineteen theme would be at wp-content/themes/twentynineteen.

Note: It’s only the active theme you need to be concerned with. Ignore any other themes you have installed on your site.

In your theme folder, you’ll find a file called header.php. Open it.

The 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 the Twenty Nineteen theme, the full <head> section looks like this:

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?

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 the 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.

Once you know your theme is ready, the next step is to get hold of the Google Analytics tracking code.

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. If you’re one of those rare people who don’t have an account with Google yet, you’ll need to set up a Google account.

Once you’ve done that, all you need to do is make sure that you have added your site to Google Analytics and then copy your tracking code from the Admin section.

If you need help with this step, check out our tutorial on how to add Google Analytics tracking code to WordPress.

Google Analytics tracking code.
Google Analytics code example.

Every site you add to Google Analytics has its own unique ID.  Copy everything in the tracking code, including the opening and closing <script> tags. You will paste this code into your plugin shortly.

Creating the Plugin

Next, let’s create the plugin that we’ll be adding the tracking code to.

Note: Make sure you create a backup of your site in case you do something accidentally that messes up your code.

In your site’s wp-content/plugins folder, create a new PHP file. Give it a memorable name: I’m calling mine wpmudev-google-analytics.php.

In your code editor, open that file. Add the following to it:

This tells WordPress that this is a plugin, who the author is, and what the plugin does. Edit your plugin and author details to match.

Now below that code add the function that 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 on 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 to your clipboard earlier.

Your function will now look something like this:

Activating the Plugin

Now, save your plugin file and go to your site admin screens. Your WordPress Google Analytics plugin should be listed in the WordPress plugins screen:

WordPress Google Analytics plugin google analytics plugin
Your Google Analytics plugin in the WordPress plugin admin screen.

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.

Congratulations! Now you know how to create a plugin for Google Analytics and WordPress.

Adding Google Analytics To WordPress – Recap

Learning how to add Google Analytics to WordPress is not hard but adding Google Analytics without a plugin in WordPress is a lot trickier.

If you want to manually add a Google tracking code to your WordPress site, then you shouldn’t be adding it to your theme.

A better way is to create a Google Analytics plugin for WordPress like the simple plugin we’ve just you how to create. 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.

The best way 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, is to use a plugin like Beehive. 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 your site’s activity. And, of course, there are other Google Analytics plugins for WordPress that you can install.

Google Analytics Plugin Installed? Great! Here Are Your Next Steps

Once you’ve added Google Analytics done this, you’ll not only be able to track visitors to your site as part of your SEO strategy but if you offer WordPress management services to clients, you can integrate analytics with the WPMU dashboard and add it to all your client reports (nice!).

If you’re not a member of WPMU DEV yet, you can still create client PDF reports with WordPress Google Analytics.

You can also run Google Analytics experiments, like A/B tests, track multisite analytics, and a whole lot more!

For more information on using Google Analytics with WordPress, see our comprehensive guide to Google Analytics and understanding your WordPress site traffic.

Tags:
Rachel McCollin
Rachel McCollin Rachel is a freelance web designer and writer specializing in mobile and responsive WordPress development. She's the author of four WordPress books including WordPress Pushing the Limits, published by Wiley.
Martin Aranovitch
Martin Aranovitch Martin believes that we can all live sustainably in a WordPress-centric digital universe and spends most of his time taking copious notes and creating epic tutorials to prove his theory. Before joining WPMU DEV, Martin authored many WordPress guides and courses for beginners.
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.