Community Tip: Last Touch UTM Tags - Mixpanel
Blog Post

Community Tip: Last Touch UTM Tags

Last edited: Sep 6, 2021

This Community Tip describes how to tie users’ most recent UTM tags (aka last touch attribution) to their actions. We will detail why it’s useful, provide code for developers in JavaScript, and show the power in Mixpanel’s reporting.

Why track last touch UTM tags?

Last touch attribution allows you to see how a user found your site most recently. You can utilize last touch UTM properties to measure the effectiveness of various marketing campaigns through Mixpanel’s Segmentation report. You could also leverage Mixpanel’s Funnel report to determine if specific campaigns impacted conversions. If a user eventually ends up making a purchase or completing some other event of consequence, you can use last touch UTM tags to determine what acquisition channel brought them to the site most recently.

First touch UTM tags are automatically tracked by Mixpanel’s JavaScript Library and stored in the Mixpanel cookie so that they can be referenced on subsequent sessions. First touch attribution allows you to see how a user originally found your site, even for events that occur after the first visit. If a user eventually ends up making a purchase or completing some other event of consequence, we want to know what acquisition channel brought them to your site originally, even if it’s been some time since they first arrived.

Collecting Last Touch UTMs

The following code will automatically parse and register last touch UTM tags as super-properties. Simply paste the following code below the Mixpanel snippet and the code will take care of the rest.

function getQueryParam(url, param) {
// Expects a raw URL
param = param.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regexS = "[\?&]" + param + "=([^&#]*)",
    regex = new RegExp( regexS ),
    results = regex.exec(url);
if (results === null || (results && typeof(results[1]) !== 'string' && results[1].length)) {
  return '';
  } else {
  return decodeURIComponent(results[1]).replace(/\W/gi, ' ');
  }
};


function campaignParams() {
var campaign_keywords = 'utm_source utm_medium utm_campaign utm_content utm_term'.split(' ')
    , kw = ''
    , params = {}
    , first_params = {};

var index;
for (index = 0; index < campaign_keywords.length; ++index) {
  kw = getQueryParam(document.URL, campaign_keywords[index]);
  if (kw.length) {
    params[campaign_keywords[index] + ' [last touch]'] = kw;
  }
}
for (index = 0; index < campaign_keywords.length; ++index) {
  kw = getQueryParam(document.URL, campaign_keywords[index]);
  if (kw.length) {
    first_params[campaign_keywords[index] + ' [first touch]'] = kw;
  }
}

mixpanel.people.set(params);
mixpanel.people.set_once(first_params);
mixpanel.register(params);
}



campaignParams();

You can remove the .people.set() calls if you don’t plan on storing user profiles. mixpanel.register(params) is the function that captures these last touch UTM parameters and registers them as Super Properties to be tracked with all future events.

Tracking UTM parameters as properties of user’s profiles

One key behavior to understand about Mixpanel is that Super Properties are only attached to events. The code above will allow you to filter and segment events by UTM parameters, however your People Profiles will not contain attribution data unless you specifically set your UTM parameters as People Properties using mixpanel.people.set() and mixpanel.identify() in tandem.

To create a user profile, simply insert the following call in the body of the HTML page collecting these UTM Campaign Parameters:

mixpanel.identify()

In combination with the code above, this call will create a user profile containing both first and last touch UTM parameters for every user that loads the page.

This is especially valuable for tracking lifetime revenue using Mixpanel’s Revenue report. You’ll want to store first and last touch UTM parameters as People properties so that you will be able to segment users’ Lifetime Revenue by both the original campaign they came from, as well as the most recent campaign they interacted with.

Since the core power of People Profiles is the ability to send Notifications, and our users tend to not send notifications based on UTM parameters, so it may not be worth your time to change your code in order to capture this data if you’re not using Revenue.

Have any questions about UTM tags, attribution, or life time value calculations? Reach out to support@mixpanel.com to speak to someone smart, quickly.

Get the latest from Mixpanel
This field is required.