device id how to generate this for client side Mixpanel and device id for server side in Mixpanel
Hi event, Please stand by as I search the Mixpanel knowledge base for an AI-powered answer. I normally take about 15 seconds or so. In case AI cannot answer your question, feel free to reach out to Mixpanel support at mixpanel.com/get-support. If you have follow-up questions for this thread, please tag me again with your question: @kapa.ai <follow-up question> đź‘‹
Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai
For Mixpanel, the way you generate and manage the device ID depends on whether you are using client-side or server-side tracking: ---
Client-side (Web/Mobile SDKs)
- Automatic Generation: Mixpanel’s client-side SDKs (JavaScript, Android, iOS, etc.) automatically generate a $device_id
for each new user/session. This ID is local to the device and is persisted until you call .reset()
, which will generate a new $device_id
for the session.
- No Manual Generation Needed: You do not need to manually generate or assign the $device_id
when using Mixpanel’s client-side SDKs. The SDK handles this for you and attaches the $device_id
to all events automatically.
- Behavior: If you call .reset()
, the SDK will clear the local storage and generate a new $device_id
for the next session.> “Mixpanel’s SDKs will generate a $device_id
and set that to distinct_id
to associate these events to the same anonymous user.”
“Calling .reset
will clear the local storage (which contains the $user_id
and $device_id
), and generate a new $device_id
for the session.”
Identifying Users (Simplified) – Client-side Identity Management---
Server-side (APIs and Server SDKs)
- Manual Generation Required: Mixpanel’s server-side SDKs and HTTP API do not generate IDs automatically. You are responsible for generating a unique device ID (commonly a UUID) and persisting it (e.g., in a cookie or session).
- How to Generate: The recommended approach is to generate a UUID (universally unique identifier) and store it in a cookie or session variable for each user/session.
- Usage: Set this value as the $device_id
property in your event payloads. If you do not know the user’s identity (anonymous user), only set $device_id
. When the user logs in, include both $device_id
and $user_id
in your events to allow Mixpanel to merge the identities.> “Mixpanel’s server-side SDKs and HTTP API do not generate IDs automatically, meaning your server is responsible for generating IDs and maintaining ID persistence.”
Server-Side Best Practices – Identifying Users> “We recommend generating a UUID and storing that value in a cookie. All common server frameworks provide a simple way to set and retrieve cookies per request.”
Identifying Users (Simplified) – Server-side Identity Management---**Summary Table:**| Environment | Who Generates $device_id
? | How is it Generated? |
|---------------|-----------------------------|-------------------------------------|
| Client-side | Mixpanel SDK | Automatically, per device/session |
| Server-side | You (the developer) | Generate UUID, store in cookie/etc. |If you need code examples or further details, let me know your specific environment (e.g., Python, Node.js, etc.)!
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.