Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dev NPS Survey Log & semaphore.signal() in Decide #537

Merged
merged 13 commits into from
Apr 21, 2022
1 change: 1 addition & 0 deletions Sources/Decide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 9 additions & 10 deletions Sources/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> = [:], 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)
Expand All @@ -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)
}
)
Expand Down