BlogTutorialsWeb Design Tricks and Tips

Creating a Custom Google Analytics Plugin for WordPress

By January 23, 2014 No Comments

Often, I come across an article that provides some really helpful info that I need to reference often and or a combination of articles that seem necessary to create an end goal, and this is one of those times. I create a lot of sites and almost always integrate Google Analytics.

Many of the newer themes out there include a separate form box that allows you to add your Google Analytics code in but many are lacking in some advanced functionality like link / event tracking, etc. that I need to include. Prior to this article, I would simply add the GA right into the header.php but there are many pitfalls to that approach, most namely losing your changes when you change themes or run a theme update (if you aren’t using child themes appropriately)…

Many thanks to WP Ninjas for the two articles they wrote that bring this tutorial together:


Step 1: Create the Plugin Folder and File

Creating a custom plugin is easier and more useful than I ever imagined.  at the core of every custom plugin is a directory folder to live in your /plugins folder (typically wp-content/plugins/) and a .php file to create your function.

  1. First, create a folder in your plugins directory with your plugin name (use lowercase and no spaces for best results): For example:   /wp-content/plugins/my-analytics-plugin
  2. Then, create a .php file and name it the same way: e.g. my-analytics-plugin.php

The content of that php file needs to start with the WP header info to name it and associate it appropriately.  All that is required is the plugin name but you can give it some additional info to help in the admin screens. Here is an example:


Step 2: Add the Functions to Your .php File

Then below the plugin information, we need to include the Analytics info (change the UA Account into to your account ID):

You can, of course customize the Analytics code from there. I often need to add things like cross domain tracking, _setAllowLinker, etc., hence why I go through this exercise.  Also, note in the example above, I wanted to call the Analytics in the <head> of the page because my cross domain tracking efforts require that.  You can place it in other places (such as the footer) with the WP Action Hooks… For Example, change line 1 in the function above to reflect “wp_footer” in place of “wp_head”:

I don’t mess with this much so feel free to read up if you want more:

Step 3: Upload and Activate

From there, you can upload your folder to your plugins directory, visit your plugins admin and activate your plugin and you are off and running!


I’ve said it over and over, I am not a programmer or developer so always do this at your own risk and backup, backup, backup.

If you were to do something on accident and mess up your code or add a bad function, I have always had some luck with either removing the plugin’s folder from the plugin directory  or changing the file extension to a .txt extension to automatically deactivate the plugin…

Hope that helps some out there and thanks to WP Ninjas for the reference!



Author admin

More posts by admin

Leave a Reply