Docs
Best Practices
Traffic Attribution

Traffic Attribution: Analyze your marketing channels

Web Attribution

When setting up your Mixpanel implementation, one issue of particular interest is tracking users by their original source of traffic. By default, Mixpanel does some of this tracking for you on the web in the form of several default properties.

UTM Properties

Mixpanel's Javascript library tracks all UTM tags by default. This allows you to segment key actions by relevant campaign information using attribution models, so that you can quantify the effectiveness of specific campaigns.

Mixpanel's Javascript library will also track initial_utm_parameters as a profile property, based on the first ever visit. This is helpful as if a user makes a purchase or completes some other important event, it is important to know want to know what acquisition channel brought them to your site originally.

  • utm_source: source where traffic is coming from, including a website or advertiser
  • utm_medium: advertising medium, including email and banner ads
  • utm_campaign: campaign name associated with the traffic
  • utm_content: specific link within an ad that a user clicked
  • utm_term: keywords associated with campaigns
  • utm_id / utm_campaign_id : unique identifier of a campaign
  • utm_source_platform: marketing platform responsible for directing traffic
  • utm_creative_format: type of ad creative, for example: display, native, video, search
  • utm_marketing_tactic: targeting criteria applied to a campaign, for example: remarketing, prospecting
  • Initial UTM: tracks the first time a user reaches any of the above parameters

Last-Touch UTM tracking

By default, Mixpanel's Javascript SDK tracks UTM tags in a first-touch manner by setting the first UTM tags detected for the user as a profile property and super property. This allows you to see what acquisition channel brought your users to your website originally.

It can also be helpful to track UTM tags in a last-touch manner, which would set the UTM tag to the most recent value detected for the user. This allows you to evaluate the effectiveness of more recent acquisition channels on your user's behavior.

The Javascript SDK does not track last-touch UTM tags by default, but you can add the following code in your website to automatically parse the UTM tags from the current URL, and register the tags as last-touch UTM super properties and profile properties.

// Paste the following code below the Mixpanel Javascript Snippet 
// that you add to your site to track events.
 
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() {
 
  // campaign_keywords sets the UTM tags to parse for
  var campaign_keywords = 'utm_source utm_medium utm_campaign utm_content utm_term'.split(' ')
      , kw = ''
      , last_params = {}
      , first_params = {};
 
  var index;
 
  //save UTM tags parsed from the URL as a key-value pair
  //used to set last-touch UTM properties
  for (index = 0; index < campaign_keywords.length; ++index) {
    kw = getQueryParam(document.URL, campaign_keywords[index]);
    if (kw.length) {
      last_params[campaign_keywords[index] + ' [last touch]'] = kw;
    }
  }
 
  //save UTM tags parsed from the URL as a key-value pair
  //used to set first-touch UTM properties
  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;
    }
  }
 
  //set last-touch UTM tags as profile properties
  mixpanel.people.set(last_params);
  //update last-touch UTM super property with the latest value
  mixpanel.register(last_params);
 
  //set first-touch UTM tag as profile property
  //will be skipped if the property is already present
  mixpanel.people.set_once(first_params);
 
}
 
campaignParams();

Initial Referrer and Initial Referring Domain Properties

Mixpanel's Javascript library will track Initial Referrer and Initial Referring Domain and append them as a property to any event that a user completes. These properties are stored in the Mixpanel cookie the first time a user comes to your site and will not change on future site visits as long as the cookie is not cleared.

If a user comes to your site from www.google.com (opens in a new tab) for the first time, for example, then the initial referring domain is equal to www.google.com (opens in a new tab) and the initial referrer is the URL that they first came from. This information will be stored in the Mixpanel cookie and sent with all future events.

Having this information allows you to build reports to see how users from different initial referrers convert through an application.

$direct

An initial referrer is equal to $direct when a user first lands on a site without being referred by another website. The user may have typed the website address directly, clicked a bookmark, clicked a link from an email, or might have security settings in their browser that prevent referrer data from being passed.

Mobile Attribution

Mobile attribution, or tracking campaign sources for app installs/uninstalls/downloads on mobile devices, can be more complex than the web due to the way mobile devices store attribution information.

For Android, Google provides a Play Install Referrer Library (opens in a new tab) so you know where your installations came from. You can use the getInstallReferrer ( ) (opens in a new tab) method to retrieve the referrer URL string, parse it and send that data to Mixpanel as properties in events.

For iOS, users enter the Apple App Store carrying data about where they came from, but the App Store strips that data once the user arrives in the store. Therefore, users who download your application don’t come with any data showing where they were before arriving at the App Store.

In order to track channel attribution on iOS, you'll need to use a mobile attribution tool. You can see a list of the partners we integrate with on our tech partners page (opens in a new tab).

Server-Side Attribution

Unlike web tracking, server-side implementations generally don't have access to global contexts or variables that can provide attribution data. This means these data such as UTM parameters and referrer information need to be extracted manually from the request. Below is an example of how this can be done using Python:

from urllib.parse import urlparse
 
from mixpanel import Mixpanel
 
mp = mixpanel.init("YOUR_TOKEN")
 
def track_to_mp(request, event_name, properties):
 
  # ... handle additional event properties such as $browser, $device, and $os ...
 
  if "Referer" in request.headers:
    properties.update({
      "$referrer": request.headers["Referer"]
      "$referring_domain": urlparse(request.headers["Referer"]).hostname
    })
 
  # assumes query parameters are available as Flask request.args dict
  utm_keys = ["utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term"]
  utm_values = {key: request.args[key] for key in utm_keys if request.args.get(key)}
  properties.update(utm_values)
 
  properties["ip"] = request.remote_addr
  mp.track(request.user_id, event_name, properties)

Was this page useful?