-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
349 lines (262 loc) · 11.5 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
// @flow
'use strict'
import { NativeModules } 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
initialized: boolean
constructor(apiToken: ?string) {
this.apiToken = apiToken
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> {
return RNMixpanel.sharedInstanceWithToken(this.apiToken).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)
}
/*
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('getDistinctId')))
}
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)
}
alias(alias: string): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('createAlias'))
return RNMixpanel.createAlias(alias, 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)
}
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)
}
removePushDeviceToken(deviceToken: Object): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('removePushDeviceToken'))
return RNMixpanel.removePushDeviceToken(deviceToken, 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)
}
// 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(): 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(this.apiToken)
}
reset(): Promise<void> {
if (!this.initialized) throw new Error(uninitializedError('reset'))
return RNMixpanel.reset(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): Promise<void> {
const instance = new MixpanelInstance(apiToken)
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)
})
},
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()
},
createAlias(alias: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.alias(alias)
},
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)
},
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(deviceToken: Object) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.removePushDeviceToken(deviceToken)
},
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)
},
addPushDeviceToken(token: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.addPushDeviceToken(token)
},
// android only
setPushRegistrationId(token: string) {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.setPushRegistrationId(token)
},
// android only
clearPushRegistrationId() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.clearPushRegistrationId()
},
reset() {
if (!defaultInstance) throw new Error(NO_INSTANCE_ERROR)
defaultInstance.reset()
}
}