如何在开发时禁用swift-Mixpanel?

时间:2017-09-22 06:41:51

标签: swift mixpanel

在appDelegate上,我曾经在obj-c上这样写,但是在swift中,当app调用Mixpanel.mainInstance()时会在开发时崩溃.track(event:“”)

什么是不在开发模式下发送mixpanel数据的好方法?

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

1 个答案:

答案 0 :(得分:1)

根据您的陈述,我提出以下解决方案:

在Mixpanel中为开发创建一个项目,在方法didFinishLaunchWithOptions中的AppDelegate中你做这样的事情:

#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

通过这种方式,您可以保证开发和测试时的事件不会与生产中的应用程序事件混合

另一种解决方案可能是创建自己的通用方法来发送事件,在其中你验证应用程序是在开发还是生产中,以防止从调用Mixpanel实例发送事件的方法,如下所示:

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@mail.com"
MixpanelOwn.trackEventOwn(sbEventMixpanel:"User entered to ProductsScreen", props:props);

问候!