Steve Taylor photo

Google Analytics Measurement Protocol plugin for WordPress

Recently I had a request from a client to trigger some Google Analytics events for things that were happening on the server. This sort of thing can be tracked indirectly through client-side JavaScript analytics code, with, for example, “success” pages where a user is redirected after the server-side event. However, it seemed to make more sense to look into proper server-side analytics for WordPress sites.

I found a few WordPress / PHP approaches, but I quickly learned that (1) Google have recently standardized this kind of thing within what they call the Measurement Protocol, and (2) the Measurement Protocol is only available as part of their new Universal Analytics.

So, the first step was to upgrade the site from Classic Analytics (which runs through the ga.js script) to Universal Analytics (which runs through the analytics.js script). There’s a few good tutorials around for managing the upgrade process, and of course the official docs are worth having to hand. Basically, it’s pretty painless if you have no custom stuff running, but you need to consider the process carefully if you do. The syntax for triggering events and pageviews via JS has changed. It’s certainly an improvement, more standardized, but you’ll need to rewrite your code.

Once you’re with UA, you can go ahead and try out the Measurement Protocol for server-side analytics. I found this article by Stu Miller¬†invaluable for getting going.

Now, I’ve standardized on using Yoast’s Google Analytics for WordPress plugin on my sites, which is generally excellent. However, it has yet to be updated to use UA. At the time of writing, this post¬†contains a 6-week-old comment from Joost de Valk explaining why this hasn’t happened yet. I’ll keep my eye on this, but for now I decided to create my own plugin that (1) does the basics to include the JS tracking code, UA-stylee, and (2) contains functionality for easily using the Measurement Protocol.

So, here’s the Google Analytics Measurement Protocol plugin on GitHub. Documentation is very basic, but between the source,¬†Stu Miller’s piece, and the official dev guide, you’ll get going much quicker than otherwise.

Once the plugin is activated, you need to go the settings page and add your Google ID (the code beginning with UA). It’s up to you if you use this plugin for inserting the JS tracking code. If you do, you can enter a WP capability, and any logged-in user with this capability won’t be tracked.

Very basic usage is shown in the readme—check the build_hit function for details of how to trigger something.

It works well so far. If you find a bug, please report it on GitHub. If you can see any good improvements that need making, please fork and contribute! I’ll be looking for simple and widely useful developments—I’m sure that either the Yoast plugin or another big player will catch up soon and provide a much more comprehensive solution. For now, hopefully this little plugin is useful.