forked from SudoPlz/react-native-amplitude-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (100 loc) · 3.45 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
/**
* Stub of AmplitudeSDK for React Native
*
* @providesModule AmplitudeSDK
* @flow
*/
'use strict';
// Libraries
import { NativeModules, Platform } from 'react-native';
// Native Modules
const { RNAmplitudeSDK } = NativeModules;
var amplitudeHasInitialized = false;
class Amplitude {
/**
* Creates a new Amplitude client
*/
constructor(apiKey, trackSessionEvents, eventPrefix) {
if (apiKey && typeof apiKey === 'string') {
if (RNAmplitudeSDK) {
if (eventPrefix) {
this.evPrefix = eventPrefix;
}
RNAmplitudeSDK.initialize(apiKey, trackSessionEvents === true);
amplitudeHasInitialized = true;
} else {
throw new Error('RNAmplitudeSDK: No native client found. Is RNAmplitudeSDK installed in your native code project?');
}
} else {
throw new Error('RNAmplitudeSDK: A client must be constructed with an API key. i.e new Amplitude(key);');
}
}
// --------------------------------------------------
// Identify
// --------------------------------------------------
setUserId(userId) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setUserId(userId ? userId.toString() : null);
} else {
throw new Error('You called Amplitude.setUserId before initializing it. Run new Amplitute(key) first.');
}
}
setUserProperties(properties) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setUserProperties(properties);
} else {
throw new Error('You called Amplitude.setUserProperties before initializing it. Run new Amplitute(key) first.');
}
}
clearUserProperties() {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.clearUserProperties();
} else {
throw new Error('You called Amplitude.clearUserProperties before initializing it. Run new Amplitute(key) first.');
}
}
regenerateDeviceId() {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.regenerateDeviceId();
} else {
throw new Error('You called Amplitude.regenerateDeviceId before initializing it. Run new Amplitute(key) first.');
}
}
setLogEventPrefix(prefix) {
if (amplitudeHasInitialized) {
this.evPrefix = prefix;
} else {
throw new Error('You called Amplitude.setLogEventPrefix before initializing it. Run new Amplitute(key) first.');
}
}
// --------------------------------------------------
// Track
// --------------------------------------------------
logEvent(name, properties) {
if (amplitudeHasInitialized) {
var eventName = this.evPrefix ? this.evPrefix + name : name;
if (properties) {
return RNAmplitudeSDK.logEventWithProps(eventName, properties);
} else {
return RNAmplitudeSDK.logEvent(eventName);
}
} else {
throw new Error('You called Amplitude.logEvent before initializing it. Run new Amplitute(key) first.');
}
}
// --------------------------------------------------
// Revenue
// --------------------------------------------------
logRevenue(productIdentifier, quantity, amount, receipt) {
if (amplitudeHasInitialized) {
if (Platform.OS === 'ios') {
return RNAmplitudeSDK.logRevenue(productIdentifier, quantity, amount, receipt);
} else {
return RNAmplitudeSDK.logRevenue(productIdentifier, quantity, amount);
}
} else {
throw new Error('You called Amplitude.logRevenue before initializing it. Run new Amplitute(key) first.');
}
}
}
export default Amplitude;