Cross Domain Tracking in Mixpanel

Understand the Concept + Step-by-Step Implementation (with and without GTM)

When your product or marketing experience spans more than one domain, like www.mysite1.com and www.mysite2.com, you need cross-domain tracking to keep the user journey intact. Without it, tools like Mixpanel may treat a single user as multiple users just because they moved between domains.

Let’s break down:

  • What cross-domain tracking means in Mixpanel.
  • Why it matters.
  • How to implement it using Google Tag Manager or directly in code.

What is Cross-Domain Tracking?

In Mixpanel, every user is assigned a distinct_id. This allows Mixpanel to identify each individual and tie all their activity together. This works fine as long as there is one single domain, as the distinct_id created by Mixpanel uses the domain as one of the ingredients for creating the distinct_id.

However, when a user navigates from one domain to another (say from www.mysite1.com to www.mysite2.com), a new distinct_id is created by Mixpanel on the second domain.  So the same person appears as two different people on two different domains.

Note: This concept is not unique to Mixpanel. Other tools, such as Amplitude, GA4, and  Piwik PRO, all have similar limitations.

Cross-domain tracking is a way to ensure that Mixpanel (and other tools) assigns the same distinct_id or sees the user as one person event when they go from one site to another.  This is generally achieved by passing the distinct_id from one site to another when the user clicks to go from one site to another

Cross-Domain High-Level Strategy

  1. Identify the user (either with their user ID or Mixpanel’s distinct_id) on the site (domain) they visit first.
  2. Pass that ID to the next domain, typically using a URL parameter.  (Note: This process is commonly known as "Link Decoration")
  3. Read the ID on the new domain, and re-identify the user to stitch the journey.

Option 1: Direct Implementation in Code

Let’s say someone clicks “Start Trial” on your marketing site and lands on your product domain.

On Domain A (Marketing Site)

// Identify user (email, user_id, etc.)
mixpanel.identify("user_123");

// Optional: alias anonymous ID to known ID
mixpanel.alias("user_123");

// Then append the ID to the URL that takes the use to the other domain.
const ctaButton = document.querySelector('#cta');
ctaButton.href = `https://app.mysite.com/?distinct_id=user_123`;

On Domain B (Product App)

const urlParams = new URLSearchParams(window.location.search);
const idFromUrl = urlParams.get('distinct_id');

if (idFromUrl) {
  mixpanel.identify(idFromUrl);
}
Note: This has to be done on both the domains, if they link to each other. 
Also, if you have lots of cross linking links across your sites then you things get little tircky. Reach out to use and will be able to help you find an efficient way to handle it.

Option 2: Cross-Domain Tracking via Google Tag Manager (GTM)

If you're using GTM on both domains, here’s how to do it without editing much code.

Step 1: On Domain A (Sending Domain)

  • Create a GTM Variable: First Party Cookie – lets call it, _mpcexpl_distinct_id
  • Create a URL Variable: distinct_id – use query key distinct_id
  • Use a Custom JS Variable to grab the ID:
function() {
  return window.mixpanel && mixpanel.get_distinct_id 
    ? mixpanel.get_distinct_id() 
    : undefined;
}

Then use a GTM tag or auto-event variable to rewrite outbound links with the distinct_id added.

Step 2: On Domain B (Receiving Domain)

Create a custom HTML tag in GTM to run on all pages:

<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('distinct_id');

if (id && window.mixpanel && mixpanel.identify) {
  mixpanel.identify(id);
}
</script>

Optionally, use GTM to strip the distinct_id from the URL after processing.

Final Checks

  • Use Mixpanel’s Live View to confirm events from both domains appear under the same user ID.
  • Test in a private/incognito session.
  • Ensure events (e.g., CTA clicks, signups) are firing on both domains.

TL;DR

Step Without GTM With GTM
Identify user mixpanel.identify() Use Custom JS Variable
Pass ID to other domain Append to URL manually Modify links using GTM
Re-identify Read from URL + call identify() Use custom HTML tag in GTM

Bonus Tip

If your domains are subdomains (like www. and app.), you may be able to unify cookies using the cookie_domain config when initializing Mixpanel:

mixpanel.init("YOUR_TOKEN", {
  cookie_domain: ".mysite.com"
});

But if the domains are completely different (mysite.com and myapp.io,  you must use the full ID-passing method above.

Need Help Setting It Up?

Cross-domain tracking can be tricky, especially if you’re dealing with multiple environments, staging sites, or authentication flows.
If you’d like help with setup, auditing, or tracking strategies,
reach out to Optizent.
We specialize in analytics setups that drive better business insights.

Leave a Reply

Your email address will not be published. Required fields are marked *