-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
221 lines (195 loc) · 6.98 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
/**
* 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
// --------------------------------------------------
setEventUploadThreshold(threshold) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setEventUploadThreshold(threshold);
} else {
throw new Error('You called Amplitude.setEventUploadThreshold before initializing it. Run new Amplitute(key) first.');
}
}
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.');
}
}
getUserId() {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.getUserId();
} else {
throw new Error('You called Amplitude.getUserId before initializing it. Run new Amplitude(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.');
}
}
setOptOut(optOut) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setOptOut(optOut);
} else {
throw new Error('You called Amplitude.setOptOut 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.');
}
}
getDeviceId() {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.getDeviceId();
} else {
throw new Error('You called Amplitude.getDeviceId before initializing it. Run new Amplitude(key) first.')
}
}
getSessionId() {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.getSessionId();
} else {
throw new Error('You called Amplitude.getSessionId before initializing it. Run new Amplitute(key) first.');
}
}
setDeviceId(deviceId) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setDeviceId(deviceId ? deviceId.toString() : null);
} else {
throw new Error('You called Amplitude.setDeviceId 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.');
}
}
logEventWithTimestamp(name, timestamp, properties = {}) {
if (amplitudeHasInitialized) {
var eventName = this.evPrefix ? this.evPrefix + name : name;
return RNAmplitudeSDK.logEventWithTimestamp(eventName, timestamp, properties);
} 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.');
}
}
logRevenueV2(properties = {}) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.logRevenueV2(properties);
} else {
throw new Error('You called Amplitude.logRevenue before initializing it. Run new Amplitute(key) first.');
}
}
addToUserProperty(property, amount) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.addToUserProperty(property, amount);
} else {
throw new Error('You called Amplitude.addToUserProperty before initializing it. Run new Amplitute(key) first.');
}
}
setUserPropertyOnce(property, value) {
if (amplitudeHasInitialized) {
return RNAmplitudeSDK.setUserPropertyOnce(property, value);
} else {
throw new Error('You called Amplitude.setUserPropertyOnce before initializing it. Run new Amplitute(key) first.');
}
}
appendToUserProperty(property, value) {
if (amplitudeHasInitialized) {
if (typeof value === 'string') {
return RNAmplitudeSDK.appendToUserProperty(property, value);
} else {
throw new Error('Amplitude.appendToUserProperty only accepts string values .');
}
} else {
throw new Error('You called Amplitude.appendToUserProperty before initializing it. Run new Amplitute(key) first.');
}
}
prependToUserProperty(property, value) {
if (amplitudeHasInitialized) {
if (typeof value === 'string') {
return RNAmplitudeSDK.prependToUserProperty(property, value);
} else {
throw new Error('Amplitude.prependToUserProperty only accepts string values .');
}
} else {
throw new Error('You called Amplitude.prependToUserProperty before initializing it. Run new Amplitute(key) first.');
}
}
}
export default Amplitude;