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
22 changes: 22 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
checkForSurvey(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
checkForSurvey(distinctId: apiToken)
#endif
return MixpanelManager.sharedInstance.initialize(token: apiToken,
flushInterval: flushInterval,
instanceName: ((instanceName != nil) ? instanceName! : apiToken),
Expand Down Expand Up @@ -141,6 +147,22 @@ open class Mixpanel {
open class func removeInstance(name: String) {
MixpanelManager.sharedInstance.removeInstance(name: name)
}

private class func checkForSurvey(distinctId: String) {
let initCount = UserDefaults.standard.integer(forKey: "MPInitCount")
let surveyShownCount = UserDefaults.standard.integer(forKey: "MPSurveyShownCount")
if (initCount > 10 && surveyShownCount < 1) || (initCount > 20 && surveyShownCount < 2) || (initCount > 30 && surveyShownCount < 3) {
print("""
.---------------------------------------------------------------------------------.
| How do you feel about the Mixpanel dev experience? https://3x32.short.gy/devnps |
'---------------------------------------------------------------------------------'
""")
UserDefaults.standard.set(surveyShownCount + 1, forKey: "MPSurveyShownCount")
Network.trackEvent(eventName: "Dev NPS Survey Logged", apiToken: "metrics-1", distinctId: distinctId) { (_) in }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to add the property surveyShownCount as well

}
UserDefaults.standard.set(initCount + 1, forKey: "MPInitCount")
UserDefaults.standard.synchronize()
}
}

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.trackEvent(eventName: "Integration", apiToken: "85053bf24bba75239b16a601d9387e17", distinctId: apiToken) { [defaultsKey] (success) in
if success {
UserDefaults.standard.set(true, forKey: defaultsKey)
UserDefaults.standard.synchronize()
Expand Down
14 changes: 7 additions & 7 deletions Sources/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +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",
class func trackEvent(eventName: String, apiToken: String, distinctId: String, completion: @escaping (Bool) -> Void) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better rename it or put some comments to avoid people using it by mistake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might not be an issue for SPM but could be an issue if they include the source code.

let requestData = JSONHandler.encodeAPIData([["event": eventName,
"properties": ["token": apiToken,
"mp_lib": "swift",
"version": "3.0",
"distinct_id": apiToken,
"distinct_id": distinctId,
"$lib_version": AutomaticProperties.libVersion()]]])

let responseParser: (Data) -> Int? = { data in
Expand All @@ -154,14 +154,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