Tracking internal WordPress events with Google Analytics

Recently I have been trying to use more of the features of Google Analytics beyond the basic stats. In particular, I wanted to keep track of when I publish a new blog post so that I can look for any changes in traffic.

Now, for a tiny site like mine there’s not going a huge rush on the web to see what pearls of wisdom (or ignorance) I have unleashed on the world, nevertheless I thought it would be interesting and could certainly be useful to my clients who have more traffic to analyse.

Annotations – a feature in search of an API

Screenshot of an annotation on Google AnalyticsMy first thought was to use annotations to keep track of things. Annotations allow you to add a note to a certain point on your timeline to record when external events happened that might have had an impact on the data. For example it could be a new post, new code deployment or a change in hosting setup.

Seems perfect right? One problem: there is not external API for adding annotations. There is a feature request currently open for it but, given that it goes back to 2010, it seems unlikely that Google consider it a priority for future developments.

So, what next? Events to the rescue!

Events are commonly used for interactions that occur on a site, but which do not trigger a page view and therefore don’t send the tracking beacon. A good example is the opening of a modal overlay which contains some content or goal that you want to track. By triggering an event using the analytics JavaScrip code, you can record what happened and include it in your reports.

But what if we want to record an event that doesn’t include any direct user interaction with the site – for instance, publishing a post? Well it turns out we can use the rather grand sounding Measurement Protocol. To quote Google:

The Google Analytics Measurement Protocol allows developers to make HTTP requests to send raw user interaction data directly to Google Analytics servers.

How does it work? essentially, we make a POST request with the event information and a bit of configuration and Analytics will record the event as if we had clicked on something on the site.

curl --data "v=1&tid=UA-XXXXXXX-Y&cid=123-456&t=event&ec=WordPress&ea=Publish%20Post&el=The%20Post%20Title" https://ssl.google-analytics.com/collect

A full reference for the parameters can be found here, but essentially we set the UA string (tid), a reference to the user (cid) and then the details for the event.

Using it in WordPress

If we put all of these things together, we can tell WordPress to trigger an Analytics event when we publish a post. All we need to do is hook into the publish_post action and send a remote request using wp_remote_post and we have automatic tracking of our events.

add_action( 'publish_post', 'dbisso_trigger_ga_post_event', 10, 1 );

function dbisso_trigger_ga_post_event( $post_id ) {
    $title = get_the_title( (int) $post_id );

    $data = array(
        'v' => 1,
        'tid' => 'UA-XXXXXXXX-Y', // Replace with a real UA string
        'cid' => get_current_user_id(),
        't'   => 'event',
        'ec'  => 'WordPress',
        'ea'  => 'Publish Post',
        'el'  => $title,
    );

    wp_remote_post(
        'https://ssl.google-analytics.com/collect',
        array( 'body' => $data )
    );
}

To make things easier, I have rolled a small plugin that does the job of triggering the request at the right time with the correct parameters. I’ve called it Google Analytics Internal. Why the generic name? Well there are numerous other internal WordPress events that you might want to track in addition to publishing posts, so I left the door open to extending the plugin in the future.

Screenshot of event report in Google Analytics

Anyway, if you think this technique might be useful just download the plugin from your dashboard or from the WordPress plugin directory and give it a try.

The source code is available on GitHub so any issues or pull requests can be posted there.

Leave a Reply