Mixpanel Community Icon

Mixpanel Initialization Issues: Anonymous ID Format and Identify Call

·
·

(function ($, Drupal, drupalSettings, cookies) { 'use strict'; Drupal.tabClicked = false; Drupal.behaviors.mixpanelInit = { attach: function (context, settings) { // Check if Mixpanel has been initialized if (!Drupal.behaviors.mixpanelInit.initialized) { // Mixpanel token var token = drupalSettings.mixpanel?.mp_token; mixpanel.init(token, {debug: true, persistence: 'localStorage'}); // Identify user logged in or anonymous. if (drupalSettings.user.uid) { mixpanel.identify(drupalSettings.mixpanel.userData.user_id); } else { const mixpanelAnnonId = getCookie('mixpanel_annon_id'); mixpanelAnnonId ? mixpanel.identify(mixpanelAnnonId) : mixpanel.identify(drupalSettings.mixpanel.userData?.device_id) } // Set a flag to indicate that Mixpanel has been initialized Drupal.behaviors.mixpanelInit.initialized = true; } why we are getting 255467 format of distinct id for guest users as anonymous id on cleint side and also identify call is not getting triggered?

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

    Hi Mansi g., 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
    ·
    ·

    Based on the code you've shared, I can see a few issues that might explain why you're getting a numeric format for distinct_id (like "255467") for guest users and why the identify call might not be triggering properly.

    Issues with your Mixpanel implementation:

    1. Anonymous ID handling: Your code is trying to use a cookie called 'mixpanel_annon_id' for anonymous users, but if that cookie doesn't exist, it's falling back to drupalSettings.mixpanel.userData?.device_id. This might be returning a numeric ID instead of a proper UUID format that Mixpanel typically uses for anonymous users.2. Identify call timing: The identify call might not be getting triggered because it's inside a conditional that checks if Mixpanel has been initialized, and this flag might be preventing subsequent calls.

    Recommended fixes:

    For anonymous users, you should let Mixpanel generate the device ID automatically rather than trying to set it manually. According to Mixpanel's documentation on ID management: > When using our Web or Mobile SDKs, Mixpanel will automatically generate a $device_id that's local to that user's device, and set that value as the distinct_id for the event. This ID will persist on all events tracked by that user on that device, until you call identify() or reset(). Identifying Users (Simplified)For proper ID management, follow these best practices: 1. For logged-in users, call identify() with their user ID: LBKSif (drupalSettings.user.uid) { mixpanel.identify(drupalSettings.mixpanel.userData.user_id); }LBKS2. For anonymous users, you don't need to call identify() at all - let Mixpanel handle the device ID generation: LBKS// No need to call identify for anonymous usersLBKS3. When a user logs out, call reset(): LBKSmixpanel.reset();LBKSThis approach follows Mixpanel's best practices for client-side identity management.Also, check your Mixpanel configuration to ensure you're using the correct ID merge system (Original or Simplified) as this affects how identification works.

    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.