DataActs

How to track traffic sources in Mixpanel as Google analytics does?

traffic sources

Google analytics has it’s unique way to track traffic sources and attribute your user data to them. Here you can see what I am talking about:

GA Sources

Mixpanel by default can only track utm parameters as they have mentioned in their documentation here – https://help.mixpanel.com/hc/en-us/articles/115004561786-Track-UTM-Tags

This means that with utm tracking you can track your Google ads, Facebook ads campaigns etc. in Mixpanel.

 

But what about other traffic sources like Direct, Organic and Referral where there are no utm parameters in the URL?

Here is the little hack for this:

Julius Fedorovicius has a nice blog post about how to track initial traffic source here –https://www.analyticsmania.com/post/track-the-initial-traffic-source-of-the-visitor-gtm/   In this blog post he talks about how you can track the initial traffic source and check some other sources that are bringing traffic to your site instead of just relying on GA’s last non-direct click attribution model. The code mentioned in his blog post can be implemented using Google tag manager or directly into the site (if there’s a pro javascript developer on your side). That code will set a cookie called initialTrafficSource into the web browser of the user having values of utm source, medium and campaign etc from the URL. No worries, if there is no utm parameters in the URL then it will automatically detect Direct, Referral and Organic traffic and will set the cookie’s value accordingly. First the developer’s job is to understand the format of the cookie values. The format of the initialTrafficSource cookie formatted as follows:

 

utmcsr=example.com|utmcmd=affl-link|utmccn=foo|utmcct=bar|utmctr=biz

  • utmcsr is campaign source
  • utmcmd is campaign medium
  • utmccn is campaign name
  • utmcct is campaign content
  • utmctr is campaign term

 

Now if the user will come from any of your utm tagged URL then it will be tracked automatically by Mixpanel as mentioned above. And when the user will come from the non utm tagged URL like organically or direct then his source value will be captured and will be saved in the initialTrafficSource cookie in his web browser.

 

The developer’s job is the make a javascript code which will do this:

  1. Check if there are UTM parameters in the URL or not. If there are UTM parameters in the URM of the first page the user has visited then do nothing.
  2. If there are no UTM parameters in the URL, then write a code which will read utm parameter values from the cookie initialTrafficSource and will create utm parameters in the URL.
  3. If UTM parameter values are added to the URL then they will be automatically picked by the Mixpanel.

 

Note: Developer has to make sure that the Mixpanel tracking code runs after his code so that when Mixpanel code runs, it will have UTM parameters added to the URL. Or the developer can pass UTM parameter values from one page to another so that if UTM parameters are not tracked on one page then they will be tracked on another page. Here is the article explaining that – https://www.analyticsmania.com/post/transfer-utm-parameters-google-tag-manager/

 

Join the Founder’s Squad

Founder stories, advice and case studies

By Ranjan Kashyap

I am a seasoned Data Analyst and AI Engineer with deep expertise in leveraging sophisticated analytics and AI to drive strategic decisions. My technical acumen includes GA4, GTM, Mixpanel, and Amplitude implementations, along with robust data warehousing using BigQuery and Snowflake. I specialize in transforming complex datasets into actionable insights and optimizing business processes through advanced BI tools and CDP technologies. My approach helps businesses harness the full potential of their data, enhancing efficiency and promoting scalable growth.

Leave a comment