-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathamplitude-snippet.js
92 lines (92 loc) · 2.92 KB
/
amplitude-snippet.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
/**
* Imported in client browser via <script> tag
* Async capabilities: Interally creates stubbed window.amplitude object until real SDK loaded
* Stubbed functions keep track of funciton calls and their arguments
* These are sent once real SDK loaded through another <script> tag
*/
(function (window, document) {
var amplitude = window.amplitude || { _q: [], _iq: {} };
var as = document.createElement('script');
as.type = 'text/javascript';
// Don't edit as.integrity, it is tracked by semantic-release-bot during releases
as.integrity = 'sha384-BFhCFljbuMkiF3EzVMjFk+zh1dxo9ckGpYDTPLhKibORvR5LPenaPdnGFD9VNZJr';
as.crossOrigin = 'anonymous';
as.async = true;
// Don't edit as.src, it is tracked by semantic-release-bot during releases
as.src = 'https://cdn.amplitude.com/libs/amplitude-7.3.3-min.gz.js';
as.onload = function () {
if (!window.amplitude.runQueuedFunctions) {
console.log('[Amplitude] Error: could not load SDK');
}
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
function proxy(obj, fn) {
obj.prototype[fn] = function () {
this._q.push([fn].concat(Array.prototype.slice.call(arguments, 0)));
return this;
};
}
var Identify = function () {
this._q = [];
return this;
};
var identifyFuncs = ['add', 'append', 'clearAll', 'prepend', 'set', 'setOnce', 'unset'];
for (var i = 0; i < identifyFuncs.length; i++) {
proxy(Identify, identifyFuncs[i]);
}
amplitude.Identify = Identify;
var Revenue = function () {
this._q = [];
return this;
};
var revenueFuncs = ['setProductId', 'setQuantity', 'setPrice', 'setRevenueType', 'setEventProperties'];
for (var j = 0; j < revenueFuncs.length; j++) {
proxy(Revenue, revenueFuncs[j]);
}
amplitude.Revenue = Revenue;
var funcs = [
'init',
'logEvent',
'logRevenue',
'setUserId',
'setUserProperties',
'setOptOut',
'setVersionName',
'setDomain',
'setDeviceId',
'enableTracking',
'setGlobalUserProperties',
'identify',
'clearUserProperties',
'setGroup',
'logRevenueV2',
'regenerateDeviceId',
'groupIdentify',
'onInit',
'logEventWithTimestamp',
'logEventWithGroups',
'setSessionId',
'resetSessionId',
];
function setUpProxy(instance) {
function proxyMain(fn) {
instance[fn] = function () {
instance._q.push([fn].concat(Array.prototype.slice.call(arguments, 0)));
};
}
for (var k = 0; k < funcs.length; k++) {
proxyMain(funcs[k]);
}
}
setUpProxy(amplitude);
amplitude.getInstance = function (instance) {
instance = (!instance || instance.length === 0 ? '$default_instance' : instance).toLowerCase();
if (!amplitude._iq.hasOwnProperty(instance)) {
amplitude._iq[instance] = { _q: [] };
setUpProxy(amplitude._iq[instance]);
}
return amplitude._iq[instance];
};
window.amplitude = amplitude;
})(window, document);