Build A WordPress Plugin to Add Google Analytics Code

how to build a wordpress plugin for your google analytics tracking codeWhen it comes time to add Google Analytics code to your WordPress website, there are a whole lot of options these days. Some themes will give you a field to paste the code. Many plugins have been created around adding giving you various options, whether just for the Google Analytics code or the bigger topic of SEO, such as SEO by Yoast. If you have the chops of a programmer you might even edit the header or footer files of your theme (or better yet, child theme) to add the code as well.

So why build your own plugin?

Each of the options above also come with their own down side. If you add the code to your theme, whether a theme option or editing the code, you have to remember to re-add it if you change the theme later on. If you are seeking a plugin just for the purpose of adding your Analytics code, it probably comes with more features (weight) than you will actually use.

The easy, lightweight solution

When I add Google Analytics code to sites, whether I built the site or I’m just adding the code, I prefer to use my own plugin. It’s a super simple plugin that doesn’t even add the weight of an options page and field. Another advantage is that I don’t need FTP access to the site, just an administrator level account in WordPress.

Keep in mind that this is not a deep dive into creating WordPress plugins, but it does demonstrate the essentials. The principles here could apply to a wide variety of uses and I certainly use the custom plugin concept for just about all of the custom functions I add to a WordPress site project.

Step 1

In a text editor, we need to create a new file that will house the code that creates the plugin. Don’t be scared by the word code, however. Technically a WordPress plugin doesn’t require any code, just a few lines of comments in a file, in order for it to be seen as a plugin. Of course without code your plugin won’t actually do anything.

Minimum required text for a plugin:

Technically, that’s it! Since you are creating this plugin for yourself, you don’t really need all the additional comments to describe where to find more information, author name/email, etc., but since I use this plugin on a variety of client websites I prefer to give more details here so they can find me later or just know that it’s not a good idea to remove my plugin.

Here’s the complete list WordPress recommends here:

Step 2

Now for the code that will add your Google Analytics script code.

What we have here is a function that uses the wp_footer hook and adds it’s contents to the footer of every page. The actual content of the function at this point is just to prepare the php code to receive a chunk of javascript. I have intentionally left out any code related to creating an options page in the WordPress admin area or fields to receive settings. You’ll understand why when I get to the final step.

Step 4

At this point, you could copy and paste the above code, add your Analytics code where indicated, save it as a .php file and FTP it up to your plugins folder and your plugin would be a functional that once activated would serve its purpose. Let’s take this one step further, though, and make this a reusable plugin you can add just like any other plugin.

To do this, don’t actually add your Google Analytics code just yet. Instead, save your file as a .php file and just to help keep your plugins folder clean, save it inside a folder with a similar name. Don’t get too generic with your folder name so it will not match any other plugin folders.

Now zip your folder with the file inside and save it somewhere you can always find it.

When you want to add it to a site, just use the WordPress plugin installer and choose to upload your own plugin. After you upload it, click activate and it will be ready for action.

Step 5

Now that your plugin is activated, the only thing left to do is add the Google Analytics created specifically for this site. If you haven’t done this yet, you do so logged into Google Analytics by creating a new property.

To add your Analytics code, you just need to use the WordPress editor. You can get to either from the Editor link under the Plugins option and selecting your plugin, or from the Edit link under your plugin’s title on the Plugins page. In the editor you just paste your code where indicated in the code we added. Save it and that’s it, done!

The wrap up

Now you have a super simple, lightweight plugin you can use on all the websites you are needing to add Google Analytics tracking code. This same example will work with any PHP functions you are needing to add to WordPress site. When you see code examples that say “Add to your functions.php” it’s a far better idea to add them to your own custom plugin.

Done for you

Finally, if you’ve read this tutorial and would still prefer to just download the finished plugin and not have to hassle with following any of these steps, you’re in luck! I took the lessons above and wrapped them up into a finished plugin. Just download from this page. All I ask is that you follow my blog to download the zip file. No tricks, you can always remove yourself from the mailing list, but I’m sure you will enjoy automatically receiving a link every time I post a new article. Download as a plugin.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.