Cross-Domain Tracking in Mixpanel Analytics

image by PublicDomainArchive, source Pixabay

Mixpanel is a great analytics tool. It helps you track all user's activities across the browser and devices. Unlike Universal Google Analytics, where it will count two users if the same user comes from two different browsers or devices, Mixpanel, with its identity management, will count it as one user.

So Mixpanel does a great job at tracking users. But what about tracking users across two different websites? Google Analytics can easily track that. And with GTM’s help, it is only a few settings away. But what about Mixpanel? Can it do that?

Yes definitely. With Mixpanel it is very much possible to track users across domains as well. I mean common if Mixpanel is good at tracking users across devices how big of a deal it would be to track users across websites. Well, in Mixpanel’s case it is not that simple.

But before we dive into how to track users across websites. It is crucial to understand why a new user would be created in Mixpanel if we don’t implement cross-domain tracking.

Mixpanel associates all user events across an id called distinct id. Distinct id is similar to client id in GA. So when a user lands on a website Mixpanel creates a random id and stores all the events for that user against this random it. Even after the user is identified this distinct id persists for that browser. You can easily see the distinct id of any user in the user profile in Mixpanel.

Distinct Id in Mixpanel people profile, by Muffaddal

The same is the case when the same user lands on a website from a different browser/device. In this case as well, the Mixpanel library will create a new random id and track all the events against this new random id until the user logs in. At which Mixpanel will be able to map the current anonymous activities of this user to his old activities within Mixpanel.

Mixpanel stores distinct id in the cookie of the browser, or local storage depending upon the library settings. And cookies are only accessible from the domain on which it was set. So one domain cookie is not accessible by another domain cookie.

Now let’s suppose a user on one domain with a distinct id goes to another domain of the same website. Since on this new domain, there is no distinct id present in the cookie Mixpanel would generate a new distinct id and will start to track all the following events against that new distinct id. As a result, we have two users instead of one. Unless the user is identified on this new domain Mixpanel will count two users.

Source Giphy

How to track users across websites domains?

So the problem is different distinct ids get generated on a different domain. And somehow we need the same distinct id on both. How to do it?

We definitely can’t create the same distinct id ourselves. If we could it wouldn’t be a random id.

The only option is to pass the distinct id from one domain to another domain. And use that to send events to.

So we need to do two things
1. Send distinct Id from the first website to the second website.
2. Use the first website distinct id on the second website.

Simple.

Let’s dive into the details.

1- Send distinct Id from the first website to the second website.

We have to fetch the distinct id from Mixpanel SDK on the first domain and append it to all the page URLs which send the user to the second domain.

You can get the distinct id using mixpanel.get_distinct_id()

So suppose the second website URL looks something like this

www.second-domain.com/page1/

After appending the fetched distinct id to the URL it will become like

www.second-domain.com/page1/?distinct_id=dd4cv7a47–991c-457ha-bf97–2b75c76b3aaa1

When the user clicks this updated URL it will open the second website but the page will also have a distinct_id parameter in the url.

And of course, appending an id with a URL requires a lot of code to be triggered when a page is viewed. Well, nobody said it would be easy!

I won’t go into detail about the code here to keep the article short and precise but feel free to reach out to me if you need any assistance on that front.

Here is a really good article on how to append values to the URL of the page

2. Use the first website distinct id on the second website.

Now we have the distinct id of the first website in our second website’s navigation bar.

here is the code to get the parameter from URL

var url = new URL(location.href);
var distinctId = url.searchParams.get(“distinct_id”);

Finally, identify the user with the id

mixpanel.identify(distinctId);

and we are good to go!

Now the activities of user on the second website will be sent to the same user profile in Mixpanel as the first website.

Summary

To recap in order to do cross-domain tracking in Mixpanel we need to

1- Grab the distinct id on the first website.

2- Append it to the URL of the second website as the query parameter

3- Read distinct id from the URL on the second website

4-Identify user with new distinct id.

Read More on Mixpanel Analytics

--

--

--

Muffaddal has around 5 years of experience working with web analytics and user behavior data. https://www.linkedin.com/in/muffaddal-qutbuddin/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build an interactive Slack app with message menus

Sectioned RecyclerView Android Kotlin

The quirks of programming music theory

New Market Pair: Jasmy (JASMY)

Dependency Injection in Java

Deque Systems: Preety Kumar’s Big Idea That Might Change The World

Hadoop days are over..long live big data

7 Essential Python Skills for Research

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
muffaddal qutbuddin

muffaddal qutbuddin

Muffaddal has around 5 years of experience working with web analytics and user behavior data. https://www.linkedin.com/in/muffaddal-qutbuddin/

More from Medium

Digital analyst is dead. Long live digital analyst!

How to make your Google Tag Manager secure? GTM Tip!

7 steps to build a dashboard from scratch and not die trying

Game, Set, and Vis!