Umeumeume
Umeumeume

Reputation: 1988

How can I disable swift-Mixpanel on development?

On appDelegate, I used to write like this on obj-c, but in swift, it will crash on development when the app call Mixpanel.mainInstance().track(event: "")

What is a good way not to send mixpanel data on development mode?

#if DEBUG
#else
    Mixpanel.initialize(token: Key.Mixpanel.token)
#endif

Upvotes: 0

Views: 567

Answers (1)

in what you state, I propose the following solution:

Create a project in Mixpanel for development and in the AppDelegate in the method didFinishLaunchWithOptions you do something like this:

#if DEBUG
  Mixpanel.initialize(token: Constants.APP.MIXPANEL.DEVELOPMENT_TOKEN)
#else
  Mixpanel.initialize(token: Constants.APP.MIXPANEL.PRODUCTION_TOKEN)
#endif

//Where Constants.APP.MIXPANEL.DEVELOPMENT_TOKEN and Constants.APP.MIXPANEL.PRODUCTION_TOKEN contain your Strings from your tokens of each of the mixpanel projects

With that you guarantee that the events as you develop and test do not mix with the events of the application in production

Another solution could be to create own and generic methods of sending events where within them you validate if the app is in development or production to prevent the methods of sending events from the Mixpanel instance being called, something like this:

class MixpanelOwn {
  static func trackEventOwn(sbEventMixpanel:String, props:Properties?){
     #if !DEBUG
        if let properties = props {
          Mixpanel.mainInstance().track(event:sbEventMixpanel, properties: properties)
        }else{
          Mixpanel.mainInstance().track(event: sbEventMixpanel)
        }       
     #endif
  }
}

//To call it, it would be something like this
//Without props 
MixpanelOwn.trackEventOwn(sbEventMixpanel:"User entered to ProductsScreen", props:nil);
//With props
var props:Properties = Properties();
props["userId"] = 12345;
props["email"] = "[email protected]"
MixpanelOwn.trackEventOwn(sbEventMixpanel:"User entered to ProductsScreen", props:props);

Regards!

Upvotes: 1

Related Questions