I apologize for the confusion in my previous response. You're right to be concerned about a new $device_id being generated on every refresh, as this shouldn't be happening with the default Mixpanel configuration. Let's investigate this issue further.
According to the Identifying Users (Simplified) documentation:
"Mixpanel's SDKs will generate a $device_id and set that to distinct_id to associate these events to the same anonymous user. This ID will persist on all events tracked by that user on that device, until you call .identify() or .reset()."
The $device_id should persist across page refreshes unless .reset() is being called or there's an issue with the persistence storage.
Here are a few things to check:
1. Persistence: Ensure that the persistence is working correctly. You're using 'localStorage' for persistence, which is correct. Check if localStorage is available and functioning in your browser.
2. Persistence Name: You've set persistence_name: 'session'. This might be causing issues. Try removing this line or changing it to a more permanent name like 'mixpanel'.
3. Browser Settings: Check if your browser is set to clear localStorage on exit or if you're using any privacy extensions that might be clearing it.
4. Check for .reset() calls: Ensure that mixpanel.reset() isn't being called unintentionally somewhere in your code.
Here's a modified version of your code that might help diagnose the issue:
```
try {
mixpanel.init(window.MIXPANEL_TOKEN, {
debug: true,
track_pageview: 'full-url',
persistence: 'localStorage',
loaded(mixpanel) {
console.log('Mixpanel loaded');
console.log('Distinct ID:', mixpanel.get_distinct_id());
console.log('Device ID:', mixpanel.get_property('$device_id'));
},
});
} catch (error) {
console.error('Mixpanel initialization error:', error);
}
export const Mixpanel = mixpanel;
LBKSThis code will log the `distinct_id` and `$device_id` to the console when Mixpanel loads. If these values are changing on every refresh, there's likely an issue with persistence or an unintended reset occurring.
If the problem persists after these changes, you may need to contact Mixpanel support for further assistance, as this behavior is not expected and may indicate a more complex issue with your specific setup or environment.
Remember, you shouldn't need to manually set or manage the `$device_id` in most cases. Mixpanel should handle this automatically for you.