mixpanel-react-native 2.4.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,39 +1,39 @@
1
1
  "use strict";
2
2
 
3
- import { Platform, NativeModules } from "react-native";
3
+ import {Platform, NativeModules} from "react-native";
4
4
  import packageJson from "./package.json";
5
- const { MixpanelReactNative } = NativeModules;
5
+ const {MixpanelReactNative} = NativeModules;
6
6
 
7
7
  if (!MixpanelReactNative) {
8
- throw new Error(`mixpanel-react-native: MixpanelReactNative is null.
8
+ throw new Error(`mixpanel-react-native: MixpanelReactNative is null.
9
9
  Please follow the guide on the Github repository: https://github.com/mixpanel/mixpanel-react-native.`);
10
10
  }
11
11
 
12
12
  const DevicePlatform = {
13
- Unknown: "Unknown",
14
- Android: "android",
15
- iOS: "ios"
16
- }
13
+ Unknown: "Unknown",
14
+ Android: "android",
15
+ iOS: "ios",
16
+ };
17
17
 
18
18
  const ERROR_MESSAGE = {
19
- INVALID_OBJECT: " is not a valid json object",
20
- INVALID_STRING: " is not a valid string",
21
- REQUIRED_DOUBLE: " is not a valid number"
22
- }
19
+ INVALID_OBJECT: " is not a valid json object",
20
+ INVALID_STRING: " is not a valid string",
21
+ REQUIRED_DOUBLE: " is not a valid number",
22
+ };
23
23
 
24
24
  const PARAMS = {
25
- TOKEN: "token",
26
- DISTINCT_ID: "distinctId",
27
- ALIAS: "alias",
28
- EVENT_NAME: "eventName",
29
- GROUP_KEY: "groupKey",
30
- PROPERTIES: "properties",
31
- PROPERTY_NAME: "propertyName",
32
- PROP: "prop",
33
- NAME: "name",
34
- CHARGE: "charge",
35
- PROPERTY_VALUE: "property value"
36
- }
25
+ TOKEN: "token",
26
+ DISTINCT_ID: "distinctId",
27
+ ALIAS: "alias",
28
+ EVENT_NAME: "eventName",
29
+ GROUP_KEY: "groupKey",
30
+ PROPERTIES: "properties",
31
+ PROPERTY_NAME: "propertyName",
32
+ PROP: "prop",
33
+ NAME: "name",
34
+ CHARGE: "charge",
35
+ PROPERTY_VALUE: "property value",
36
+ };
37
37
 
38
38
  const DEFAULT_OPT_OUT = false;
39
39
 
@@ -41,493 +41,528 @@ const DEFAULT_OPT_OUT = false;
41
41
  * The primary class for integrating Mixpanel with your app.
42
42
  */
43
43
  export class Mixpanel {
44
-
45
- constructor(token, trackAutomaticEvents) {
46
- if (!StringHelper.isValid(token)) {
47
- StringHelper.raiseError(PARAMS.TOKEN);
48
- }
49
- if (trackAutomaticEvents == null) {
50
- throw new Error(`trackAutomaticEvents is undefined`);
51
- }
52
- this.token = token;
53
- this.trackAutomaticEvents = trackAutomaticEvents;
54
- this.people = new People(this.token);
55
- }
56
-
57
- /**
58
- * Initializes Mixpanel
59
- *
60
- * @param {boolean} optOutTrackingDefault Optional Whether or not Mixpanel can start tracking by default. See optOutTracking()
61
- * @param {object} superProperties Optional A Map containing the key value pairs of the super properties to register
62
- * @param {string} serverURL Optional Set the base URL used for Mixpanel API requests. See setServerURL()
63
- *
64
- */
65
- async init(optOutTrackingDefault = DEFAULT_OPT_OUT, superProperties = {}, serverURL = "https://api.mixpanel.com") {
66
- let metadata = Helper.getMetaData();
67
- await MixpanelReactNative.initialize(this.token, this.trackAutomaticEvents, optOutTrackingDefault, {...metadata, ...superProperties}, serverURL);
68
- }
69
-
70
- /**
71
- * @deprecated since version 1.3.0. To initialize Mixpanel, please use the instance method `init` instead. See the example below:
72
- *
73
- * <pre><code>
74
- * const trackAutomaticEvents = true;
75
- * const mixpanel = new Mixpanel('your project token', trackAutomaticEvents);
76
- * mixpanel.init();
77
- * </code></pre>
78
- *
79
- * Initializes Mixpanel and return an instance of Mixpanel the given project token.
80
- *
81
- * @param {string} token your project token.
82
- * @param {boolean} trackAutomaticEvents Whether or not to automatically track common mobile events
83
- * @param {boolean} Optional Whether or not Mixpanel can start tracking by default. See optOutTracking()
84
- *
85
- */
86
- static async init(token, trackAutomaticEvents, optOutTrackingDefault = DEFAULT_OPT_OUT) {
87
- let metadata = Helper.getMetaData();
88
- await MixpanelReactNative.initialize(token, trackAutomaticEvents, optOutTrackingDefault, metadata, "https://api.mixpanel.com");
89
- return new Mixpanel(token, trackAutomaticEvents);
90
- }
91
-
92
- /**
93
- * Set the base URL used for Mixpanel API requests.
94
- * Useful if you need to proxy Mixpanel requests. Defaults to https://api.mixpanel.com.
95
- * To route data to Mixpanel's EU servers, set to https://api-eu.mixpanel.com
96
- *
97
- * @param {string} serverURL the base URL used for Mixpanel API requests
98
- *
99
- */
100
- setServerURL(serverURL) {
101
- MixpanelReactNative.setServerURL(this.token, serverURL);
102
- }
103
-
104
- /**
105
- * This allows enabling or disabling of all Mixpanel logs at run time.
106
- * All logging is disabled by default. Usually, this is only required if
107
- * you are running into issues with the SDK that you want to debug
108
- *
109
- * @param {boolean} loggingEnabled whether to enable logging
110
- *
111
- */
112
- setLoggingEnabled(loggingEnabled) {
113
- MixpanelReactNative.setLoggingEnabled(this.token, loggingEnabled);
114
- }
115
-
116
- /**
117
- * This allows enabling or disabling whether or not Mixpanel flushes events
118
- * when the app enters the background on iOS. This is set to true by default.
119
- *
120
- * @param {boolean} flushOnBackground whether to enable logging
121
- *
122
- */
123
- setFlushOnBackground(flushOnBackground) {
124
- if (Platform.OS === 'ios') {
125
- MixpanelReactNative.setFlushOnBackground(this.token, flushOnBackground);
126
- } else {
127
- console.warn('Mixpanel setFlushOnBackground was called and ignored because this method only works on iOS.')
128
- }
129
- }
130
-
131
- /**
132
- * This controls whether to automatically send the client IP Address as part of event tracking.
133
- * With an IP address, geo-location is possible down to neighborhoods within a city,
134
- * although the Mixpanel Dashboard will just show you city level location specificity.
135
- *
136
- * @param {boolean} useIpAddressForGeolocation whether to automatically send the client IP Address.
137
- * Defaults to true.
138
- *
139
- */
140
- setUseIpAddressForGeolocation(useIpAddressForGeolocation) {
141
- MixpanelReactNative.setUseIpAddressForGeolocation(this.token, useIpAddressForGeolocation);
142
- }
143
-
144
- /**
145
- * Set the number of events sent in a single network request to the Mixpanel server.
146
- * By configuring this value, you can optimize network usage and manage the frequency of communication between the client and the server. The maximum size is 50; any value over 50 will default to 50.
147
- *
148
- * @param {integer} flushBatchSize whether to automatically send the client IP Address.
149
- * Defaults to true.
150
- *
151
- */
152
- setFlushBatchSize(flushBatchSize) {
153
- MixpanelReactNative.setFlushBatchSize(this.token, flushBatchSize);
154
- }
155
-
156
-
157
- /**
158
- * Will return true if the user has opted out from tracking.
159
- *
160
- * @return {Promise<boolean>} true if user has opted out from tracking. Defaults to false.
161
- */
162
- hasOptedOutTracking() {
163
- return MixpanelReactNative.hasOptedOutTracking(this.token);
164
- }
165
-
166
- /**
167
- * Use this method to opt-in an already opted-out user from tracking. People updates and track
168
- * calls will be sent to Mixpanel after using this method.
169
- * This method will internally track an opt-in event to your project.
170
- *
171
- */
172
- optInTracking() {
173
- MixpanelReactNative.optInTracking(this.token);
174
- }
175
-
176
- /**
177
- * Use this method to opt-out a user from tracking. Events and people updates that haven't been
178
- * flushed yet will be deleted. Use flush() before calling this method if you want
179
- * to send all the queues to Mixpanel before.
180
- *
181
- * This method will also remove any user-related information from the device.
182
- */
183
- optOutTracking() {
184
- MixpanelReactNative.optOutTracking(this.token);
185
- }
186
-
187
- /**
188
- * Associate all future calls to track() with the user identified by
189
- * the given distinct id.
190
- *
191
- * <p>Calls to track() made before corresponding calls to identify
192
- * will use an anonymous locally generated distinct id, which means it is best to call identify
193
- * early to ensure that your Mixpanel funnels and retention analytics can continue to track the
194
- * user throughout their lifetime. We recommend calling identify when the user authenticates.
195
- *
196
- * <p>Once identify is called, the local distinct id persists across restarts of
197
- * your application.
198
- *
199
- * @param {string} distinctId a string uniquely identifying this user. Events sent to
200
- * Mixpanel using the same disinct_id will be considered associated with the
201
- * same visitor/customer for retention and funnel reporting, so be sure that the given
202
- * value is globally unique for each individual user you intend to track.
203
- * @returns {Promise} A promise that resolves when the identify is successful.
204
- * It does not return any value.
205
- *
206
- */
207
- identify(distinctId) {
208
- return new Promise((resolve, reject) => {
209
- if (!StringHelper.isValid(distinctId)) {
210
- StringHelper.raiseError(PARAMS.DISTINCT_ID);
211
- reject(new Error('Invalid distinctId'));
212
- }
213
- MixpanelReactNative.identify(this.token, distinctId)
214
- .then(() => {
215
- resolve();
216
- })
217
- .catch(err => {
218
- reject(err);
219
- });
44
+ constructor(token, trackAutomaticEvents) {
45
+ if (!StringHelper.isValid(token)) {
46
+ StringHelper.raiseError(PARAMS.TOKEN);
47
+ }
48
+ if (trackAutomaticEvents == null) {
49
+ throw new Error(`trackAutomaticEvents is undefined`);
50
+ }
51
+ this.token = token;
52
+ this.trackAutomaticEvents = trackAutomaticEvents;
53
+ this.people = new People(this.token);
54
+ }
55
+
56
+ /**
57
+ * Initializes Mixpanel
58
+ *
59
+ * @param {boolean} optOutTrackingDefault Optional Whether or not Mixpanel can start tracking by default. See optOutTracking()
60
+ * @param {object} superProperties Optional A Map containing the key value pairs of the super properties to register
61
+ * @param {string} serverURL Optional Set the base URL used for Mixpanel API requests. See setServerURL()
62
+ *
63
+ */
64
+ async init(
65
+ optOutTrackingDefault = DEFAULT_OPT_OUT,
66
+ superProperties = {},
67
+ serverURL = "https://api.mixpanel.com"
68
+ ) {
69
+ await MixpanelReactNative.initialize(
70
+ this.token,
71
+ this.trackAutomaticEvents,
72
+ optOutTrackingDefault,
73
+ {...Helper.getMetaData(), ...superProperties},
74
+ serverURL
75
+ );
76
+ }
77
+
78
+ /**
79
+ * @deprecated since version 1.3.0. To initialize Mixpanel, please use the instance method `init` instead. See the example below:
80
+ *
81
+ * <pre><code>
82
+ * const trackAutomaticEvents = true;
83
+ * const mixpanel = new Mixpanel('your project token', trackAutomaticEvents);
84
+ * mixpanel.init();
85
+ * </code></pre>
86
+ *
87
+ * Initializes Mixpanel and return an instance of Mixpanel the given project token.
88
+ *
89
+ * @param {string} token your project token.
90
+ * @param {boolean} trackAutomaticEvents Whether or not to automatically track common mobile events
91
+ * @param {boolean} Optional Whether or not Mixpanel can start tracking by default. See optOutTracking()
92
+ *
93
+ */
94
+ static async init(
95
+ token,
96
+ trackAutomaticEvents,
97
+ optOutTrackingDefault = DEFAULT_OPT_OUT
98
+ ) {
99
+ await MixpanelReactNative.initialize(
100
+ token,
101
+ trackAutomaticEvents,
102
+ optOutTrackingDefault,
103
+ Helper.getMetaData(),
104
+ "https://api.mixpanel.com"
105
+ );
106
+ return new Mixpanel(token, trackAutomaticEvents);
107
+ }
108
+
109
+ /**
110
+ * Set the base URL used for Mixpanel API requests.
111
+ * Useful if you need to proxy Mixpanel requests. Defaults to https://api.mixpanel.com.
112
+ * To route data to Mixpanel's EU servers, set to https://api-eu.mixpanel.com
113
+ *
114
+ * @param {string} serverURL the base URL used for Mixpanel API requests
115
+ *
116
+ */
117
+ setServerURL(serverURL) {
118
+ MixpanelReactNative.setServerURL(this.token, serverURL);
119
+ }
120
+
121
+ /**
122
+ * This allows enabling or disabling of all Mixpanel logs at run time.
123
+ * All logging is disabled by default. Usually, this is only required if
124
+ * you are running into issues with the SDK that you want to debug
125
+ *
126
+ * @param {boolean} loggingEnabled whether to enable logging
127
+ *
128
+ */
129
+ setLoggingEnabled(loggingEnabled) {
130
+ MixpanelReactNative.setLoggingEnabled(this.token, loggingEnabled);
131
+ }
132
+
133
+ /**
134
+ * This allows enabling or disabling whether or not Mixpanel flushes events
135
+ * when the app enters the background on iOS. This is set to true by default.
136
+ *
137
+ * @param {boolean} flushOnBackground whether to enable logging
138
+ *
139
+ */
140
+ setFlushOnBackground(flushOnBackground) {
141
+ if (Platform.OS === "ios") {
142
+ MixpanelReactNative.setFlushOnBackground(this.token, flushOnBackground);
143
+ } else {
144
+ console.warn(
145
+ "Mixpanel setFlushOnBackground was called and ignored because this method only works on iOS."
146
+ );
147
+ }
148
+ }
149
+
150
+ /**
151
+ * This controls whether to automatically send the client IP Address as part of event tracking.
152
+ * With an IP address, geo-location is possible down to neighborhoods within a city,
153
+ * although the Mixpanel Dashboard will just show you city level location specificity.
154
+ *
155
+ * @param {boolean} useIpAddressForGeolocation whether to automatically send the client IP Address.
156
+ * Defaults to true.
157
+ *
158
+ */
159
+ setUseIpAddressForGeolocation(useIpAddressForGeolocation) {
160
+ MixpanelReactNative.setUseIpAddressForGeolocation(
161
+ this.token,
162
+ useIpAddressForGeolocation
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Set the number of events sent in a single network request to the Mixpanel server.
168
+ * By configuring this value, you can optimize network usage and manage the frequency of communication between the client and the server. The maximum size is 50; any value over 50 will default to 50.
169
+ *
170
+ * @param {integer} flushBatchSize whether to automatically send the client IP Address.
171
+ * Defaults to true.
172
+ *
173
+ */
174
+ setFlushBatchSize(flushBatchSize) {
175
+ MixpanelReactNative.setFlushBatchSize(this.token, flushBatchSize);
176
+ }
177
+
178
+ /**
179
+ * Will return true if the user has opted out from tracking.
180
+ *
181
+ * @return {Promise<boolean>} true if user has opted out from tracking. Defaults to false.
182
+ */
183
+ hasOptedOutTracking() {
184
+ return MixpanelReactNative.hasOptedOutTracking(this.token);
185
+ }
186
+
187
+ /**
188
+ * Use this method to opt-in an already opted-out user from tracking. People updates and track
189
+ * calls will be sent to Mixpanel after using this method.
190
+ * This method will internally track an opt-in event to your project.
191
+ *
192
+ */
193
+ optInTracking() {
194
+ MixpanelReactNative.optInTracking(this.token);
195
+ }
196
+
197
+ /**
198
+ * Use this method to opt-out a user from tracking. Events and people updates that haven't been
199
+ * flushed yet will be deleted. Use flush() before calling this method if you want
200
+ * to send all the queues to Mixpanel before.
201
+ *
202
+ * This method will also remove any user-related information from the device.
203
+ */
204
+ optOutTracking() {
205
+ MixpanelReactNative.optOutTracking(this.token);
206
+ }
207
+
208
+ /**
209
+ * Associate all future calls to track() with the user identified by
210
+ * the given distinct id.
211
+ *
212
+ * <p>Calls to track() made before corresponding calls to identify
213
+ * will use an anonymous locally generated distinct id, which means it is best to call identify
214
+ * early to ensure that your Mixpanel funnels and retention analytics can continue to track the
215
+ * user throughout their lifetime. We recommend calling identify when the user authenticates.
216
+ *
217
+ * <p>Once identify is called, the local distinct id persists across restarts of
218
+ * your application.
219
+ *
220
+ * @param {string} distinctId a string uniquely identifying this user. Events sent to
221
+ * Mixpanel using the same disinct_id will be considered associated with the
222
+ * same visitor/customer for retention and funnel reporting, so be sure that the given
223
+ * value is globally unique for each individual user you intend to track.
224
+ * @returns {Promise} A promise that resolves when the identify is successful.
225
+ * It does not return any value.
226
+ *
227
+ */
228
+ identify(distinctId) {
229
+ return new Promise((resolve, reject) => {
230
+ if (!StringHelper.isValid(distinctId)) {
231
+ StringHelper.raiseError(PARAMS.DISTINCT_ID);
232
+ reject(new Error("Invalid distinctId"));
233
+ }
234
+ MixpanelReactNative.identify(this.token, distinctId)
235
+ .then(() => {
236
+ resolve();
237
+ })
238
+ .catch((err) => {
239
+ reject(err);
220
240
  });
221
- }
222
-
223
- /**
224
- * The alias method creates an alias which Mixpanel will use to remap one id to another.
225
- * Multiple aliases can point to the same identifier.
226
- *
227
- * `mixpane.alias("New ID", mixpane.distinctId)`
228
- * `mixpane.alias("Newer ID", mixpane.distinctId)`
229
- *
230
- * <p>This call does not identify the user after. You must still call identify()
231
- * if you wish the new alias to be used for Events and People.
232
- *
233
- * @param {string} alias A unique identifier that you want to use as an identifier for this user.
234
- * @param {string} distinctId the current distinct_id that alias will be mapped to.
235
- */
236
- alias(alias, distinctId) {
237
- if (!StringHelper.isValid(alias)) {
238
- StringHelper.raiseError(PARAMS.ALIAS);
239
- }
240
- if (!StringHelper.isValid(distinctId)) {
241
- StringHelper.raiseError(PARAMS.DISTINCT_ID);
242
- }
243
- MixpanelReactNative.alias(this.token, alias, distinctId);
244
- }
245
-
246
- /**
247
- * Track an event.
248
- *
249
- * <p>Every call to track eventually results in a data point sent to Mixpanel. These data points
250
- * are what are measured, counted, and broken down to create your Mixpanel reports. Events
251
- * have a string name, and an optional set of name/value pairs that describe the properties of
252
- * that event.
253
- *
254
- * @param {string} eventName The name of the event to send
255
- * @param {object} properties A Map containing the key value pairs of the properties to include in this event.
256
- * Pass null if no extra properties exist.
257
- */
258
- track(eventName, properties) {
259
- if (!StringHelper.isValid(eventName)) {
260
- StringHelper.raiseError(PARAMS.EVENT_NAME);
261
- }
262
- if (!ObjectHelper.isValidOrUndefined(properties)) {
263
- ObjectHelper.raiseError(PARAMS.PROPERTIES);
264
- }
265
- MixpanelReactNative.track(this.token, eventName, properties || {});
266
- }
267
-
268
- /**
269
- * Returns a Mixpanel People object that can be used to set and increment
270
- * People Analytics properties.
271
- *
272
- * @return {People} an instance of People that you can use to update
273
- * records in Mixpanel People Analytics
274
- */
275
- getPeople() {
276
- return this.people;
277
- }
278
-
279
- /**
280
- * Track an event with specific groups.
281
- *
282
- * <p>Every call to track eventually results in a data point sent to Mixpanel. These data points
283
- * are what are measured, counted, and broken down to create your Mixpanel reports. Events
284
- * have a string name, and an optional set of name/value pairs that describe the properties of
285
- * that event. Group key/value pairs are upserted into the property map before tracking.
286
- *
287
- * @param {string} eventName The name of the event to send
288
- * @param {object} properties A Map containing the key value pairs of the properties to include in this event.
289
- * Pass null if no extra properties exist.
290
- * @param {object} groups A Map containing the group key value pairs for this event.
291
- *
292
- */
293
- trackWithGroups(eventName, properties, groups) {
294
- if (!StringHelper.isValid(eventName)) {
295
- StringHelper.raiseError(PARAMS.EVENT_NAME);
296
- }
297
- if (!ObjectHelper.isValidOrUndefined(properties)) {
298
- ObjectHelper.raiseError(PARAMS.PROPERTIES);
299
- }
300
- MixpanelReactNative.trackWithGroups(this.token, eventName, properties || {}, groups);
301
- }
302
-
303
- /**
304
- * Set the group this user belongs to.
305
- *
306
- * @param {string} groupKey The property name associated with this group type (must already have been set up).
307
- * @param {object} groupID The group the user belongs to.
308
- */
309
- setGroup(groupKey, groupID) {
310
- if (!StringHelper.isValid(groupKey)) {
311
- StringHelper.raiseError(PARAMS.GROUP_KEY);
312
- }
313
- MixpanelReactNative.setGroup(this.token, groupKey, groupID);
314
- }
315
-
316
- /**
317
- * Returns a MixpanelGroup object that can be used to set and increment
318
- * Group Analytics properties.
319
- *
320
- * @param {string} groupKey String identifying the type of group (must be already in use as a group key)
321
- * @param {object} groupID Object identifying the specific group
322
- * @return an instance of MixpanelGroup that you can use to update
323
- * records in Mixpanel Group Analytics
324
- */
325
- getGroup(groupKey, groupID) {
326
- return new MixpanelGroup(this.token, groupKey, groupID);
327
- }
328
-
329
- /**
330
- * Add a group to this user's membership for a particular group key
331
- *
332
- * @param {string} groupKey The property name associated with this group type (must already have been set up).
333
- * @param {object} groupID The new group the user belongs to.
334
- */
335
- addGroup(groupKey, groupID) {
336
- if (!StringHelper.isValid(groupKey)) {
337
- StringHelper.raiseError(PARAMS.GROUP_KEY);
338
- }
339
- MixpanelReactNative.addGroup(this.token, groupKey, groupID);
340
- }
341
-
342
- /**
343
- * Remove a group from this user's membership for a particular group key
344
- *
345
- * @param {string} groupKey The property name associated with this group type (must already have been set up).
346
- * @param {object} groupID The group value to remove.
347
- */
348
- removeGroup(groupKey, groupID) {
349
- if (!StringHelper.isValid(groupKey)) {
350
- StringHelper.raiseError(PARAMS.GROUP_KEY);
351
- }
352
- MixpanelReactNative.removeGroup(this.token, groupKey, groupID);
353
- }
354
-
355
- /**
356
- * Permanently deletes this group's record from Group Analytics.
357
- *
358
- * @param {string} groupKey String identifying the type of group (must be already in use as a group key)
359
- * @param {object} groupID Object identifying the specific group
360
- * <p>Calling deleteGroup deletes an entire record completely. Any future calls
361
- * to Group Analytics using the same group value will create and store new values.
362
- */
363
- deleteGroup(groupKey, groupID) {
364
- if (!StringHelper.isValid(groupKey)) {
365
- StringHelper.raiseError(PARAMS.GROUP_KEY);
366
- }
367
- MixpanelReactNative.deleteGroup(this.token, groupKey, groupID);
368
- }
369
-
370
- /**
371
- * Register properties that will be sent with every subsequent call to track().
372
- *
373
- * <p>SuperProperties are a collection of properties that will be sent with every event to Mixpanel,
374
- * and persist beyond the lifetime of your application.
375
- *
376
- * <p>Setting a superProperty with registerSuperProperties will store a new superProperty,
377
- * possibly overwriting any existing superProperty with the same name (to set a
378
- * superProperty only if it is currently unset, use registerSuperPropertiesOnce())
379
- *
380
- * <p>SuperProperties will persist even if your application is taken completely out of memory.
381
- * to remove a superProperty, call unregisterSuperProperty() or clearSuperProperties()
382
- *
383
- * @param {object} properties A Map containing super properties to register
384
- */
385
- registerSuperProperties(properties) {
386
- if (!ObjectHelper.isValidOrUndefined(properties)) {
387
- ObjectHelper.raiseError(PARAMS.PROPERTIES);
388
- }
389
- MixpanelReactNative.registerSuperProperties(this.token, properties || {});
390
- }
391
-
392
- /**
393
- * Register super properties for events, only if no other super property with the
394
- * same names has already been registered.
395
- *
396
- * <p>Calling registerSuperPropertiesOnce will never overwrite existing properties.
397
- *
398
- * @param {object} properties A Map containing the super properties to register.
399
- */
400
- registerSuperPropertiesOnce(properties) {
401
- if (!ObjectHelper.isValidOrUndefined(properties)) {
402
- ObjectHelper.raiseError(PARAMS.PROPERTIES);
403
- }
404
- MixpanelReactNative.registerSuperPropertiesOnce(this.token, properties || {});
405
- }
406
-
407
- /**
408
- * Remove a single superProperty, so that it will not be sent with future calls to track().
409
- *
410
- * <p>If there is a superProperty registered with the given name, it will be permanently
411
- * removed from the existing superProperties.
412
- * To clear all superProperties, use clearSuperProperties()
413
- *
414
- * @param {string} propertyName name of the property to unregister
415
- */
416
- unregisterSuperProperty(propertyName) {
417
- if (!StringHelper.isValid(propertyName)) {
418
- StringHelper.raiseError(PARAMS.PROPERTY_NAME);
419
- }
420
- MixpanelReactNative.unregisterSuperProperty(this.token, propertyName);
421
- }
422
-
423
- /**
424
- * Returns a json object of the user's current super properties
425
- *
426
- *<p>SuperProperties are a collection of properties that will be sent with every event to Mixpanel,
427
- * and persist beyond the lifetime of your application.
428
- *
429
- * @return {Promise<object>} Super properties for this Mixpanel instance.
430
- */
431
- getSuperProperties() {
432
- return MixpanelReactNative.getSuperProperties(this.token);
433
- }
434
-
435
- /**
436
- * Erase all currently registered superProperties.
437
- *
438
- * <p>Future tracking calls to Mixpanel will not contain the specific
439
- * superProperties registered before the clearSuperProperties method was called.
440
- *
441
- * <p>To remove a single superProperty, use unregisterSuperProperty()
442
- */
443
- clearSuperProperties() {
444
- MixpanelReactNative.clearSuperProperties(this.token);
445
- }
446
-
447
- /**
448
- * Begin timing of an event. Calling timeEvent("Thing") will not send an event, but
449
- * when you eventually call track("Thing"), your tracked event will be sent with a "$duration"
450
- * property, representing the number of seconds between your calls.
451
- *
452
- * @param {string} eventName the name of the event to track with timing.
453
- */
454
- timeEvent(eventName) {
455
- if (!StringHelper.isValid(eventName)) {
456
- StringHelper.raiseError(PARAMS.EVENT_NAME);
457
- }
458
- MixpanelReactNative.timeEvent(this.token, eventName);
459
- }
460
-
461
- /**
462
- * Retrieves the time elapsed for the named event since timeEvent() was called.
463
- *
464
- * @param {string} eventName the name of the event to be tracked that was previously called with timeEvent()
465
- *
466
- * @return {Promise<number>} Time elapsed since timeEvent(String) was called for the given eventName.
467
- */
468
- eventElapsedTime(eventName) {
469
- if (!StringHelper.isValid(eventName)) {
470
- StringHelper.raiseError(PARAMS.EVENT_NAME);
471
- }
472
- return MixpanelReactNative.eventElapsedTime(this.token, eventName);
473
- }
474
-
475
- /**
241
+ });
242
+ }
243
+
244
+ /**
245
+ * The alias method creates an alias which Mixpanel will use to remap one id to another.
246
+ * Multiple aliases can point to the same identifier.
247
+ *
248
+ * `mixpane.alias("New ID", mixpane.distinctId)`
249
+ * `mixpane.alias("Newer ID", mixpane.distinctId)`
250
+ *
251
+ * <p>This call does not identify the user after. You must still call identify()
252
+ * if you wish the new alias to be used for Events and People.
253
+ *
254
+ * @param {string} alias A unique identifier that you want to use as an identifier for this user.
255
+ * @param {string} distinctId the current distinct_id that alias will be mapped to.
256
+ */
257
+ alias(alias, distinctId) {
258
+ if (!StringHelper.isValid(alias)) {
259
+ StringHelper.raiseError(PARAMS.ALIAS);
260
+ }
261
+ if (!StringHelper.isValid(distinctId)) {
262
+ StringHelper.raiseError(PARAMS.DISTINCT_ID);
263
+ }
264
+ MixpanelReactNative.alias(this.token, alias, distinctId);
265
+ }
266
+
267
+ /**
268
+ * Track an event.
269
+ *
270
+ * <p>Every call to track eventually results in a data point sent to Mixpanel. These data points
271
+ * are what are measured, counted, and broken down to create your Mixpanel reports. Events
272
+ * have a string name, and an optional set of name/value pairs that describe the properties of
273
+ * that event.
274
+ *
275
+ * @param {string} eventName The name of the event to send
276
+ * @param {object} properties A Map containing the key value pairs of the properties to include in this event.
277
+ * Pass null if no extra properties exist.
278
+ */
279
+ track(eventName, properties) {
280
+ if (!StringHelper.isValid(eventName)) {
281
+ StringHelper.raiseError(PARAMS.EVENT_NAME);
282
+ }
283
+ if (!ObjectHelper.isValidOrUndefined(properties)) {
284
+ ObjectHelper.raiseError(PARAMS.PROPERTIES);
285
+ }
286
+ MixpanelReactNative.track(this.token, eventName, {
287
+ ...Helper.getMetaData(),
288
+ ...properties,
289
+ });
290
+ }
291
+
292
+ /**
293
+ * Returns a Mixpanel People object that can be used to set and increment
294
+ * People Analytics properties.
295
+ *
296
+ * @return {People} an instance of People that you can use to update
297
+ * records in Mixpanel People Analytics
298
+ */
299
+ getPeople() {
300
+ return this.people;
301
+ }
302
+
303
+ /**
304
+ * Track an event with specific groups.
305
+ *
306
+ * <p>Every call to track eventually results in a data point sent to Mixpanel. These data points
307
+ * are what are measured, counted, and broken down to create your Mixpanel reports. Events
308
+ * have a string name, and an optional set of name/value pairs that describe the properties of
309
+ * that event. Group key/value pairs are upserted into the property map before tracking.
310
+ *
311
+ * @param {string} eventName The name of the event to send
312
+ * @param {object} properties A Map containing the key value pairs of the properties to include in this event.
313
+ * Pass null if no extra properties exist.
314
+ * @param {object} groups A Map containing the group key value pairs for this event.
315
+ *
316
+ */
317
+ trackWithGroups(eventName, properties, groups) {
318
+ if (!StringHelper.isValid(eventName)) {
319
+ StringHelper.raiseError(PARAMS.EVENT_NAME);
320
+ }
321
+ if (!ObjectHelper.isValidOrUndefined(properties)) {
322
+ ObjectHelper.raiseError(PARAMS.PROPERTIES);
323
+ }
324
+ MixpanelReactNative.trackWithGroups(
325
+ this.token,
326
+ eventName,
327
+ {
328
+ ...Helper.getMetaData(),
329
+ ...properties,
330
+ },
331
+ groups
332
+ );
333
+ }
334
+
335
+ /**
336
+ * Set the group this user belongs to.
337
+ *
338
+ * @param {string} groupKey The property name associated with this group type (must already have been set up).
339
+ * @param {object} groupID The group the user belongs to.
340
+ */
341
+ setGroup(groupKey, groupID) {
342
+ if (!StringHelper.isValid(groupKey)) {
343
+ StringHelper.raiseError(PARAMS.GROUP_KEY);
344
+ }
345
+ MixpanelReactNative.setGroup(this.token, groupKey, groupID);
346
+ }
347
+
348
+ /**
349
+ * Returns a MixpanelGroup object that can be used to set and increment
350
+ * Group Analytics properties.
351
+ *
352
+ * @param {string} groupKey String identifying the type of group (must be already in use as a group key)
353
+ * @param {object} groupID Object identifying the specific group
354
+ * @return an instance of MixpanelGroup that you can use to update
355
+ * records in Mixpanel Group Analytics
356
+ */
357
+ getGroup(groupKey, groupID) {
358
+ return new MixpanelGroup(this.token, groupKey, groupID);
359
+ }
360
+
361
+ /**
362
+ * Add a group to this user's membership for a particular group key
363
+ *
364
+ * @param {string} groupKey The property name associated with this group type (must already have been set up).
365
+ * @param {object} groupID The new group the user belongs to.
366
+ */
367
+ addGroup(groupKey, groupID) {
368
+ if (!StringHelper.isValid(groupKey)) {
369
+ StringHelper.raiseError(PARAMS.GROUP_KEY);
370
+ }
371
+ MixpanelReactNative.addGroup(this.token, groupKey, groupID);
372
+ }
373
+
374
+ /**
375
+ * Remove a group from this user's membership for a particular group key
376
+ *
377
+ * @param {string} groupKey The property name associated with this group type (must already have been set up).
378
+ * @param {object} groupID The group value to remove.
379
+ */
380
+ removeGroup(groupKey, groupID) {
381
+ if (!StringHelper.isValid(groupKey)) {
382
+ StringHelper.raiseError(PARAMS.GROUP_KEY);
383
+ }
384
+ MixpanelReactNative.removeGroup(this.token, groupKey, groupID);
385
+ }
386
+
387
+ /**
388
+ * Permanently deletes this group's record from Group Analytics.
389
+ *
390
+ * @param {string} groupKey String identifying the type of group (must be already in use as a group key)
391
+ * @param {object} groupID Object identifying the specific group
392
+ * <p>Calling deleteGroup deletes an entire record completely. Any future calls
393
+ * to Group Analytics using the same group value will create and store new values.
394
+ */
395
+ deleteGroup(groupKey, groupID) {
396
+ if (!StringHelper.isValid(groupKey)) {
397
+ StringHelper.raiseError(PARAMS.GROUP_KEY);
398
+ }
399
+ MixpanelReactNative.deleteGroup(this.token, groupKey, groupID);
400
+ }
401
+
402
+ /**
403
+ * Register properties that will be sent with every subsequent call to track().
404
+ *
405
+ * <p>SuperProperties are a collection of properties that will be sent with every event to Mixpanel,
406
+ * and persist beyond the lifetime of your application.
407
+ *
408
+ * <p>Setting a superProperty with registerSuperProperties will store a new superProperty,
409
+ * possibly overwriting any existing superProperty with the same name (to set a
410
+ * superProperty only if it is currently unset, use registerSuperPropertiesOnce())
411
+ *
412
+ * <p>SuperProperties will persist even if your application is taken completely out of memory.
413
+ * to remove a superProperty, call unregisterSuperProperty() or clearSuperProperties()
414
+ *
415
+ * @param {object} properties A Map containing super properties to register
416
+ */
417
+ registerSuperProperties(properties) {
418
+ if (!ObjectHelper.isValidOrUndefined(properties)) {
419
+ ObjectHelper.raiseError(PARAMS.PROPERTIES);
420
+ }
421
+ MixpanelReactNative.registerSuperProperties(this.token, properties || {});
422
+ }
423
+
424
+ /**
425
+ * Register super properties for events, only if no other super property with the
426
+ * same names has already been registered.
427
+ *
428
+ * <p>Calling registerSuperPropertiesOnce will never overwrite existing properties.
429
+ *
430
+ * @param {object} properties A Map containing the super properties to register.
431
+ */
432
+ registerSuperPropertiesOnce(properties) {
433
+ if (!ObjectHelper.isValidOrUndefined(properties)) {
434
+ ObjectHelper.raiseError(PARAMS.PROPERTIES);
435
+ }
436
+ MixpanelReactNative.registerSuperPropertiesOnce(
437
+ this.token,
438
+ properties || {}
439
+ );
440
+ }
441
+
442
+ /**
443
+ * Remove a single superProperty, so that it will not be sent with future calls to track().
444
+ *
445
+ * <p>If there is a superProperty registered with the given name, it will be permanently
446
+ * removed from the existing superProperties.
447
+ * To clear all superProperties, use clearSuperProperties()
448
+ *
449
+ * @param {string} propertyName name of the property to unregister
450
+ */
451
+ unregisterSuperProperty(propertyName) {
452
+ if (!StringHelper.isValid(propertyName)) {
453
+ StringHelper.raiseError(PARAMS.PROPERTY_NAME);
454
+ }
455
+ MixpanelReactNative.unregisterSuperProperty(this.token, propertyName);
456
+ }
457
+
458
+ /**
459
+ * Returns a json object of the user's current super properties
460
+ *
461
+ *<p>SuperProperties are a collection of properties that will be sent with every event to Mixpanel,
462
+ * and persist beyond the lifetime of your application.
463
+ *
464
+ * @return {Promise<object>} Super properties for this Mixpanel instance.
465
+ */
466
+ getSuperProperties() {
467
+ return MixpanelReactNative.getSuperProperties(this.token);
468
+ }
469
+
470
+ /**
471
+ * Erase all currently registered superProperties.
472
+ *
473
+ * <p>Future tracking calls to Mixpanel will not contain the specific
474
+ * superProperties registered before the clearSuperProperties method was called.
475
+ *
476
+ * <p>To remove a single superProperty, use unregisterSuperProperty()
477
+ */
478
+ clearSuperProperties() {
479
+ MixpanelReactNative.clearSuperProperties(this.token);
480
+ }
481
+
482
+ /**
483
+ * Begin timing of an event. Calling timeEvent("Thing") will not send an event, but
484
+ * when you eventually call track("Thing"), your tracked event will be sent with a "$duration"
485
+ * property, representing the number of seconds between your calls.
486
+ *
487
+ * @param {string} eventName the name of the event to track with timing.
488
+ */
489
+ timeEvent(eventName) {
490
+ if (!StringHelper.isValid(eventName)) {
491
+ StringHelper.raiseError(PARAMS.EVENT_NAME);
492
+ }
493
+ MixpanelReactNative.timeEvent(this.token, eventName);
494
+ }
495
+
496
+ /**
497
+ * Retrieves the time elapsed for the named event since timeEvent() was called.
498
+ *
499
+ * @param {string} eventName the name of the event to be tracked that was previously called with timeEvent()
500
+ *
501
+ * @return {Promise<number>} Time elapsed since timeEvent(String) was called for the given eventName.
502
+ */
503
+ eventElapsedTime(eventName) {
504
+ if (!StringHelper.isValid(eventName)) {
505
+ StringHelper.raiseError(PARAMS.EVENT_NAME);
506
+ }
507
+ return MixpanelReactNative.eventElapsedTime(this.token, eventName);
508
+ }
509
+
510
+ /**
476
511
  Clear super properties and generates a new random distinctId for this instance.
477
512
  Useful for clearing data when a user logs out.
478
513
  */
479
- reset() {
480
- MixpanelReactNative.reset(this.token);
481
- }
482
-
483
- /**
484
- * Returns the current distinct id of the user.
485
- * This is either the id automatically generated by the library or the id that has been passed by a call to identify().
486
- *
487
- * example of usage:
488
- * <pre>
489
- * <code>
490
- * const distinctId = await mixpanel.getDistinctId();
491
- * </code>
492
- * </pre>
493
- *
494
- * @return {Promise<string>} A Promise to the distinct id associated with Mixpanel event and People Analytics
495
- *
496
- */
497
- getDistinctId() {
498
- return MixpanelReactNative.getDistinctId(this.token);
499
- }
500
-
501
- /**
502
- * Returns the current device id of the device.
503
- * This id automatically generated by the library and regenerated when logout or reset is called.
504
- *
505
- * example of usage:
506
- * <pre>
507
- * <code>
508
- * const deviceId = await mixpanel.getDeviceId();
509
- * </code>
510
- * </pre>
511
- *
512
- * @return {Promise<string>} A Promise to the device id
513
- *
514
- */
515
- getDeviceId() {
516
- return MixpanelReactNative.getDeviceId(this.token);
517
- }
518
-
519
- /**
520
- * Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
521
- *
522
- * <p>Events and People messages are pushed gradually throughout
523
- * the lifetime of your application. This means that to ensure that all messages
524
- * are sent to Mixpanel when your application is shut down, you will
525
- * need to call flush() to let the Mixpanel library know it should
526
- * send all remaining messages to the server.
527
- */
528
- flush() {
529
- MixpanelReactNative.flush(this.token);
530
- }
514
+ reset() {
515
+ MixpanelReactNative.reset(this.token);
516
+ }
517
+
518
+ /**
519
+ * Returns the current distinct id of the user.
520
+ * This is either the id automatically generated by the library or the id that has been passed by a call to identify().
521
+ *
522
+ * example of usage:
523
+ * <pre>
524
+ * <code>
525
+ * const distinctId = await mixpanel.getDistinctId();
526
+ * </code>
527
+ * </pre>
528
+ *
529
+ * @return {Promise<string>} A Promise to the distinct id associated with Mixpanel event and People Analytics
530
+ *
531
+ */
532
+ getDistinctId() {
533
+ return MixpanelReactNative.getDistinctId(this.token);
534
+ }
535
+
536
+ /**
537
+ * Returns the current device id of the device.
538
+ * This id automatically generated by the library and regenerated when logout or reset is called.
539
+ *
540
+ * example of usage:
541
+ * <pre>
542
+ * <code>
543
+ * const deviceId = await mixpanel.getDeviceId();
544
+ * </code>
545
+ * </pre>
546
+ *
547
+ * @return {Promise<string>} A Promise to the device id
548
+ *
549
+ */
550
+ getDeviceId() {
551
+ return MixpanelReactNative.getDeviceId(this.token);
552
+ }
553
+
554
+ /**
555
+ * Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
556
+ *
557
+ * <p>Events and People messages are pushed gradually throughout
558
+ * the lifetime of your application. This means that to ensure that all messages
559
+ * are sent to Mixpanel when your application is shut down, you will
560
+ * need to call flush() to let the Mixpanel library know it should
561
+ * send all remaining messages to the server.
562
+ */
563
+ flush() {
564
+ MixpanelReactNative.flush(this.token);
565
+ }
531
566
  }
532
567
 
533
568
  /**
@@ -542,199 +577,202 @@ export class Mixpanel {
542
577
  *
543
578
  */
544
579
  export class People {
545
-
546
- constructor(token) {
547
- if (!StringHelper.isValid(token)) {
548
- StringHelper.raiseError(PARAMS.TOKEN);
549
- }
550
- this.token = token;
551
- }
552
-
553
- /**
554
- * Sets a single property with the given name and value for this user.
555
- * The given name and value will be assigned to the user in Mixpanel People Analytics,
556
- * possibly overwriting an existing property with the same name.
557
- *
558
- * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
559
- * @param {object} to The value of the Mixpanel property. For "Zip Code", this value might be the String "90210"
560
- */
561
- set(prop, to) {
562
- let properties = {};
563
- if (ObjectHelper.isValid(prop)) {
564
- properties = JSON.parse(JSON.stringify(prop || {}));
565
- } else {
566
- if (!StringHelper.isValid(prop)) {
567
- StringHelper.raiseError(PARAMS.PROP);
568
- }
569
- properties[prop] = to;
570
- }
571
- MixpanelReactNative.set(this.token, properties);
572
- }
573
-
574
- /**
575
- * Works just like set(), except it will not overwrite existing property values. This is useful for properties like "First login date".
576
- *
577
- * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
578
- * @param {object} to The value of the Mixpanel property. For "Zip Code", this value might be the String "90210"
579
- */
580
- setOnce(prop, to) {
581
- let properties = {};
582
- if (ObjectHelper.isValid(prop)) {
583
- prop = prop || {};
584
- properties = JSON.parse(JSON.stringify(prop));
585
- } else {
586
- if (!StringHelper.isValid(prop)) {
587
- StringHelper.raiseError(PARAMS.PROP);
588
- }
589
- properties[prop] = to;
590
- }
591
- MixpanelReactNative.setOnce(this.token, properties);
592
- }
593
-
594
- /**
595
- * Add the given amount to an existing property on the identified user. If the user does not already
596
- * have the associated property, the amount will be added to zero. To reduce a property,
597
- * provide a negative number for the value.
598
- *
599
- * @param {string} prop the People Analytics property that should have its value changed
600
- * @param {number} by the amount to be added to the current value of the named property
601
- *
602
- */
603
- increment(prop, by) {
604
- var add = {};
605
- if (ObjectHelper.isValid(prop)) {
606
- Object.keys(prop).forEach(function (key) {
607
- var val = prop[key];
608
- if (isNaN(parseFloat(val))) {
609
- throw new Error(`${PARAMS.PROPERTY_VALUE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`);
610
- }
611
- add[key] = val;
612
- });
613
- } else {
614
- by = by || 1;
615
- if (isNaN(parseFloat(by))) {
616
- throw new Error(`${PARAMS.PROPERTY_VALUE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`);
617
- }
618
-
619
- if (!StringHelper.isValid(prop)) {
620
- StringHelper.raiseError(PARAMS.NAME);
621
- }
622
-
623
- add[prop] = by;
624
- }
625
- MixpanelReactNative.increment(this.token, add);
626
- }
627
-
628
- /**
629
- * Appends a value to a list-valued property. If the property does not currently exist,
630
- * it will be created as a list of one element. If the property does exist and doesn't
631
- * currently have a list value, the append will be ignored.
632
- * @param {string} name the People Analytics property that should have it's value appended to
633
- * @param {object} value the new value that will appear at the end of the property's list
634
- */
635
- append(name, value) {
636
- let appendProp = {};
637
- if (!StringHelper.isValid(name)) {
638
- StringHelper.raiseError(PARAMS.NAME);
639
- } else {
640
- appendProp[name] = value;
641
- }
642
-
643
- if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
644
- MixpanelReactNative.append(this.token, appendProp);
645
- } else {
646
- MixpanelReactNative.append(this.token, name, value);
647
- }
648
- }
649
-
650
- /**
651
- * Adds values to a list-valued property only if they are not already present in the list.
652
- * If the property does not currently exist, it will be created with the given list as it's value.
653
- * If the property exists and is not list-valued, the union will be ignored.
654
- *
655
- * @param {string} name name of the list-valued property to set or modify
656
- * @param {array} value an array of values to add to the property value if not already present
657
- */
658
- union(name, value) {
659
- if (!StringHelper.isValid(name)) {
660
- StringHelper.raiseError(PARAMS.NAME);
661
- }
662
-
663
- value = Array.isArray(value) ? value : [value];
664
-
665
- if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
666
- MixpanelReactNative.union(this.token, {[name]: value});
667
- } else {
668
- MixpanelReactNative.union(this.token, name, value);
669
- }
670
- }
671
-
672
- /**
673
- * Remove value from a list-valued property only if they are already present in the list.
674
- * If the property does not currently exist, the remove will be ignored.
675
- * If the property exists and is not list-valued, the remove will be ignored.
676
- * @param {string} name the People Analytics property that should have it's value removed from
677
- * @param {object} value the value that will be removed from the property's list
678
- */
679
- remove(name, value) {
680
- let removeProp = {};
681
- if (!StringHelper.isValid(name)) {
682
- StringHelper.raiseError(PARAMS.NAME);
683
- } else {
684
- removeProp[name] = value;
685
- }
686
-
687
- if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
688
- MixpanelReactNative.remove(this.token, removeProp);
689
- } else {
690
- MixpanelReactNative.remove(this.token, name, value);
691
- }
692
- }
693
-
694
- /**
695
- * permanently removes the property with the given name from the user's profile
696
- * @param {string} name name of a property to unset
697
- */
698
- unset(name) {
699
- if (!StringHelper.isValid(name)) {
700
- StringHelper.raiseError(PARAMS.PROPERTY_NAME);
701
- }
702
- MixpanelReactNative.unset(this.token, name);
703
- }
704
-
705
- /**
706
- * Track a revenue transaction for the identified people profile.
707
- *
708
- * @param {number} charge the amount of money exchanged. Positive amounts represent purchases or income from the customer, negative amounts represent refunds or payments to the customer.
709
- * @param {object} properties an optional collection of properties to associate with this transaction.
710
- */
711
- trackCharge(charge, properties) {
712
- if (isNaN(parseFloat(charge))) {
713
- throw new Error(`${PARAMS.CHARGE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`)
714
- }
715
-
716
- if (!ObjectHelper.isValidOrUndefined(properties)) {
717
- ObjectHelper.raiseError(PARAMS.PROPERTIES);
718
- }
719
- MixpanelReactNative.trackCharge(this.token, charge, properties || {});
720
- }
721
-
722
- /**
723
- * Permanently clear the whole transaction history for the identified people profile.
724
- */
725
- clearCharges() {
726
- MixpanelReactNative.clearCharges(this.token);
727
- }
728
-
729
- /**
730
- * Permanently deletes the identified user's record from People Analytics.
731
- *
732
- * <p>Calling deleteUser deletes an entire record completely. Any future calls
733
- * to People Analytics using the same distinct id will create and store new values.
734
- */
735
- deleteUser() {
736
- MixpanelReactNative.deleteUser(this.token);
737
- }
580
+ constructor(token) {
581
+ if (!StringHelper.isValid(token)) {
582
+ StringHelper.raiseError(PARAMS.TOKEN);
583
+ }
584
+ this.token = token;
585
+ }
586
+
587
+ /**
588
+ * Sets a single property with the given name and value for this user.
589
+ * The given name and value will be assigned to the user in Mixpanel People Analytics,
590
+ * possibly overwriting an existing property with the same name.
591
+ *
592
+ * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
593
+ * @param {object} to The value of the Mixpanel property. For "Zip Code", this value might be the String "90210"
594
+ */
595
+ set(prop, to) {
596
+ let properties = {};
597
+ if (ObjectHelper.isValid(prop)) {
598
+ properties = JSON.parse(JSON.stringify(prop || {}));
599
+ } else {
600
+ if (!StringHelper.isValid(prop)) {
601
+ StringHelper.raiseError(PARAMS.PROP);
602
+ }
603
+ properties[prop] = to;
604
+ }
605
+ MixpanelReactNative.set(this.token, properties);
606
+ }
607
+
608
+ /**
609
+ * Works just like set(), except it will not overwrite existing property values. This is useful for properties like "First login date".
610
+ *
611
+ * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
612
+ * @param {object} to The value of the Mixpanel property. For "Zip Code", this value might be the String "90210"
613
+ */
614
+ setOnce(prop, to) {
615
+ let properties = {};
616
+ if (ObjectHelper.isValid(prop)) {
617
+ prop = prop || {};
618
+ properties = JSON.parse(JSON.stringify(prop));
619
+ } else {
620
+ if (!StringHelper.isValid(prop)) {
621
+ StringHelper.raiseError(PARAMS.PROP);
622
+ }
623
+ properties[prop] = to;
624
+ }
625
+ MixpanelReactNative.setOnce(this.token, properties);
626
+ }
627
+
628
+ /**
629
+ * Add the given amount to an existing property on the identified user. If the user does not already
630
+ * have the associated property, the amount will be added to zero. To reduce a property,
631
+ * provide a negative number for the value.
632
+ *
633
+ * @param {string} prop the People Analytics property that should have its value changed
634
+ * @param {number} by the amount to be added to the current value of the named property
635
+ *
636
+ */
637
+ increment(prop, by) {
638
+ var add = {};
639
+ if (ObjectHelper.isValid(prop)) {
640
+ Object.keys(prop).forEach(function (key) {
641
+ var val = prop[key];
642
+ if (isNaN(parseFloat(val))) {
643
+ throw new Error(
644
+ `${PARAMS.PROPERTY_VALUE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`
645
+ );
646
+ }
647
+ add[key] = val;
648
+ });
649
+ } else {
650
+ by = by || 1;
651
+ if (isNaN(parseFloat(by))) {
652
+ throw new Error(
653
+ `${PARAMS.PROPERTY_VALUE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`
654
+ );
655
+ }
656
+
657
+ if (!StringHelper.isValid(prop)) {
658
+ StringHelper.raiseError(PARAMS.NAME);
659
+ }
660
+
661
+ add[prop] = by;
662
+ }
663
+ MixpanelReactNative.increment(this.token, add);
664
+ }
665
+
666
+ /**
667
+ * Appends a value to a list-valued property. If the property does not currently exist,
668
+ * it will be created as a list of one element. If the property does exist and doesn't
669
+ * currently have a list value, the append will be ignored.
670
+ * @param {string} name the People Analytics property that should have it's value appended to
671
+ * @param {object} value the new value that will appear at the end of the property's list
672
+ */
673
+ append(name, value) {
674
+ let appendProp = {};
675
+ if (!StringHelper.isValid(name)) {
676
+ StringHelper.raiseError(PARAMS.NAME);
677
+ } else {
678
+ appendProp[name] = value;
679
+ }
680
+
681
+ if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
682
+ MixpanelReactNative.append(this.token, appendProp);
683
+ } else {
684
+ MixpanelReactNative.append(this.token, name, value);
685
+ }
686
+ }
687
+
688
+ /**
689
+ * Adds values to a list-valued property only if they are not already present in the list.
690
+ * If the property does not currently exist, it will be created with the given list as it's value.
691
+ * If the property exists and is not list-valued, the union will be ignored.
692
+ *
693
+ * @param {string} name name of the list-valued property to set or modify
694
+ * @param {array} value an array of values to add to the property value if not already present
695
+ */
696
+ union(name, value) {
697
+ if (!StringHelper.isValid(name)) {
698
+ StringHelper.raiseError(PARAMS.NAME);
699
+ }
700
+
701
+ value = Array.isArray(value) ? value : [value];
702
+
703
+ if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
704
+ MixpanelReactNative.union(this.token, {[name]: value});
705
+ } else {
706
+ MixpanelReactNative.union(this.token, name, value);
707
+ }
708
+ }
709
+
710
+ /**
711
+ * Remove value from a list-valued property only if they are already present in the list.
712
+ * If the property does not currently exist, the remove will be ignored.
713
+ * If the property exists and is not list-valued, the remove will be ignored.
714
+ * @param {string} name the People Analytics property that should have it's value removed from
715
+ * @param {object} value the value that will be removed from the property's list
716
+ */
717
+ remove(name, value) {
718
+ let removeProp = {};
719
+ if (!StringHelper.isValid(name)) {
720
+ StringHelper.raiseError(PARAMS.NAME);
721
+ } else {
722
+ removeProp[name] = value;
723
+ }
724
+
725
+ if (DevicePlatform.iOS === Helper.getDevicePlatform()) {
726
+ MixpanelReactNative.remove(this.token, removeProp);
727
+ } else {
728
+ MixpanelReactNative.remove(this.token, name, value);
729
+ }
730
+ }
731
+
732
+ /**
733
+ * permanently removes the property with the given name from the user's profile
734
+ * @param {string} name name of a property to unset
735
+ */
736
+ unset(name) {
737
+ if (!StringHelper.isValid(name)) {
738
+ StringHelper.raiseError(PARAMS.PROPERTY_NAME);
739
+ }
740
+ MixpanelReactNative.unset(this.token, name);
741
+ }
742
+
743
+ /**
744
+ * Track a revenue transaction for the identified people profile.
745
+ *
746
+ * @param {number} charge the amount of money exchanged. Positive amounts represent purchases or income from the customer, negative amounts represent refunds or payments to the customer.
747
+ * @param {object} properties an optional collection of properties to associate with this transaction.
748
+ */
749
+ trackCharge(charge, properties) {
750
+ if (isNaN(parseFloat(charge))) {
751
+ throw new Error(`${PARAMS.CHARGE}${ERROR_MESSAGE.REQUIRED_DOUBLE}`);
752
+ }
753
+
754
+ if (!ObjectHelper.isValidOrUndefined(properties)) {
755
+ ObjectHelper.raiseError(PARAMS.PROPERTIES);
756
+ }
757
+ MixpanelReactNative.trackCharge(this.token, charge, properties || {});
758
+ }
759
+
760
+ /**
761
+ * Permanently clear the whole transaction history for the identified people profile.
762
+ */
763
+ clearCharges() {
764
+ MixpanelReactNative.clearCharges(this.token);
765
+ }
766
+
767
+ /**
768
+ * Permanently deletes the identified user's record from People Analytics.
769
+ *
770
+ * <p>Calling deleteUser deletes an entire record completely. Any future calls
771
+ * to People Analytics using the same distinct id will create and store new values.
772
+ */
773
+ deleteUser() {
774
+ MixpanelReactNative.deleteUser(this.token);
775
+ }
738
776
  }
739
777
 
740
778
  /**
@@ -743,169 +781,194 @@ export class People {
743
781
  * <p>The MixpanelGroup object is used to update properties in a group's Group Analytics record.
744
782
  */
745
783
  export class MixpanelGroup {
746
-
747
- constructor(token, groupKey, groupID) {
748
- if (!StringHelper.isValid(token)) {
749
- StringHelper.raiseError(PARAMS.TOKEN);
750
- }
751
- this.token = token;
752
- this.groupKey = groupKey;
753
- this.groupID = groupID;
754
- }
755
-
756
- /**
757
- * Sets a single property with the given name and value for this group.
758
- * The given name and value will be assigned to the user in Mixpanel Group Analytics,
759
- * possibly overwriting an existing property with the same name.
760
- *
761
- * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
762
- * @param {string} to The value to set on the given property name. For "Zip Code", this value might be the String "90210"
763
- */
764
- set(prop, to) {
765
- let properties = {};
766
- if (ObjectHelper.isValid(prop)) {
767
- properties = JSON.parse(JSON.stringify(prop || {}));
768
- } else {
769
- if (!StringHelper.isValid(prop)) {
770
- StringHelper.raiseError(PARAMS.PROP);
771
- }
772
- properties[prop] = to;
773
- }
774
- MixpanelReactNative.groupSetProperties(this.token, this.groupKey, this.groupID, properties);
775
- }
776
-
777
- /**
778
- * Works just like groupSet() except it will not overwrite existing property values. This is useful for properties like "First login date".
779
- *
780
- * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
781
- * @param {string} to The value to set on the given property name. For "Zip Code", this value might be the String "90210"
782
- */
783
- setOnce(prop, to) {
784
- let properties = {};
785
- if (ObjectHelper.isValid(prop)) {
786
- properties = JSON.parse(JSON.stringify(prop || {}));
787
- } else {
788
- if (!StringHelper.isValid(prop)) {
789
- StringHelper.raiseError(PARAMS.PROP);
790
- }
791
- properties[prop] = to;
792
- }
793
- MixpanelReactNative.groupSetPropertyOnce(this.token, this.groupKey, this.groupID, properties);
794
- }
795
-
796
- /**
797
- * Permanently removes the property with the given name from the group's profile
798
- *
799
- * @param {string} prop name of a property to unset
800
- */
801
- unset(prop) {
802
- if (!StringHelper.isValid(prop)) {
803
- StringHelper.raiseError(PARAMS.PROPERTY_NAME);
804
- }
805
- MixpanelReactNative.groupUnsetProperty(this.token, this.groupKey, this.groupID, prop);
806
- }
807
-
808
- /**
809
- * Remove value from a list-valued property only if it is already present in the list.
810
- * If the property does not currently exist, the remove will be ignored.
811
- * If the property exists and is not list-valued, the remove will be ignored.
812
- *
813
- * @param {string} name the Group Analytics list-valued property that should have a value removed
814
- * @param {any} value the value that will be removed from the list
815
- */
816
- remove(name, value) {
817
- if (!StringHelper.isValid(name)) {
818
- StringHelper.raiseError(PARAMS.PROPERTY_NAME);
819
- }
820
-
821
- MixpanelReactNative.groupRemovePropertyValue(this.token, this.groupKey, this.groupID, name, value);
822
- }
823
-
824
- /**
825
- * Adds values to a list-valued property only if they are not already present in the list.
826
- * If the property does not currently exist, it will be created with the given list as its value.
827
- * If the property exists and is not list-valued, the union will be ignored.
828
- *
829
- * @param {string} name name of the list-valued property to set or modify
830
- * @param {array} value an array of values to add to the property value if not already present
831
- */
832
- union(name, value) {
833
- if (!StringHelper.isValid(name)) {
834
- StringHelper.raiseError(PARAMS.PROPERTY_NAME);
835
- }
836
- value = Array.isArray(value) ? value : [value];
837
- MixpanelReactNative.groupUnionProperty(this.token, this.groupKey, this.groupID, name, value);
838
- }
839
-
784
+ constructor(token, groupKey, groupID) {
785
+ if (!StringHelper.isValid(token)) {
786
+ StringHelper.raiseError(PARAMS.TOKEN);
787
+ }
788
+ this.token = token;
789
+ this.groupKey = groupKey;
790
+ this.groupID = groupID;
791
+ }
792
+
793
+ /**
794
+ * Sets a single property with the given name and value for this group.
795
+ * The given name and value will be assigned to the user in Mixpanel Group Analytics,
796
+ * possibly overwriting an existing property with the same name.
797
+ *
798
+ * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
799
+ * @param {string} to The value to set on the given property name. For "Zip Code", this value might be the String "90210"
800
+ */
801
+ set(prop, to) {
802
+ let properties = {};
803
+ if (ObjectHelper.isValid(prop)) {
804
+ properties = JSON.parse(JSON.stringify(prop || {}));
805
+ } else {
806
+ if (!StringHelper.isValid(prop)) {
807
+ StringHelper.raiseError(PARAMS.PROP);
808
+ }
809
+ properties[prop] = to;
810
+ }
811
+ MixpanelReactNative.groupSetProperties(
812
+ this.token,
813
+ this.groupKey,
814
+ this.groupID,
815
+ properties
816
+ );
817
+ }
818
+
819
+ /**
820
+ * Works just like groupSet() except it will not overwrite existing property values. This is useful for properties like "First login date".
821
+ *
822
+ * @param {string} prop The name of the Mixpanel property. This must be a String, for example "Zip Code"
823
+ * @param {string} to The value to set on the given property name. For "Zip Code", this value might be the String "90210"
824
+ */
825
+ setOnce(prop, to) {
826
+ let properties = {};
827
+ if (ObjectHelper.isValid(prop)) {
828
+ properties = JSON.parse(JSON.stringify(prop || {}));
829
+ } else {
830
+ if (!StringHelper.isValid(prop)) {
831
+ StringHelper.raiseError(PARAMS.PROP);
832
+ }
833
+ properties[prop] = to;
834
+ }
835
+ MixpanelReactNative.groupSetPropertyOnce(
836
+ this.token,
837
+ this.groupKey,
838
+ this.groupID,
839
+ properties
840
+ );
841
+ }
842
+
843
+ /**
844
+ * Permanently removes the property with the given name from the group's profile
845
+ *
846
+ * @param {string} prop name of a property to unset
847
+ */
848
+ unset(prop) {
849
+ if (!StringHelper.isValid(prop)) {
850
+ StringHelper.raiseError(PARAMS.PROPERTY_NAME);
851
+ }
852
+ MixpanelReactNative.groupUnsetProperty(
853
+ this.token,
854
+ this.groupKey,
855
+ this.groupID,
856
+ prop
857
+ );
858
+ }
859
+
860
+ /**
861
+ * Remove value from a list-valued property only if it is already present in the list.
862
+ * If the property does not currently exist, the remove will be ignored.
863
+ * If the property exists and is not list-valued, the remove will be ignored.
864
+ *
865
+ * @param {string} name the Group Analytics list-valued property that should have a value removed
866
+ * @param {any} value the value that will be removed from the list
867
+ */
868
+ remove(name, value) {
869
+ if (!StringHelper.isValid(name)) {
870
+ StringHelper.raiseError(PARAMS.PROPERTY_NAME);
871
+ }
872
+
873
+ MixpanelReactNative.groupRemovePropertyValue(
874
+ this.token,
875
+ this.groupKey,
876
+ this.groupID,
877
+ name,
878
+ value
879
+ );
880
+ }
881
+
882
+ /**
883
+ * Adds values to a list-valued property only if they are not already present in the list.
884
+ * If the property does not currently exist, it will be created with the given list as its value.
885
+ * If the property exists and is not list-valued, the union will be ignored.
886
+ *
887
+ * @param {string} name name of the list-valued property to set or modify
888
+ * @param {array} value an array of values to add to the property value if not already present
889
+ */
890
+ union(name, value) {
891
+ if (!StringHelper.isValid(name)) {
892
+ StringHelper.raiseError(PARAMS.PROPERTY_NAME);
893
+ }
894
+ value = Array.isArray(value) ? value : [value];
895
+ MixpanelReactNative.groupUnionProperty(
896
+ this.token,
897
+ this.groupKey,
898
+ this.groupID,
899
+ name,
900
+ value
901
+ );
902
+ }
840
903
  }
841
904
 
842
905
  class Helper {
843
- /**
906
+ /**
844
907
  Get the library data from package.json file.
845
908
  */
846
- static getMetaData() {
847
- let metadata = JSON.parse(JSON.stringify(packageJson.metadata));
848
- metadata["$lib_version"] = packageJson.version;
849
- return metadata;
850
- }
909
+ static getMetaData() {
910
+ let metadata = JSON.parse(JSON.stringify(packageJson.metadata));
911
+ metadata["$lib_version"] = packageJson.version;
912
+ return metadata;
913
+ }
851
914
 
852
- /**
915
+ /**
853
916
  Get current device platform.
854
917
  */
855
- static getDevicePlatform() {
856
- switch (Platform.OS) {
857
- case "android":
858
- return DevicePlatform.Android;
859
- case "ios":
860
- return DevicePlatform.iOS;
861
- default:
862
- return DevicePlatform.Unknown;
863
- }
918
+ static getDevicePlatform() {
919
+ switch (Platform.OS) {
920
+ case "android":
921
+ return DevicePlatform.Android;
922
+ case "ios":
923
+ return DevicePlatform.iOS;
924
+ default:
925
+ return DevicePlatform.Unknown;
864
926
  }
927
+ }
865
928
  }
866
929
 
867
930
  class StringHelper {
868
- /**
931
+ /**
869
932
  Check whether the parameter is not a blank string.
870
933
  */
871
- static isValid(str) {
872
- return typeof str === "string" && !(/^\s*$/).test(str);
873
- }
934
+ static isValid(str) {
935
+ return typeof str === "string" && !/^\s*$/.test(str);
936
+ }
874
937
 
875
- /**
938
+ /**
876
939
  Check whether the parameter is undefined or not a blank string.
877
940
  */
878
- static isValidOrUndefined(str) {
879
- return str === undefined || StringHelper.isValid(str);
880
- }
941
+ static isValidOrUndefined(str) {
942
+ return str === undefined || StringHelper.isValid(str);
943
+ }
881
944
 
882
- /**
945
+ /**
883
946
  Raise a string validation error.
884
947
  */
885
- static raiseError(paramName) {
886
- throw new Error(`${paramName}${ERROR_MESSAGE.INVALID_STRING}`);
887
- }
948
+ static raiseError(paramName) {
949
+ throw new Error(`${paramName}${ERROR_MESSAGE.INVALID_STRING}`);
950
+ }
888
951
  }
889
952
 
890
953
  class ObjectHelper {
891
- /**
954
+ /**
892
955
  Check whether the parameter is an object.
893
956
  */
894
- static isValid(obj) {
895
- return typeof obj === "object";
896
- }
957
+ static isValid(obj) {
958
+ return typeof obj === "object";
959
+ }
897
960
 
898
- /**
961
+ /**
899
962
  Check whether the parameter is undefined or an object.
900
963
  */
901
- static isValidOrUndefined(obj) {
902
- return obj === undefined || ObjectHelper.isValid(obj);
903
- }
964
+ static isValidOrUndefined(obj) {
965
+ return obj === undefined || ObjectHelper.isValid(obj);
966
+ }
904
967
 
905
- /**
968
+ /**
906
969
  Raise an object validation error.
907
970
  */
908
- static raiseError(paramName) {
909
- throw new Error(`${paramName}${ERROR_MESSAGE.INVALID_OBJECT}`);
910
- }
971
+ static raiseError(paramName) {
972
+ throw new Error(`${paramName}${ERROR_MESSAGE.INVALID_OBJECT}`);
973
+ }
911
974
  }