Mixpanel Community Icon

How to get Device Id from mixpanel

·
·

How to get Device Id from mixpanel

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    Hi George K., 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

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    To get the Device ID from Mixpanel, the approach depends on whether you're using client-side or server-side tracking. Here's how you can obtain the Device ID:For client-side tracking (using Web or Mobile SDKs):Mixpanel automatically generates a $device_id for each user's device. This ID is used as the distinct_id for anonymous users until they are identified. You don't need to manually retrieve or set this ID as it's handled automatically by the SDK.For server-side tracking:Since server-side libraries don't automatically generate Device IDs, you need to manage them yourself. Here's the recommended approach:1. Generate a unique identifier (e.g., a UUID) for each user session or device. 2. Set this identifier as the $device_id property in your events.Here's an example of how to include the $device_id in your events for server-side tracking:```import uuiddef track_to_mp(request, event_name, properties):

    session_id = request.cookies.get('SESSION_ID') or str(uuid.uuid4())

    properties["$device_id"] = session_id

    if request.user.is_authenticated(): properties["$user_id"] = request.user.username

    mp.track("", event_name, properties)```This approach is described in the Identifying Users (Simplified) documentation.Remember, for server-side tracking, you're responsible for generating and managing these IDs. The $device_id should persist for the duration of a user's session or across sessions on the same device.

    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.