forked from davide-scalzo/react-native-mixpanel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
512 lines (386 loc) · 16.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// @flow
'use strict'
import { NativeModules, Platform } from 'react-native'
const { RNMixpanel } = NativeModules
/*
An error that is thrown or promise.rejected when a function is invoked before initialize() has completed.
*/
const NO_INSTANCE_ERROR = 'No mixpanel instance created yet. You must call sharedInstanceWithToken(token) or MixPanelInstance.initialize(token) before anything else and should wait for its promise to fulfill before others calls to avoid any internal native issue.'
const uninitializedError: (string) => Error = (method: string) => new Error(`Mixpanel instance was not initialized yet. Please run initialize() and wait for its promise to resolve before calling ${method}(...)`)
let defaultInstance:?MixpanelInstance = null
/*
A Mixpanel target. Normally there is only one of these. If you want to log to multiple Mixpanel projects, you can create a new instance of this class with a unique name. Then call initiaze and you can start logging.
Most of the functions, like track and alias return a Promise. The functions will be called and normally you shouldn't have to care about awaiting them.
However since React Native makes no guarantees about whether native methods are called in order, if you want to be 100% sure everything will work, like calling identify() before track(), you should await those promises to ensure everything is called properly.
*/
export class MixpanelInstance {
apiToken: ?string
optOutTrackingDefault: boolean
trackCrashes: boolean
automaticPushTracking: boolean
launchOptions: object
initialized: boolean
constructor(apiToken: ?string, optOutTrackingDefault: ?boolean = false, trackCrashes: ?boolean = true, automaticPushTracking: ?boolean = true, launchOptions: ?Object = null) {
this.apiToken = apiToken
this.optOutTrackingDefault = optOutTrackingDefault
this.trackCrashes = trackCrashes
this.automaticPushTracking = automaticPushTracking
this.launchOptions = launchOptions
this.initialized = false
}
/*
Initializes the instance in native land. Returns a promise that resolves when the instance has been created and is ready for use.
*/
initialize(): Promise<void> {
if (Platform.OS === 'ios'){
return RNMixpanel.sharedInstanceWithToken(this.apiToken, this.optOutTrackingDefault, this.trackCrashes, this.automaticPushTracking, this.launchOptions)
.then(() => {
this.initialized = true
})
} else {
return RNMixpanel.sharedInstanceWithToken(this.apiToken, this.optOutTrackingDefault)
.then(() => {
this.initialized = true
})
}
}
/*
Gets the unique identifier for the current user. Returns a promise that resolves with the value.
*/
getDistinctId(): Promise<string> {
if (!this.initialized) {
return Promise.reject(new Error(uninitializedError('getDistinctId')))
}
return RNMixpanel.getDistinctId(this.apiToken)
}
/*
Retrieves current Firebase Cloud Messaging token.
*/
getPushRegistrationId(): Promise<string> {
if (!this.initialized) {
return Promise.reject(new Error(uninitializedError('getPushRegistrationId')))
}
if (!RNMixpanel.getPushRegistrationId) throw new Error('No native implementation for getPushRegistrationId. This is Android only.')
return RNMixpanel.getPushRegistrationId(this.apiToken)
}
/*
Gets the given super property. Returns a promise that resolves to the value.
*/
getSuperProperty(propertyName: string): Promise<mixed> {
if (!this.initialized) {
return Promise.reject(new Error(uninitializedError('getSuperProperty')))
}
return RNMixpanel.getSuperProperty(propertyName, this.apiToken)
}
/*
Logs the event.
*/
track(event: string, properties?: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('track'))
if (properties) {
return RNMixpanel.trackWithProperties(event, properties, this.apiToken)
} else {
return RNMixpanel.track(event, this.apiToken)
}
}
flush(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('flush'))
return RNMixpanel.flush(this.apiToken)
}
disableIpAddressGeolocalization(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('disableIpAddressGeolocalization'))
if (!RNMixpanel.disableIpAddressGeolocalization) throw new Error('No native implementation for disableIpAddressGeolocalization. This is iOS only.')
return RNMixpanel.disableIpAddressGeolocalization(this.apiToken)
}
alias(alias: string, oldDistinctID?: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('createAlias'))
return RNMixpanel.createAlias(alias, oldDistinctID, this.apiToken)
}
identify(userId: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('identify'))
return RNMixpanel.identify(userId, this.apiToken)
}
timeEvent(event: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('timeEvent'))
return RNMixpanel.timeEvent(event, this.apiToken)
}
registerSuperProperties(properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('registerSuperProperties'))
return RNMixpanel.registerSuperProperties(properties, this.apiToken)
}
registerSuperPropertiesOnce(properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('registerSuperPropertiesOnce'))
return RNMixpanel.registerSuperPropertiesOnce(properties, this.apiToken)
}
clearSuperProperties(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('clearSuperProperties'))
return RNMixpanel.clearSuperProperties(this.apiToken)
}
initPushHandling(token: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('initPushHandling'))
return RNMixpanel.initPushHandling(token, this.apiToken)
}
set(properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('set'))
return RNMixpanel.set(properties, this.apiToken)
}
setOnce(properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('setOnce'))
return RNMixpanel.setOnce(properties, this.apiToken)
}
trackCharge(charge: number): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('trackCharge'))
return RNMixpanel.trackCharge(charge, this.apiToken)
}
trackChargeWithProperties(charge: number, properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('trackChargeWithProperties'))
return RNMixpanel.trackChargeWithProperties(charge, properties, this.apiToken)
}
increment(property: string, by: number): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('increment'))
return RNMixpanel.increment(property, by, this.apiToken)
}
union(name: string, properties: any[]): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('union'))
return RNMixpanel.union(name, properties, this.apiToken)
}
append(name: string, properties: any[]): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('append'))
return RNMixpanel.append(name, properties, this.apiToken)
}
removePushDeviceToken(pushDeviceToken: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('removePushDeviceToken'))
return RNMixpanel.removePushDeviceToken(pushDeviceToken, this.apiToken)
}
removeAllPushDeviceTokens(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('removeAllPushDeviceTokens'))
return RNMixpanel.removeAllPushDeviceTokens(this.apiToken)
}
addPushDeviceToken(token: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('addPushDeviceToken'))
return RNMixpanel.addPushDeviceToken(token, this.apiToken)
}
// iOS only
setAppSessionPropertiesIOS(properties: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('setAppSessionPropertiesIOS'))
if (!RNMixpanel.setAppSessionPropertiesIOS) throw new Error('No native implementation for setAppSessionPropertiesIOS. This is iOS only.')
return RNMixpanel.setAppSessionPropertiesIOS(properties)
}
// android only
setPushRegistrationId(token: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('setPushRegistrationId'))
if (!RNMixpanel.setPushRegistrationId) throw new Error('No native implementation for setPushRegistrationId. This is Android only.')
return RNMixpanel.setPushRegistrationId(token, this.apiToken)
}
// android only
clearPushRegistrationId(token?: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('clearPushRegistrationId'))
if (!RNMixpanel.clearPushRegistrationId) throw new Error('No native implementation for setPusclearPushRegistrationIdhRegistrationId. This is Android only.')
return RNMixpanel.clearPushRegistrationId(token, this.apiToken)
}
reset(flushOnReset?: boolean = true, autoGenerateNewUniqueId?: boolean = true): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('reset'))
return RNMixpanel.reset(this.apiToken, flushOnReset, autoGenerateNewUniqueId)
}
showInAppMessageIfAvailable(): Promise<void> {
if (!this.initialized) throw uninitializedError('showNotificationIfAvailable')
if (Platform.OS === "android") {
return RNMixpanel.showNotificationIfAvailable(this.apiToken)
} else {
return RNMixpanel.showNotification(this.apiToken)
}
}
optInTracking(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('optInTracking'))
return RNMixpanel.optInTracking(this.apiToken)
}
optOutTracking(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('optOutTracking'))
return RNMixpanel.optOutTracking(this.apiToken)
}
addGroup(groupKey: string, groupId: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('addGroup'))
return RNMixpanel.addGroup(groupKey, groupId, this.apiToken)
}
setGroup(groupKey: string, groupId: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('setGroup'))
return RNMixpanel.setGroup(groupKey, groupId, this.apiToken)
}
}
/*
This is the original API and can still be used. However some may find it useful to use MixpanelInstance instead, like
```
const mixpanel = new MixpanelInstance(TOKEN)
await mixpanel.initialize()
mixpanel.track('my event')
```
*/
export default {
sharedInstanceWithToken(apiToken: string, optOutTrackingDefault: ?boolean = false, trackCrashes: ?boolean = true, automaticPushTracking: ?boolean = true, launchOptions: ?Object = null): Promise<void> {
const instance = new MixpanelInstance(apiToken, optOutTrackingDefault, trackCrashes, automaticPushTracking, launchOptions)
if (!defaultInstance) defaultInstance = instance
return instance.initialize()
},
/*
Gets the unique instance for a user. If you want to use promises, use the MixpanelInstace class API instead.
*/
getDistinctId(callback: (id: ?string) => void) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.getDistinctId()
.then((id: string) => {
callback(id)
})
.catch((err) => {
console.error('Error in mixpanel getDistinctId', err)
callback(null)
})
},
/*
Retrieves current Firebase Cloud Messaging token.
*/
getPushRegistrationId(callback: (token: ?string) => void) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.getPushRegistrationId()
.then((token: string) => {
callback(token)
})
.catch((err) => {
console.error('Error in mixpanel getPushRegistrationId', err)
callback(null)
})
},
getSuperProperty(propertyName: string, callback: (value: mixed) => void) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.getSuperProperty(propertyName)
.then((value: mixed) => {
callback(value)
})
.catch((err) => {
console.error('Error in mixpanel getSuperProperty', err)
callback(null)
})
},
track(event: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.track(event)
},
trackWithProperties(event: string, properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.track(event, properties)
},
flush() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.flush()
},
disableIpAddressGeolocalization() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.disableIpAddressGeolocalization()
},
createAlias(alias: string, oldDistinctID: ?string = null) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.alias(alias, oldDistinctID)
},
identify(userId: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.identify(userId)
},
timeEvent(event: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.timeEvent(event)
},
registerSuperProperties(properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.registerSuperProperties(properties)
},
registerSuperPropertiesOnce(properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.registerSuperPropertiesOnce(properties)
},
clearSuperProperties() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.clearSuperProperties()
},
initPushHandling(token: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.initPushHandling(token)
},
set(properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.set(properties)
},
setOnce(properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.setOnce(properties)
},
removePushDeviceToken(pushDeviceToken: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.removePushDeviceToken(pushDeviceToken)
},
removeAllPushDeviceTokens() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.removeAllPushDeviceTokens()
},
trackCharge(charge: number) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.trackCharge(charge)
},
trackChargeWithProperties(charge: number, properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.trackChargeWithProperties(charge, properties)
},
increment(property: string, by: number) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.increment(property, by)
},
union(name: string, properties: any[]) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.union(name, properties)
},
append(name: string, properties: any[]) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.append(name, properties)
},
addPushDeviceToken(token: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.addPushDeviceToken(token)
},
// iOS only
setAppSessionPropertiesIOS(properties: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.setAppSessionPropertiesIOS(properties)
},
// android only
setPushRegistrationId(token: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.setPushRegistrationId(token)
},
// android only
clearPushRegistrationId(token?: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.clearPushRegistrationId(token)
},
reset(flushOnReset?: boolean = true, autoGenerateNewUniqueId?: boolean = true) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.reset(flushOnReset, autoGenerateNewUniqueId)
},
showInAppMessageIfAvailable() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.showInAppMessageIfAvailable()
},
optInTracking() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.optInTracking()
},
optOutTracking() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.optOutTracking()
},
addGroup(groupKey: string, groupId: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.addGroup(groupKey, groupId)
},
setGroup(groupKey: string, groupId: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.setGroup(groupKey, groupId)
},
}