-
Notifications
You must be signed in to change notification settings - Fork 2
/
rakam.native.js
1592 lines (1283 loc) · 46.1 KB
/
rakam.native.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
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.rakam = factory());
}(this, (function () { 'use strict';
/* jshint bitwise: false */
/*
* UTF-8 encoder/decoder
* http://www.webtoolkit.info/
*/
var UTF8 = {
encode: function encode(s) {
var utftext = '';
for (var n = 0; n < s.length; n++) {
var c = s.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if (c > 127 && c < 2048) {
utftext += String.fromCharCode(c >> 6 | 192);
utftext += String.fromCharCode(c & 63 | 128);
} else {
utftext += String.fromCharCode(c >> 12 | 224);
utftext += String.fromCharCode(c >> 6 & 63 | 128);
utftext += String.fromCharCode(c & 63 | 128);
}
}
return utftext;
},
decode: function decode(utftext) {
var s = '';
var i = 0;
var c = 0,
c1 = 0,
c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
s += String.fromCharCode(c);
i++;
} else if (c > 191 && c < 224) {
c1 = utftext.charCodeAt(i + 1);
s += String.fromCharCode((c & 31) << 6 | c1 & 63);
i += 2;
} else {
c1 = utftext.charCodeAt(i + 1);
c2 = utftext.charCodeAt(i + 2);
s += String.fromCharCode((c & 15) << 12 | (c1 & 63) << 6 | c2 & 63);
i += 3;
}
}
return s;
}
};
/* jshint bitwise: false */
/*
* Base64 encoder/decoder
* http://www.webtoolkit.info/
*/
var Base64 = {
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
encode: function encode(input) {
try {
if (window.btoa && window.atob) {
return window.btoa(unescape(encodeURIComponent(input)));
}
} catch (e) {//log(e);
}
return Base64._encode(input);
},
_encode: function _encode(input) {
var output = '';
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = UTF8.encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) + Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
}
return output;
},
decode: function decode(input) {
try {
if (window.btoa && window.atob) {
return decodeURIComponent(escape(window.atob(input)));
}
} catch (e) {//log(e);
}
return Base64._decode(input);
},
_decode: function _decode(input) {
var output = '';
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
while (i < input.length) {
enc1 = Base64._keyStr.indexOf(input.charAt(i++));
enc2 = Base64._keyStr.indexOf(input.charAt(i++));
enc3 = Base64._keyStr.indexOf(input.charAt(i++));
enc4 = Base64._keyStr.indexOf(input.charAt(i++));
chr1 = enc1 << 2 | enc2 >> 4;
chr2 = (enc2 & 15) << 4 | enc3 >> 2;
chr3 = (enc3 & 3) << 6 | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 !== 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3);
}
}
output = UTF8.decode(output);
return output;
}
};
// A URL safe variation on the the list of Base64 characters
var base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var base64Id = function base64Id() {
var str = '';
for (var i = 0; i < 22; ++i) {
str += base64Chars.charAt(Math.floor(Math.random() * 64));
}
return str;
};
var get = function get(name) {
try {
var ca = document.cookie.split(';');
var value = null;
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(name) === 0) {
value = c.substring(name.length, c.length);
break;
}
}
return value;
} catch (e) {
return null;
}
};
var set = function set(name, value, opts) {
var expires = value !== null ? opts.expirationDays : -1;
if (expires) {
var date = new Date();
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
expires = date;
}
var str = name + '=' + value;
if (expires) {
str += '; expires=' + expires.toUTCString();
}
str += '; path=/';
if (opts.domain) {
str += '; domain=' + opts.domain;
}
if (opts.secure) {
str += '; Secure';
}
if (opts.sameSite) {
str += '; SameSite=' + opts.sameSite;
}
document.cookie = str;
}; // test that cookies are enabled - navigator.cookiesEnabled yields false positives in IE, need to test directly
var areCookiesEnabled = function areCookiesEnabled() {
var uid = String(new Date());
try {
var cookieName = 'rakam' + base64Id();
set(cookieName, uid, {});
var _areCookiesEnabled = get(cookieName + '=') === uid;
set(cookieName, null, {});
return _areCookiesEnabled;
} catch (e) {}
return false;
};
var baseCookie = {
set: set,
get: get,
areCookiesEnabled: areCookiesEnabled
};
var getHost = function getHost(url) {
var a = document.createElement('a');
a.href = url;
return a.hostname || location.hostname;
};
var topDomain = function topDomain(url) {
var host = getHost(url);
var parts = host.split('.');
var levels = [];
var cname = '_tldtest_' + base64Id();
for (var i = parts.length - 2; i >= 0; --i) {
levels.push(parts.slice(i).join('.'));
}
for (var _i = 0; _i < levels.length; ++_i) {
var domain = levels[_i];
var opts = {
domain: '.' + domain
};
baseCookie.set(cname, 1, opts);
if (baseCookie.get(cname)) {
baseCookie.set(cname, null, opts);
return domain;
}
}
return '';
};
/*
* Cookie data
*/
var _options = {
expirationDays: undefined,
domain: undefined
};
var reset = function reset() {
_options = {};
};
var options = function options(opts) {
if (arguments.length === 0) {
return _options;
}
opts = opts || {};
_options.expirationDays = opts.expirationDays;
var domain = opts.domain !== undefined ? opts.domain : '.' + topDomain(window.location.href);
var token = Math.random();
_options.domain = domain;
set$1('rakam_test', token);
var stored = get$1('rakam_test');
if (!stored || stored !== token) {
domain = null;
}
remove('rakam_test');
_options.domain = domain;
};
var _domainSpecific = function _domainSpecific(name) {
// differentiate between cookies on different domains
var suffix = '';
if (_options.domain) {
suffix = _options.domain.charAt(0) === '.' ? _options.domain.substring(1) : _options.domain;
}
return name + suffix;
};
var get$1 = function get(name) {
try {
var nameEq = _domainSpecific(name) + '=';
var ca = document.cookie.split(';');
var value = null;
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEq) === 0) {
value = c.substring(nameEq.length, c.length);
break;
}
}
if (value) {
return JSON.parse(Base64.decode(value));
}
return null;
} catch (e) {
return null;
}
};
var set$1 = function set(name, value) {
try {
_set(_domainSpecific(name), Base64.encode(JSON.stringify(value)), _options);
return true;
} catch (e) {
return false;
}
};
var _set = function _set(name, value, opts) {
var expires = value !== null ? opts.expirationDays : -1;
if (expires) {
var date = new Date();
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
expires = date;
}
var str = name + '=' + value;
if (expires) {
str += '; expires=' + expires.toUTCString();
}
str += '; path=/';
if (opts.domain) {
str += '; domain=' + opts.domain;
}
document.cookie = str;
};
var remove = function remove(name) {
try {
_set(_domainSpecific(name), null, _options);
return true;
} catch (e) {
return false;
}
};
var Cookie = {
reset: reset,
options: options,
get: get$1,
set: set$1,
remove: remove
};
var getLanguage = function getLanguage() {
return navigator && (navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage) || undefined;
};
var language = {
language: getLanguage()
};
/* jshint -W020, unused: false, noempty: false, boss: true */
/*
* Implement localStorage to support Firefox 2-3 and IE 5-7
*/
var localStorage; // jshint ignore:line
// test that Window.localStorage is available and works
function windowLocalStorageAvailable() {
var uid = new Date();
var result;
try {
window.localStorage.setItem(uid, uid);
result = window.localStorage.getItem(uid) === String(uid);
window.localStorage.removeItem(uid);
return result;
} catch (e) {// localStorage not available
}
return false;
}
if (windowLocalStorageAvailable()) {
localStorage = window.localStorage;
} else if (window.globalStorage) {
// Firefox 2-3 use globalStorage
// See https://developer.mozilla.org/en/dom/storage#globalStorage
try {
localStorage = window.globalStorage[window.location.hostname];
} catch (e) {// Something bad happened...
}
} else {
// IE 5-7 use userData
// See http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx
var div = document.createElement('div'),
attrKey = 'localStorage';
div.style.display = 'none';
document.getElementsByTagName('head')[0].appendChild(div);
if (div.addBehavior) {
div.addBehavior('#default#userdata');
localStorage = {
length: 0,
setItem: function setItem(k, v) {
div.load(attrKey);
if (!div.getAttribute(k)) {
this.length++;
}
div.setAttribute(k, v);
div.save(attrKey);
},
getItem: function getItem(k) {
div.load(attrKey);
return div.getAttribute(k);
},
removeItem: function removeItem(k) {
div.load(attrKey);
if (div.getAttribute(k)) {
this.length--;
}
div.removeAttribute(k);
div.save(attrKey);
},
clear: function clear() {
div.load(attrKey);
var i = 0;
var attr;
while (attr = div.XMLDocument.documentElement.attributes[i++]) {
div.removeAttribute(attr.name);
}
div.save(attrKey);
this.length = 0;
},
key: function key(k) {
div.load(attrKey);
return div.XMLDocument.documentElement.attributes[k];
}
};
div.load(attrKey);
localStorage.length = div.XMLDocument.documentElement.attributes.length;
}
}
if (!localStorage) {
localStorage = {
length: 0,
setItem: function setItem(k, v) {},
getItem: function getItem(k) {},
removeItem: function removeItem(k) {},
clear: function clear() {},
key: function key(k) {}
};
}
var localStorage$1 = localStorage;
var object = {};
var has = object.hasOwnProperty;
function merge(a, b) {
for (var key in b) {
if (has.call(b, key)) {
a[key] = b[key];
}
}
return a;
}
/*
* Simple AJAX request object
*/
var Request = function Request(url, data, headers) {
this.url = url;
this.data = data || {};
this.headers = headers || {};
};
function parseResponseHeaders(headerStr) {
var headers = {};
if (!headerStr) {
return headers;
}
var headerPairs = headerStr.split("\r\n");
for (var i = 0; i < headerPairs.length; i++) {
var headerPair = headerPairs[i]; // Can't use split() here because it does the wrong thing
// if the header value has the string ": " in it.
var index = headerPair.indexOf(": ");
if (index > 0) {
var key = headerPair.substring(0, index);
var val = headerPair.substring(index + 2);
headers[key] = val;
}
}
return headers;
}
Request.prototype.send = function (callback) {
var isIE = window.XDomainRequest ? true : false;
if (isIE) {
var xdr = new window.XDomainRequest();
xdr.open('POST', this.url, true);
xdr.onload = function () {
callback(xdr.responseText);
};
xdr.send(JSON.stringify(this.data));
} else {
var xhr = new XMLHttpRequest();
xhr.withCredentials = "true";
xhr.open('POST', this.url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
callback(xhr.status, xhr.responseText, parseResponseHeaders(xhr.getAllResponseHeaders()));
}
};
xhr.setRequestHeader('Content-Type', 'text/plain');
for (var key in this.headers) {
if (this.headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, this.headers[key]);
}
}
xhr.send(JSON.stringify(this.data));
}
};
/* jshint bitwise: false, laxbreak: true */
/**
* Taken straight from jed's gist: https://gist.github.com/982883
*
* Returns a random v4 UUID of the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
* where each x is replaced with a random hexadecimal digit from 0 to f, and
* y is replaced with a random hexadecimal digit from 8 to b.
*/
var uuid = function uuid(a) {
return a // if the placeholder was passed, return
? ( // a random number from 0 to 15
a ^ // unless b is 8,
Math.random() // in which case
* 16 // a random number from
>> a / 4 // 8 to 11
).toString(16) // in hexadecimal
: ( // or otherwise a concatenated string:
[1e7] + // 10000000 +
-1e3 + // -1000 +
-4e3 + // -4000 +
-8e3 + // -80000000 +
-1e11 // -100000000000,
).replace( // replacing
/[018]/g, // zeroes, ones, and eights with
uuid // random hex digits
);
};
var version = '2.6.0';
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
/* Taken from: https://github.com/component/type */
/**
* toString ref.
*/
var toString = Object.prototype.toString;
/**
* Return the type of `val`.
*
* @param {Mixed} val
* @return {String}
* @api public
*/
function type (val) {
switch (toString.call(val)) {
case '[object Date]':
return 'date';
case '[object RegExp]':
return 'regexp';
case '[object Arguments]':
return 'arguments';
case '[object Array]':
return 'array';
case '[object Error]':
return 'error';
}
if (val === null) {
return 'null';
}
if (val === undefined) {
return 'undefined';
}
if (val !== val) {
return 'nan';
}
if (val && val.nodeType === 1) {
return 'element';
}
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(val)) {
return 'buffer';
}
val = val.valueOf ? val.valueOf() : Object.prototype.valueOf.apply(val);
return _typeof(val);
}
/*
* Wrapper for a user properties JSON object that supports operations.
* Note: if a user property is used in multiple operations on the same User object,
* only the first operation will be saved, and the rest will be ignored.
*/
var API_VERSION = 1;
var wrapCallback = function wrapCallback(operation, props, callback) {
return function (status, response, headers) {
if (callback !== undefined) {
callback(status, response, headers);
}
};
};
var getUrl = function getUrl(options) {
return ('https:' === window.location.protocol ? 'https' : 'http') + '://' + options.apiEndpoint + "/user";
};
var User = function User() {};
User.prototype.init = function (options) {
this.options = options;
};
User.prototype.set = function (properties, callback) {
new Request(getUrl(this.options) + "/set_properties", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
},
id: this.options.userId,
properties: properties
}).send(wrapCallback("set_properties", properties, callback));
return this;
};
User.prototype.setOnce = function (properties, callback) {
new Request(getUrl(this.options) + "/set_properties_once", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
},
id: this.options.userId,
properties: properties
}).send(wrapCallback("set_properties_once", properties, callback));
return this;
};
User.prototype.increment = function (property, value, callback) {
new Request(getUrl(this.options) + "/increment_property", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
},
id: this.options.userId,
property: property,
value: value
}).send(wrapCallback("increment_property", property + " by " + value, callback));
return this;
};
User.prototype.unset = function (properties, callback) {
new Request(getUrl(this.options) + "/unset_properties", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
},
id: this.options.userId,
properties: type(properties) === "array" ? properties : [properties]
}).send(wrapCallback("unset_properties", properties, callback));
return this;
};
var API_VERSION$1 = 1;
var DEFAULT_OPTIONS = {
apiEndpoint: 'managed.getrakam.com',
eventEndpointPath: '/event/batch',
cookieExpiration: 365 * 10,
cookieName: 'rakam_id',
domain: undefined,
includeUtm: false,
trackForms: false,
language: language.language,
optOut: false,
platform: 'Web',
savedMaxCount: 1000,
saveEvents: true,
sessionTimeout: 30 * 60 * 1000,
unsentKey: 'rakam_unsent',
uploadBatchSize: 100,
batchEvents: false,
eventUploadThreshold: 30,
eventUploadPeriodMillis: 30 * 1000,
// 30s,
useLocalStorageForSessionization: true
};
var StorageKeys = {
LAST_ID: 'rakam_lastEventId',
LAST_EVENT_TIME: 'rakam_lastEventTime',
SESSION_ID: 'rakam_sessionId',
RETURNING_SESSION: 'rakam_returning'
};
var getSessionItem = function getSessionItem(options, key) {
if (options.useLocalStorageForSessionization) {
return localStorage$1.getItem(key);
} else {
return Cookie.get(key);
}
};
var setSessionItem = function setSessionItem(options, key, value) {
if (options.useLocalStorageForSessionization) {
localStorage$1.setItem(key, value);
} else {
Cookie.set(key, value);
}
};
/*
* Rakam API
*/
var Rakam = function Rakam() {
this._unsentEvents = [];
this.options = merge({}, DEFAULT_OPTIONS);
};
Rakam.prototype._eventId = 0;
Rakam.prototype._returningUser = false;
Rakam.prototype._sending = false;
Rakam.prototype._lastEventTime = null;
Rakam.prototype._sessionId = null;
Rakam.prototype._newSession = false;
Rakam.prototype.log = function (s) {
if (this.options.debug === true) {
console.log('[Rakam] ' + s);
}
};
/**
* Initializes Rakam.
* apiKey The API Key for your app
* opt_userId An identifier for this user
* opt_config Configuration options
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
*/
Rakam.prototype.init = function (apiKey, opt_userId, opt_config, callback) {
try {
if (!apiKey) {
throw new Error('apiKey is null');
}
this.options.apiKey = apiKey;
var user = new User();
user.init(this.options);
this.User = function () {
return user;
};
if (opt_config) {
this.options.apiEndpoint = opt_config.apiEndpoint || this.options.apiEndpoint;
this.options.debug = opt_config.debug || this.options.debug === true;
if (opt_config.saveEvents !== undefined) {
this.options.saveEvents = !!opt_config.saveEvents;
}
if (opt_config.domain !== undefined) {
this.options.domain = opt_config.domain;
}
if (opt_config.includeUtm !== undefined) {
this.options.includeUtm = !!opt_config.includeUtm;
}
if (opt_config.trackClicks !== undefined) {
this.options.trackClicks = !!opt_config.trackClicks;
}
if (opt_config.trackForms !== undefined) {
this.options.trackForms = !!opt_config.trackForms;
}
if (opt_config.batchEvents !== undefined) {
this.options.batchEvents = !!opt_config.batchEvents;
}
this.options.platform = opt_config.platform || this.options.platform;
this.options.useLocalStorageForSessionization = opt_config.useLocalStorageForSessionization !== undefined ? opt_config.useLocalStorageForSessionization : this.options.useLocalStorageForSessionization;
this.options.language = opt_config.language || this.options.language;
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
this.options.uploadBatchSize = opt_config.uploadBatchSize || this.options.uploadBatchSize;
this.options.eventUploadThreshold = opt_config.eventUploadThreshold || this.options.eventUploadThreshold;
this.options.savedMaxCount = opt_config.savedMaxCount || this.options.savedMaxCount;
this.options.eventUploadPeriodMillis = opt_config.eventUploadPeriodMillis || this.options.eventUploadPeriodMillis;
this.options.superProperties = opt_config.superProperties || [];
}
Cookie.options({
expirationDays: this.options.cookieExpiration,
domain: this.options.domain
});
this.options.domain = Cookie.options().domain;
_loadCookieData(this);
if (opt_config && opt_config.deviceId !== undefined && opt_config.deviceId !== null && opt_config.deviceId || this.options.deviceId) {
this.options.deviceId = this.options.deviceId;
} else {
this.deviceIdCreatedAt = new Date();
this.options.deviceId = uuid();
}
_saveCookieData(this);
this.log('initialized with apiKey=' + apiKey);
if (this.options.saveEvents) {
var savedUnsentEventsString = localStorage$1.getItem(this.options.unsentKey);
if (savedUnsentEventsString) {
try {
this._unsentEvents = JSON.parse(savedUnsentEventsString);
} catch (e) {
this.log(e);
}
}
}
this._sendEventsIfReady();
if (this.options.includeUtm) {
this._initUtmData();
}
if (this.options.trackForms) {
this._initTrackForms();
}
if (this.options.trackClicks) {
this._initTrackClicks();
}
this._lastEventTime = parseInt(getSessionItem(this.options, StorageKeys.LAST_EVENT_TIME)) || null;
this._sessionId = parseInt(getSessionItem(this.options, StorageKeys.SESSION_ID)) || null;
this._eventId = localStorage$1.getItem(StorageKeys.LAST_ID) || 0;
var now = new Date().getTime();
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
if (this._sessionId !== null) {
setSessionItem(this.options, StorageKeys.RETURNING_SESSION, true);
this._returningUser = true;
}
this._sessionId = now;
setSessionItem(this.options, StorageKeys.SESSION_ID, this._sessionId);
} else {
this._returningUser = getSessionItem(this.options, StorageKeys.RETURNING_SESSION) === 'true';
}
this._lastEventTime = now;
setSessionItem(this.options, StorageKeys.LAST_EVENT_TIME, this._lastEventTime);
} catch (e) {
this.log(e);
}
this.setUserId(opt_userId);
if (callback && typeof callback === 'function') {
setTimeout(function () {
callback();
}, 1);
}
};
Rakam.prototype.onEvent = function (callback) {
this.options.eventCallbacks = this.options.eventCallbacks || [];
this.options.eventCallbacks.push(callback);
};
var transformValue = function transformValue(_this, attribute, value, type) {
if (type !== null) {
type = type.toLowerCase();
}
if (type === 'long' || type === 'time' || type === 'timestamp' || type === 'date') {
value = parseInt(value);
if (isNaN(value) || !isFinite(value)) {
_this.log('ignoring ' + attribute + ': the value must be a number');
value = null;
}
} else if (type === 'double') {
value = parseFloat(value);
if (isNaN(value) || !isFinite(value)) {
_this.log('ignoring ' + attribute + ': the value is not double');
value = null;
}
} else if (type === 'boolean') {