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