diff --git a/Sources/Decide.swift b/Sources/Decide.swift index 6c699086..8781a62f 100644 --- a/Sources/Decide.swift +++ b/Sources/Decide.swift @@ -29,6 +29,7 @@ class Decide { let semaphore = DispatchSemaphore(value: 0) decideRequest.sendRequest(distinctId: distinctId, token: token) { [weak self] decideResult in guard let self = self else { + semaphore.signal() return } guard let result = decideResult else { diff --git a/Sources/Mixpanel.swift b/Sources/Mixpanel.swift index d5d80e00..377b99ef 100644 --- a/Sources/Mixpanel.swift +++ b/Sources/Mixpanel.swift @@ -46,6 +46,9 @@ open class Mixpanel { useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, serverURL: String? = nil) -> MixpanelInstance { + #if DEBUG + didDebugInit(distinctId: apiToken) + #endif return MixpanelManager.sharedInstance.initialize(token: apiToken, flushInterval: flushInterval, instanceName: ((instanceName != nil) ? instanceName! : apiToken), @@ -86,6 +89,9 @@ open class Mixpanel { useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, serverURL: String? = nil) -> MixpanelInstance { + #if DEBUG + didDebugInit(distinctId: apiToken) + #endif return MixpanelManager.sharedInstance.initialize(token: apiToken, flushInterval: flushInterval, instanceName: ((instanceName != nil) ? instanceName! : apiToken), @@ -141,6 +147,32 @@ open class Mixpanel { open class func removeInstance(name: String) { MixpanelManager.sharedInstance.removeInstance(name: name) } + + private class func didDebugInit(distinctId: String) { + let debugInitCountKey = "MPDebugInitCountKey" + let debugInitCount = UserDefaults.standard.integer(forKey: debugInitCountKey) + 1 + if debugInitCount == 1 { + Network.sendHttpEvent(eventName: "First SDK Debug Launch", apiToken: "metrics-1", distinctId: distinctId) { (_) in } + } + checkForSurvey(distinctId: distinctId, debugInitCount: debugInitCount) + UserDefaults.standard.set(debugInitCount, forKey: debugInitCountKey) + UserDefaults.standard.synchronize() + } + + private class func checkForSurvey(distinctId: String, debugInitCount: Int) { + let surveyShownCountKey = "MPSurveyShownCountKey" + let surveyShownCount = UserDefaults.standard.integer(forKey: surveyShownCountKey) + if (debugInitCount > 10 && surveyShownCount < 1) || (debugInitCount > 20 && surveyShownCount < 2) || (debugInitCount > 30 && surveyShownCount < 3) { + let waveHand = UnicodeScalar(0x1f44b) ?? "*" + let thumbsUp = UnicodeScalar(0x1f44d) ?? "*" + let thumbsDown = UnicodeScalar(0x1f44e) ?? "*" + print(""" + \(waveHand)\(waveHand) Zihe & Jared here, tell us about the Mixpanel developer experience! https://www.mixpanel.com/devnps \(thumbsUp)\(thumbsDown) + """) + UserDefaults.standard.set(surveyShownCount + 1, forKey: surveyShownCountKey) + Network.sendHttpEvent(eventName: "Dev NPS Survey Logged", apiToken: "metrics-1", distinctId: distinctId, properties: ["Survey Shown Count": surveyShownCount, "Debug Launch Count": debugInitCount]) { (_) in } + } + } } class MixpanelManager { diff --git a/Sources/MixpanelInstance.swift b/Sources/MixpanelInstance.swift index 68298851..855dd6cc 100644 --- a/Sources/MixpanelInstance.swift +++ b/Sources/MixpanelInstance.swift @@ -816,7 +816,7 @@ extension MixpanelInstance { let defaultsKey = "trackedKey" if !UserDefaults.standard.bool(forKey: defaultsKey) { trackingQueue.async { [apiToken, defaultsKey] in - Network.trackIntegration(apiToken: apiToken, serverURL: BasePath.DefaultMixpanelAPI) { [defaultsKey] (success) in + Network.sendHttpEvent(eventName: "Integration", apiToken: "85053bf24bba75239b16a601d9387e17", distinctId: apiToken) { [defaultsKey] (success) in if success { UserDefaults.standard.set(true, forKey: defaultsKey) UserDefaults.standard.synchronize() diff --git a/Sources/Network.swift b/Sources/Network.swift index 75928861..ca38d925 100644 --- a/Sources/Network.swift +++ b/Sources/Network.swift @@ -128,13 +128,12 @@ class Network { parse: parse) } - class func trackIntegration(apiToken: String, serverURL: String, completion: @escaping (Bool) -> Void) { - let requestData = JSONHandler.encodeAPIData([["event": "Integration", - "properties": ["token": "85053bf24bba75239b16a601d9387e17", - "mp_lib": "swift", - "version": "3.0", - "distinct_id": apiToken, - "$lib_version": AutomaticProperties.libVersion()]]]) + class func sendHttpEvent(eventName: String, apiToken: String, distinctId: String, properties: Dictionary = [:], completion: @escaping (Bool) -> Void) { + let trackProperties = properties.merging(["token": apiToken, + "mp_lib": "swift", + "distinct_id": distinctId, + "$lib_version": AutomaticProperties.libVersion()]) {(current, _) in current } + let requestData = JSONHandler.encodeAPIData([["event": eventName, "properties": trackProperties]]) let responseParser: (Data) -> Int? = { data in let response = String(data: data, encoding: String.Encoding.utf8) @@ -154,14 +153,14 @@ class Network { headers: ["Accept-Encoding": "gzip"], parse: responseParser) - Network.apiRequest(base: serverURL, + Network.apiRequest(base: BasePath.DefaultMixpanelAPI, resource: resource, failure: { (_, _, _) in - Logger.debug(message: "failed to track integration") + Logger.debug(message: "failed to track \(eventName)") completion(false) }, success: { (_, _) in - Logger.debug(message: "integration tracked") + Logger.debug(message: "\(eventName) tracked") completion(true) } )