diff --git a/MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift b/MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift index fea87172..0d4b8365 100644 --- a/MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift +++ b/MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift @@ -538,6 +538,17 @@ class MixpanelDemoTests: MixpanelBaseTests { "clear super properties failed") removeDBfile(testMixpanel.apiToken) } + + func testSettingSuperPropertiesWhenInit() { + let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60, superProperties: ["mp_lib": "flutter"]) + waitForTrackingQueue(testMixpanel) + XCTAssertEqual(testMixpanel.currentSuperProperties()["mp_lib"] as? String, "flutter", + "register super properties in init failed") + testMixpanel.track(event: "e1") + let e = eventQueue(token: testMixpanel.apiToken).last! + let p = e["properties"] as! InternalProperties + XCTAssertNotNil(p["mp_lib"], "flutter") + } func testInvalidPropertiesTrack() { let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60) diff --git a/Sources/Mixpanel.swift b/Sources/Mixpanel.swift index 317f9eee..88123d91 100644 --- a/Sources/Mixpanel.swift +++ b/Sources/Mixpanel.swift @@ -39,12 +39,14 @@ open class Mixpanel { flushInterval: Double = 60, instanceName: String = UUID().uuidString, optOutTrackingByDefault: Bool = false, - trackAutomaticEvents: Bool = true) -> MixpanelInstance { + trackAutomaticEvents: Bool = true, + superProperties: Properties? = nil) -> MixpanelInstance { return MixpanelManager.sharedInstance.initialize(token: apiToken, flushInterval: flushInterval, instanceName: instanceName, optOutTrackingByDefault: optOutTrackingByDefault, - trackAutomaticEvents: trackAutomaticEvents) + trackAutomaticEvents: trackAutomaticEvents, + superProperties: superProperties) } #else /** @@ -143,12 +145,14 @@ class MixpanelManager { flushInterval: Double, instanceName: String, optOutTrackingByDefault: Bool = false, - trackAutomaticEvents: Bool = true) -> MixpanelInstance { + trackAutomaticEvents: Bool = true, + superProperties: Properties? = nil) -> MixpanelInstance { let instance = MixpanelInstance(apiToken: apiToken, flushInterval: flushInterval, name: instanceName, optOutTrackingByDefault: optOutTrackingByDefault, - trackAutomaticEvents: trackAutomaticEvents) + trackAutomaticEvents: trackAutomaticEvents, + superProperties: superProperties) mainInstance = instance readWriteLock.write { instances[instanceName] = instance diff --git a/Sources/MixpanelInstance.swift b/Sources/MixpanelInstance.swift index b7cb3873..b9284e53 100644 --- a/Sources/MixpanelInstance.swift +++ b/Sources/MixpanelInstance.swift @@ -214,7 +214,8 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele #endif // DECIDE #if !os(OSX) && !os(watchOS) - init(apiToken: String?, flushInterval: Double, name: String, optOutTrackingByDefault: Bool = false, trackAutomaticEvents: Bool = true) { + init(apiToken: String?, flushInterval: Double, name: String, optOutTrackingByDefault: Bool = false, + trackAutomaticEvents: Bool = true, superProperties: Properties? = nil) { if let apiToken = apiToken, !apiToken.isEmpty { self.apiToken = apiToken } @@ -273,6 +274,11 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele if optOutTrackingByDefault && (hasOptedOutTracking() || optOutStatus == nil) { optOutTracking() } + + if let superProperties = superProperties { + registerSuperProperties(superProperties) + } + MixpanelPersistence.saveAutomacticEventsEnabledFlag(value: trackAutomaticEvents, fromDecide: false, apiToken: self.apiToken)