mixpanel-js icon indicating copy to clipboard operation
mixpanel-js copied to clipboard

mixpanel.identify() works even when it is turned off.

Open nazarHnatyuk opened this issue 10 months ago • 1 comments

Hello everyone, I encountered such a problem: mixpanel.identify(); works even when shouldEnableMixpanel = false. What could be the problem?

import mixpanel from "mixpanel-browser";

classMixpanelTracker {
  constructor(shouldEnableMixpanel = false) {
    this.shouldEnableMixpanel = shouldEnableMixpanel && !window.location.host.includes('localhost');
}
 
  initialize() {
    if (this.shouldEnableMixpanel) {
      mixpanel.init(MIXPANEL_API_TOKEN, { track_pageview: false });
      mixpanel.identify();
    }
    else {
      console.log(`Mixpanel identifying is disabled or in localhost.`);
    }
  }
 
  logEvent(eventName, eventParams) {
    if (this.shouldEnableMixpanel) {
      try {
        mixpanel.track(eventName, eventParams);
      } catch (error) {
        console.error(error);
      }
    } else {
      console.log(`Mixpanel tracking is disabled or in localhost.`);
    }
  }
 
  logUserProperty(property, value) {
    if (this.shouldEnableMixpanel) {
      try {
        mixpanel.people.set({ [property]: value });
      } catch (error) {
        console.error(error);
      }
    } else {
      console.log(`Mixpanel tracking is disabled or in localhost.`);
      }
    }
}
 
export default MixpanelTracker;

nazarHnatyuk avatar Apr 01 '24 14:04 nazarHnatyuk

mixpanel.identify() is not bound by the class you've written. Hence, to enforce that restriction you need a method in this class that will be called whenever you want to identify. And that will enforce your restriction.

doc-han avatar Apr 18 '24 09:04 doc-han