Cross-Domain Tracking in Mixpanel Analytics
Track Users Across Websites With Mixpanel Analytics
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 come on 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 User Identification
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.
The same is the case when the same user lands on a website from a different browser/device. In this case, 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. 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.
Feel free to reach out if you need a Mixpanel expert’s help.
How to track users across website 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's 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
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 how you can 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 URLvar 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 the user with a new distinct id.
Need help with Mixpanel? Let’s talk!