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 our Google Analytics , 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  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 section of the page. You should find the wp_head hook at the end of the section. So for example in the Twenty Nineteen theme, the full 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 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