forked from flurry/react-native-flurry-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
449 lines (377 loc) · 14.9 KB
/
index.js
File metadata and controls
449 lines (377 loc) · 14.9 KB
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
import {
DeviceEventEmitter,
NativeAppEventEmitter,
NativeModules,
Platform,
} from 'react-native';
const { ReactNativeFlurry } = NativeModules;
let initFlurryCalled = false;
function priorInit(wrapped) {
console.warn(`Flurry.${arguments.callee.caller.name} method is deprecated, please use Flurry.Builder instead.`);
return function() {
if (initFlurryCalled) {
console.error(`Flurry.${arguments.callee.caller.name} method must be called prior to invoking "init"`);
return;
}
wrapped.apply(this, arguments);
}
}
export default class Flurry {
static Builder = class {
constructor() {
ReactNativeFlurry.initBuilder();
}
build(...apiKeys) {
if (apiKeys.length === 0) {
console.error('Flurry.Builder.build: apiKey(string) is required');
return;
} else if (apiKeys.length === 1) {
if (typeof apiKeys[0] !== 'string') {
console.error('Flurry.Builder.build: apiKey1(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[0]);
} else if (apiKeys.length === 2) {
if (Platform.OS === 'android') {
if (typeof apiKeys[0] !== 'string') {
console.error('Flurry.Builder.build: apiKey1(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[0]);
} else if (Platform.OS === 'ios') {
if (typeof apiKeys[1] !== 'string') {
console.error('Flurry.Builder.build: apiKey2(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[1]);
}
}
}
withCrashReporting(crashReporting = true) {
ReactNativeFlurry.withCrashReporting(crashReporting);
return this;
}
withCrashReporting(crashReporting = true) {
ReactNativeFlurry.withCrashReporting(crashReporting);
return this;
}
withContinueSessionMillis(sessionMillis = 10000) {
if (sessionMillis < 5000) {
console.error('Flurry.Builder.withContinueSessionMillis: the minimum timeout for a session is 5,000 ms.');
}
ReactNativeFlurry.withContinueSessionMillis(sessionMillis);
return this;
}
withIncludeBackgroundSessionsInMetrics(includeBackgroundSessionsInMetrics = true) {
ReactNativeFlurry.withIncludeBackgroundSessionsInMetrics(includeBackgroundSessionsInMetrics);
return this;
}
withLogEnabled(enableLog = true) {
ReactNativeFlurry.withLogEnabled(enableLog);
return this;
}
withLogLevel(logLevel = 5) {
ReactNativeFlurry.withLogLevel(logLevel);
return this;
}
withMessaging(enableMessaging = true) {
ReactNativeFlurry.withMessaging(enableMessaging);
return this;
}
};
/**
* @deprecated Please use Flurry.Builder instead.
*/
static init(...apiKeys) {
console.warn(`Flurry.init method is deprecated, please use Flurry.Builder instead.`);
if (initFlurryCalled) {
console.error('Flurry.init: already called');
return;
}
if (apiKeys.length === 0) {
console.error('Flurry.init: apiKey(string) is required');
return;
} else if (apiKeys.length === 1) {
if (typeof apiKeys[0] !== 'string') {
console.error('Flurry.init: apiKey1(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[0]);
} else if (apiKeys.length === 2) {
if (Platform.OS === 'android') {
if (typeof apiKeys[0] !== 'string') {
console.error('Flurry.init: apiKey1(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[0]);
} else if (Platform.OS === 'ios') {
if (typeof apiKeys[1] !== 'string') {
console.error('Flurry.init: apiKey2(string) is required');
return;
}
ReactNativeFlurry.build(apiKeys[1]);
}
}
initFlurryCalled = true;
}
/**
* @deprecated Please use Flurry.Builder instead.
*/
static withCrashReporting(crashReporting = true) {
priorInit(ReactNativeFlurry.withCrashReporting)(crashReporting);
}
/**
* @deprecated Please use Flurry.Builder instead.
*/
static withContinueSessionMillis(sessionMillis = 10000) {
if (sessionMillis < 5000) {
console.error('Flurry.withContinueSessionMillis: the minimum timeout for a session is 5,000 ms.');
}
priorInit(ReactNativeFlurry.withContinueSessionMillis)(sessionMillis);
}
/**
* @deprecated Please use Flurry.Builder instead.
*/
static withIncludeBackgroundSessionsInMetrics(includeBackgroundSessionsInMetrics = true) {
priorInit(ReactNativeFlurry.withIncludeBackgroundSessionsInMetrics)(includeBackgroundSessionsInMetrics);
}
/**
* @deprecated Please use Flurry.Builder instead.
*/
static withLogEnabled(enableLog = true) {
priorInit(ReactNativeFlurry.withLogEnabled)(enableLog);
}
/**
* @deprecated Please use Flurry.Builder instead.
*/
static withLogLevel(logLevel = 5) {
priorInit(ReactNativeFlurry.withLogLevel)(logLevel);
}
static setAge(age) {
if (typeof age !== 'number' || age <= 0 || age >= 110) {
console.error(`Flurry.setAge: age must be a valid positive number between 0 and 110. Got ${age}`);
return;
}
ReactNativeFlurry.setAge(age);
}
static setGender(gender) {
if (typeof gender !== 'string' || !['m', 'f'].includes(gender)) {
console.error(`Flurry.setGender: gender must be one of ['m', 'f']. Got ${gender}`);
return;
}
ReactNativeFlurry.setGender(gender);
}
static setReportLocation(reportLocation) {
if (typeof reportLocation !== 'boolean') {
console.error(`Flurry.setReportLocation: reportLocation must be one of [true, false]. Got ${reportLocation}`);
return;
}
ReactNativeFlurry.setReportLocation(reportLocation);
}
static setSessionOrigin(originName, deepLink) {
if (typeof originName !== 'string') {
console.error(`Flurry.setSessionOrigin: originName must be string. Got ${originName}`);
return;
}
if (typeof deepLink !== 'string') {
console.error(`Flurry.setSessionOrigin: deepLink must be string. Got ${deepLink}`);
return;
}
ReactNativeFlurry.setSessionOrigin(originName, deepLink);
}
static setUserId(userId) {
if (typeof userId !== 'string') {
console.error(`Flurry.setUserId: userId must be a string. Got ${userId}`);
return;
}
ReactNativeFlurry.setUserId(userId);
}
static setVersionName(versionName = '1.0') {
if (typeof versionName !== 'string') {
console.error(`Flurry.setVersionName: versionName must be a string. Got ${versionName}`);
return;
}
ReactNativeFlurry.setVersionName(versionName);
}
static setIAPReportingEnabled(enableIAP = true) {
if (typeof enableIAP !== 'boolean') {
console.error(`Flurry.setIAPReportingEnabled: enableIAP must be one of [true, false]. Got ${enableIAP}`);
return;
}
ReactNativeFlurry.setIAPReportingEnabled(enableIAP);
}
/**
* There are 2 overloads
* - addOrigin(originName, originVersion)
* - addOrigin(originName, originVersion, originParameters)
*/
static addOrigin(originName, originVersion, originParameters) {
if (typeof originName !== 'string') {
console.error(`Flurry.addOrigin: originName must be string. Got ${originName}`);
return;
}
if (typeof originVersion !== 'string') {
console.error(`Flurry.addOrigin: originVersion must be string. Got ${originVersion}`);
return;
}
if (arguments.length === 2) {
ReactNativeFlurry.addOrigin(originName, originVersion);
} else if (arguments.length === 3) {
ReactNativeFlurry.addOriginParams(originName, originVersion, originParameters);
}
}
static addSessionProperty(name, value) {
if (typeof name !== 'string') {
console.error(`Flurry.addSessionProperty: name must be string. Got ${name}`);
return;
}
if (typeof value !== 'string') {
console.error(`Flurry.addSessionProperty: value must be string. Got ${value}`);
return;
}
ReactNativeFlurry.addSessionProperty(name, value);
}
static getVersions(errorCallback, successCallback) {
if (arguments.length === 0) {
return ReactNativeFlurry.getVersionsPromise();
}
ReactNativeFlurry.getVersions(errorCallback, successCallback);
}
static logBreadcrumb(crashBreadcrumb) {
if (typeof crashBreadcrumb !== 'string') {
console.error(`Flurry.logBreadcrumb: crashBreadcrumb must be a string. Got ${crashBreadcrumb}`);
return;
}
ReactNativeFlurry.logBreadcrumb(crashBreadcrumb);
}
/**
* There are four overloads
* - logEvent(eventId)
* - logEvent(eventId, timed)
* - logEvent(eventId, parameters)
* - logEvent(eventId, parameters, timed)
*/
static logEvent(eventId, parameters, timed) {
if (typeof eventId !== 'string') {
console.error(`Flurry.logEvent: eventId must be a string. Got ${eventId}`);
return;
}
if (arguments.length === 1) {
ReactNativeFlurry.logEvent(eventId);
} else if (arguments.length === 2) {
if (typeof arguments[1] === 'boolean') {
ReactNativeFlurry.logEventTimed(eventId, arguments[1]);
} else if (Object.prototype.toString.call(arguments[1]).includes('Object')) {
ReactNativeFlurry.logEventParams(eventId, arguments[1]);
}
} else if (arguments.length === 3) {
ReactNativeFlurry.logEventParamsTimed(eventId, parameters, timed);
}
}
static logPayment(productName, productId, quantity, price, currency, transactionId, parameters) {
if (typeof productName !== 'string') {
console.error(`Flurry.logPayment: productName must be a string. Got ${productName}`);
return;
}
if (typeof productId !== 'string') {
console.error(`Flurry.logPayment: productId must be a string. Got ${productId}`);
return;
}
if (typeof quantity !== 'number') {
console.error(`Flurry.logPayment: quantity must be a number. Got ${quantity}`);
return;
}
if (typeof price !== 'number') {
console.error(`Flurry.logPayment: price must be a number. Got ${price}`);
return;
}
if (typeof currency !== 'string') {
console.error(`Flurry.logPayment: currency must be a string. Got ${currency}`);
return;
}
if (typeof transactionId !== 'string') {
console.error(`Flurry.logPayment: transactionId must be a string. Got ${transactionId}`);
return;
}
ReactNativeFlurry.logPayment(productName, productId, quantity, price, currency, transactionId, parameters);
}
/**
* There are two overloads
* - endTimedEvent(eventId)
* - endTimedEvent(eventId, parameters)
*/
static endTimedEvent(eventId, parameters) {
if (typeof eventId !== 'string') {
console.error(`Flurry.logEvent: endTimedEvent must be a string. Got ${eventId}`);
return;
}
if (arguments.length === 1) {
ReactNativeFlurry.endTimedEvent(eventId);
} else if (arguments.length === 2) {
ReactNativeFlurry.endTimedEventParams(eventId, parameters);
}
}
/**
* There are two overloads
* - onError(errorId, message, errorClass)
* - onError(errorId, message, errorClass, errorParams)
*/
static onError(errorId, message, errorClass, errorParams) {
if (typeof errorId !== 'string') {
console.error(`Flurry.onError: errorId must be a string. Got ${errorId}`);
return;
}
if (typeof message !== 'string') {
console.error(`Flurry.onError: message must be a string. Got ${message}`);
return;
}
if (typeof errorClass !== 'string') {
console.error(`Flurry.onError: errorClass must be a string. Got ${errorClass}`);
return;
}
if (arguments.length === 3) {
ReactNativeFlurry.onError(errorId, message, errorClass);
} else if (arguments.length === 4) {
ReactNativeFlurry.onErrorParams(errorId, message, errorClass, errorParams);
}
}
static onPageView() {
ReactNativeFlurry.onPageView();
}
static addMessagingListener(callback) {
if (typeof callback !== 'function') {
console.error(`Flurry.addMessagingListener: callback must be a function. Got ${callback}`);
return;
}
var Emitter = (Platform.OS === 'android') ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.addListener('FlurryMessagingEvent', callback);
ReactNativeFlurry.enableMessagingListener(true);
}
static removeMessagingListener(callback) {
if (typeof callback !== 'function') {
console.error(`Flurry.removeMessagingListener: callback must be a function. Got ${callback}`);
return;
}
var Emitter = (Platform.OS === 'android') ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.removeListener('FlurryMessagingEvent', callback);
}
static willHandleMessage(handled) {
ReactNativeFlurry.willHandleMessage(handled);
}
static printMessage(message) {
if (message.hasOwnProperty('Token')) {
console.log('Flurry Messaging Type: ' + message.Type +
'\n Token: ' + message.Token);
return;
}
var data = '';
for (var prop in message.Data) {
data += '\n\t' + prop + ': ' + message.Data[prop];
}
console.log('Flurry Messaging Type: ' + message.Type +
'\n Title: ' + message.Title +
'\n Body: ' + message.Body +
'\n ClickAction: ' + message.ClickAction +
'\n Data:' + data);
}
}