@@ -47,7 +47,11 @@ open class Mixpanel {
4747 superProperties: Properties ? = nil ,
4848 serverURL: String ? = nil ) -> MixpanelInstance {
4949 #if DEBUG
50- didDebugInit ( distinctId: apiToken, properties: superProperties ?? [ : ] )
50+ didDebugInit (
51+ distinctId: apiToken,
52+ libName: superProperties? . get ( key: " mp_lib " , defaultValue: nil ) ,
53+ libVersion: superProperties? . get ( key: " $lib_version " , defaultValue: nil )
54+ )
5155 #endif
5256 return MixpanelManager . sharedInstance. initialize ( token: apiToken,
5357 flushInterval: flushInterval,
@@ -90,7 +94,11 @@ open class Mixpanel {
9094 superProperties: Properties ? = nil ,
9195 serverURL: String ? = nil ) -> MixpanelInstance {
9296 #if DEBUG
93- didDebugInit ( distinctId: apiToken, properties: superProperties ?? [ : ] )
97+ didDebugInit (
98+ distinctId: apiToken,
99+ libName: superProperties? . get ( key: " mp_lib " , defaultValue: nil ) ,
100+ libVersion: superProperties? . get ( key: " $lib_version " , defaultValue: nil )
101+ )
94102 #endif
95103 return MixpanelManager . sharedInstance. initialize ( token: apiToken,
96104 flushInterval: flushInterval,
@@ -148,21 +156,27 @@ open class Mixpanel {
148156 MixpanelManager . sharedInstance. removeInstance ( name: name)
149157 }
150158
151- private class func didDebugInit( distinctId: String , properties: Properties = [ : ] ) {
152- let debugInitCountKey = " MPDebugInitCountKey "
153- let debugInitCount = UserDefaults . standard. integer ( forKey: debugInitCountKey) + 1
154- var debugProperties : Properties = properties
155- debugProperties += [ " Debug Launch Count " : debugInitCount]
156- Network . sendHttpEvent ( eventName: " SDK Debug Launch " , apiToken: " metrics-1 " , distinctId: distinctId, properties: debugProperties)
157- checkForSurvey ( distinctId: distinctId, debugInitCount: debugInitCount, properties: properties)
158- UserDefaults . standard. set ( debugInitCount, forKey: debugInitCountKey)
159- UserDefaults . standard. synchronize ( )
159+ private class func didDebugInit( distinctId: String , libName: String ? , libVersion: String ? ) {
160+ if distinctId. count == 32 {
161+ let debugInitCount = UserDefaults . standard. integer ( forKey: InternalKeys . mpDebugInitCountKey) + 1
162+ var properties : Properties = [ " Debug Launch Count " : debugInitCount]
163+ if let libName = libName {
164+ properties [ " mp_lib " ] = libName
165+ }
166+ if let libVersion = libVersion {
167+ properties [ " $lib_version " ] = libVersion
168+ }
169+ Network . sendHttpEvent ( eventName: " SDK Debug Launch " , apiToken: " metrics-1 " , distinctId: distinctId, properties: properties) { ( _) in }
170+ checkForSurvey ( distinctId: distinctId, properties: properties)
171+ checkIfImplemented ( distinctId: distinctId, properties: properties)
172+ UserDefaults . standard. set ( debugInitCount, forKey: InternalKeys . mpDebugInitCountKey)
173+ UserDefaults . standard. synchronize ( )
174+ }
160175 }
161176
162- private class func checkForSurvey( distinctId: String , debugInitCount: Int , properties: Properties = [ : ] ) {
163- let surveyShownCountKey = " MPSurveyShownCountKey "
164- var surveyShownCount = UserDefaults . standard. integer ( forKey: surveyShownCountKey)
165- if ( debugInitCount == 10 || debugInitCount == 20 || debugInitCount == 30 ) {
177+ private class func checkForSurvey( distinctId: String , properties: Properties ) {
178+ let surveyShownDate = UserDefaults . standard. object ( forKey: InternalKeys . mpSurveyShownDateKey) as? Date ?? Date . distantPast
179+ if ( surveyShownDate. timeIntervalSinceNow < - 86400 ) {
166180 let waveHand = UnicodeScalar ( 0x1f44b ) ?? " * "
167181 let thumbsUp = UnicodeScalar ( 0x1f44d ) ?? " * "
168182 let thumbsDown = UnicodeScalar ( 0x1f44e ) ?? " * "
@@ -171,12 +185,40 @@ open class Mixpanel {
171185 Hi, Zihe & Jared here, please give feedback or tell us about the Mixpanel developer experience!
172186 open -> https://www.mixpanel.com/devnps \( thumbsUp) \( thumbsDown)
173187 """ )
174- print ( Array ( repeating: " \( waveHand) " , count: 10 ) . joined ( separator: " " ) )
175- surveyShownCount += 1
176- UserDefaults . standard. set ( surveyShownCount, forKey: surveyShownCountKey)
177- var debugProperties : Properties = properties
178- debugProperties += [ " Survey Shown Count " : surveyShownCount]
179- Network . sendHttpEvent ( eventName: " Dev NPS Survey Logged " , apiToken: " metrics-1 " , distinctId: distinctId, properties: debugProperties)
188+ UserDefaults . standard. set ( Date ( ) , forKey: InternalKeys . mpSurveyShownDateKey)
189+ let surveyShownCount = UserDefaults . standard. integer ( forKey: InternalKeys . mpSurveyShownCountKey) + 1
190+ UserDefaults . standard. set ( surveyShownCount, forKey: InternalKeys . mpSurveyShownCountKey)
191+ let trackProps = properties. merging ( [ " Survey Shown Count " : surveyShownCount] ) { ( _, new) in new}
192+ Network . sendHttpEvent ( eventName: " Dev NPS Survey Logged " , apiToken: " metrics-1 " , distinctId: distinctId, properties: trackProps) { ( _) in }
193+ }
194+ }
195+
196+ private class func checkIfImplemented( distinctId: String , properties: Properties ) {
197+ let hasImplemented : Bool = UserDefaults . standard. bool ( forKey: InternalKeys . mpDebugImplementedKey)
198+ if !hasImplemented {
199+ var completed = 0
200+ let hasTracked : Bool = UserDefaults . standard. bool ( forKey: InternalKeys . mpDebugTrackedKey)
201+ completed += hasTracked ? 1 : 0
202+ let hasIdentified : Bool = UserDefaults . standard. bool ( forKey: InternalKeys . mpDebugIdentifiedKey)
203+ completed += hasIdentified ? 1 : 0
204+ let hasAliased : Bool = UserDefaults . standard. bool ( forKey: InternalKeys . mpDebugAliasedKey)
205+ completed += hasAliased ? 1 : 0
206+ let hasUsedPeople : Bool = UserDefaults . standard. bool ( forKey: InternalKeys . mpDebugUsedPeopleKey)
207+ completed += hasUsedPeople ? 1 : 0
208+ if ( completed >= 3 ) {
209+ let trackProps = properties. merging ( [
210+ " Tracked " : hasTracked,
211+ " Identified " : hasIdentified,
212+ " Aliased " : hasAliased,
213+ " Used People " : hasUsedPeople,
214+ ] ) { ( _, new) in new}
215+ Network . sendHttpEvent (
216+ eventName: " SDK Implemented " ,
217+ apiToken: " metrics-1 " ,
218+ distinctId: distinctId,
219+ properties: trackProps) { ( _) in }
220+ UserDefaults . standard. set ( true , forKey: InternalKeys . mpDebugImplementedKey)
221+ }
180222 }
181223 }
182224}
0 commit comments