Tracking UTM parameters with Koko Analytics Pro

Urchin Tracking Module (UTM) parameters are a collection of commonly used URL parameters used by marketers to track the effectiveness of online marketing campaigns across traffic sources and publishing media.

You can use the custom events feature in Koko Analytics Pro to keep track of these UTM parameters. In this article we’ll walk you through the process step by step.

Creating the custom events in your dashboard

First you have to let Koko Analytics know about the events you would like to track.

To do so, go to the Settings page of your Koko Analytics dashboard and locate the Events section. Use the form under Add new event type to create the following event types:

  • UTM Source
  • UTM Medium
  • UTM Campaign

Once done, the page should look something like this:

Screenshot of the events section on the Koko Analytics settings page.

Option 1: Add JavaScript code to track UTM parameters

Now all that is left is to add some JavaScript code to your site that inspects the URL for any UTM parameters and sends them to the event types we just created.

We’ve created a sample code snippet for you that does just that.

// This code snippets monitors the URL for UTM parameters like "utm_source=WordPress.org" and sends it to a Koko Analytics custom event
// In order for this to work, you need to have the custom event created in your Koko Analytics dashboard settings.

window.addEventListener('load', function() {
  let map = {
    'utm_source': 'UTM Source',
    'utm_medium': 'UTM Medium',
    'utm_campaign': 'UTM Campaign',
  };

  let queryParams = new URLSearchParams(window.location.search);
  let hashParams = new URLSearchParams(window.location.hash.substring(1));
  for (let [p, eventName] of Object.entries(map)) {
    let value = queryParams.get(p) ?? hashParams.get(p);
    if (value) {
      window.koko_analytics.trackEvent(eventName, value);
    }
  }
});Code language: JavaScript (javascript)

This code will inspect the page URL for either utm_source, utm_medium or utm_campaign and if present, will trigger an event of the corresponding type.

You can add this code snippet anywhere on your site. If you’re unsure about how to proceed, there are several plugins that can help you here.

Option 2: Install plugin to track UTM parameters

Alternatively, we have prepared a sample plugin for you to use. You can find it here.

Download or copy the contents of this file and save it in your /wp-content/plugins/ directory. Then go to WP Admin > Plugins and activate the “Koko Analytics: Track UTM Parameters” plugin.

Was this article helpful? Yes / No