Skip to content

Commit

Permalink
Merge pull request #498 from mixpanel/add-super-properties-init
Browse files Browse the repository at this point in the history
add superProperties param to init
  • Loading branch information
zihejia authored Jan 11, 2022
2 parents af0a075 + b85503a commit 40e037d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
11 changes: 11 additions & 0 deletions MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 40e037d

Please sign in to comment.