I am using Simplified ID Merge in my project. My main domain is https://shiftgroup.io/. When I visit the site, a device ID is generated. At this point, the user is not authenticated. Users navigate from the main domain to the subdomain https://connect.shiftgroup.io/ in the same tab, where they get authenticated. However, during this navigation, the device ID changes, causing events not to merge. What can be done in this scenario?
To address your issue with Simplified ID Merge across domains, you can implement the following solution: 1. On your main domain (https://shiftgroup.io/), when the device ID is generated, store it in a cookie that is accessible across both domains.2. When the user navigates to the subdomain (https://connect.shiftgroup.io/) and gets authenticated, retrieve the original device ID from the cookie.3. Use this original device ID as the $device_id
when sending events from the subdomain, along with the newly acquired $user_id
.By including both the original $device_id
and the new $user_id
in events from the subdomain, you'll trigger the identity merge, linking the pre-authentication events with the post-authentication events.Here's a more detailed explanation of the process: 1. On the main domain, after generating the device ID, set it as a cookie accessible to both domains: LBKSdocument.cookie = "mp_device_id=<generated_device_id> ; domain=.[shiftgroup.io](http://shiftgroup.io); path=/; secure; samesite=none";LBKS
2. On the subdomain, after authentication, retrieve the device ID from the cookie and use it when tracking events: LBKSconst getCookie = (name) => { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); };LBKSconst deviceId = getCookie('mp_device_id'); const userId = '<authenticated_user_id> ';LBKSmixpanel.track('Event Name', { '$device_id': deviceId, '$user_id': userId });LBKS
This approach should solve your issue by ensuring that the same device ID is used across both domains, allowing Mixpanel to correctly merge the pre-authentication and post-authentication events.For more information on implementing Simplified ID Merge, you can refer to the Identifying Users (Simplified) documentation.
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
Let me check the knowledge base for you again. One moment! đź“š
To store the device ID in a cookie using JavaScript, you can follow this approach: ```function setDeviceIdCookie(deviceId) { const expirationDate = new Date(); expirationDate.setFullYear(expirationDate.getFullYear() + 1); // Set cookie to expire in 1 year
document.cookie = mp_device_id=${deviceId}; expires=${expirationDate.toUTCString()}; domain=.[shiftgroup.io](http://shiftgroup.io); path=/; secure; samesite=none
;
}// Usage
const deviceId = mixpanel.get_distinct_id(); // Get the device ID from Mixpanel
setDeviceIdCookie(deviceId);LBKSLBKSThis code creates a function `setDeviceIdCookie` that sets a cookie named `mp_device_id` with the provided device ID. The cookie is set to expire in one year, is accessible across all subdomains of [shiftgroup.io](http://shiftgroup.io), and is marked as secure and SameSite=None to allow cross-site access.LBKSWhen using this approach, keep in mind the following best practices from Mixpanel's documentation: LBKS1. Use a consistent naming convention for your cookies [Docs ](https://docs.mixpanel.com/docs/data-structure/events-and-properties#best-practices) Data-structure > Events: Capture behaviors and actions > Best Practices> .LBKS2. Be aware of Mixpanel's cookie behavior across subdomains. By default, Mixpanel cookies work across subdomains [Docs ](https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#mixpanel-cookie) Tracking-methods > Sdks > Mixpanel SDKs: Javascript > Mixpanel Cookie> .LBKS3. Consider using local storage instead of cookies for more reliable persistence, if cross-subdomain tracking is not required: LBKSLBKS
mixpanel.set_config({ persistence: "localStorage" });```Docs Quickstart > Install Mixpanel > FAQ> Remember to retrieve and use this stored device ID when tracking events on your subdomain, as explained in the previous answer, to ensure proper event merging across domains.
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
Let me check the knowledge base for you again. One moment! đź“š
To get the device ID from the distinct ID in Mixpanel, you need to understand how Mixpanel handles these identifiers in the Simplified ID Merge system. Here's what you need to know: 1. In the Simplified ID Merge system, the distinct_id
is automatically set by Mixpanel based on the presence of $device_id
and $user_id
properties in your events.2. If an event contains only a $device_id
, Mixpanel will set the distinct_id
to $device:<device_id>
.3. If an event contains a $user_id
, Mixpanel will set the distinct_id
to the value of $user_id
.Given this information, to get the device ID from the distinct ID: 1. If the distinct_id
starts with $device:
, you can extract the device ID by removing the $device:
prefix.2. If the distinct_id
doesn't start with $device:
, it's likely a user ID, and you cannot directly derive the device ID from it.Here's a JavaScript example of how you might implement this: LBKSfunction getDeviceIdFromDistinctId(distinctId) { if (distinctId.startsWith('$device:')) { return distinctId.substring(8); // Remove '$device:' prefix } return null; // This distinct_id is likely a user ID, not a device ID }LBKS// Usage const distinctId = mixpanel.get_distinct_id(); const deviceId = getDeviceIdFromDistinctId(distinctId);LBKSif (deviceId) { console.log('Device ID:', deviceId); } else { console.log('This distinct_id is not a device ID'); }LBKS
It's important to note that this method will only work for anonymous users or events that don't have a $user_id
set. Once a user is identified (i.e., a $user_id
is set), the distinct_id
will no longer contain the device ID information.For more detailed information about how Mixpanel handles identifiers in the Simplified ID Merge system, you can refer to the Identifying Users (Simplified) documentation.Remember that the best practice is to consistently use $device_id
and $user_id
properties in your events rather than relying on extracting information from the distinct_id
. This ensures more reliable tracking and identity management in Mixpanel.
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.