How do I track sources of web traffic?

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 some special super properties.

Initial Referrer and Initial Referring Domain Properties

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

If a user comes to your site from www.google.com for the first time, then this information will be stored in the Mixpanel cookie and sent with all future events. This allows you to build a Funnel to see how users from different initial referrers convert through sign up or view Segmentation to see which domains drive the most purchases on your site.

UTM Properties

Mixpanel's Javascript library will also track first touch UTM tags by default. This allows you to segment key actions by relevant campaign information so that you can quantify the effectiveness of specific campaigns.

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.

Last touch attribution allows you to see how a user found your site most recently. If you customize your tracking code 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 a 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.

Custom Source Tracking

Sometimes the above tracking sources do not suit a customer's needs, or you wish to customize the way you track users from different sources. In this way you can control how you measure and evaluate the traffic coming from all of your crucial sources.

What you can do is set up some code to track this information when users first come to your site. You can set up custom handling for different domains of special interest in order to track these sources directly.

function source(){
  if (document.referrer.search('https?://(.*)google.([^/?]*)') === 0) {
    return 'Google';
  } else if (document.referrer.search('https?://(.*)bing.([^/?]*)') === 0) {
    return 'Bing';
  } else if (document.referrer.search('https?://(.*)yahoo.([^/?]*)') === 0) {
    return 'Yahoo';
  } else if (document.referrer.search('https?://(.*)facebook.([^/?]*)') === 0) {
    return 'Facebook';
  } else if (document.referrer.search('https?://(.*)twitter.([^/?]*)') === 0) {
    return 'Twitter';
  } else {
    return 'Other';
  }
}

Using the above code you can track some specific sources directly -- you can add to this list or subtract from it to focus in on the custom sources of traffic you are most interested in measuring. Once you return the source you will want to register it as a super property if you want to send it with all events and/or as a people property to track the source of each of your users.

mixpanel.register_once({"Source": source()});
mixpanel.people.set_once({"Source": source()});

This will make it so you can use your custom source information along with any of the user's future events on your site or use it to target them with a specific message.

Mobile Attribution

Mobile attribution, or tracking campaign source for app installs on iOS/Android, can be more complex than the web due to the way mobile devices store attribution information - learn more about how to track your campaigns on mobile.