-
Notifications
You must be signed in to change notification settings - Fork 38
/
amplitude-snippet.js
99 lines (98 loc) · 3.65 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
93
94
95
96
97
98
99
"use strict";
/**
* 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: {}
};
if (amplitude.invoked) window.console && console.error && console.error('Amplitude snippet has been loaded.');else {
var proxy = function proxy(obj, fn) {
obj.prototype[fn] = function () {
this._q.push({
name: fn,
args: Array.prototype.slice.call(arguments, 0)
});
return this;
};
};
var getPromiseResult = function getPromiseResult(instance, fn, args) {
return function (resolve) {
instance._q.push({
name: fn,
args: Array.prototype.slice.call(args, 0),
resolve: resolve
});
};
};
var proxyInstance = function proxyInstance(instance, fn, args) {
instance._q.push({
name: fn,
args: Array.prototype.slice.call(args, 0)
});
};
var proxyMain = function proxyMain(instance, fn, isPromise) {
instance[fn] = function () {
if (isPromise) return {
promise: new Promise(getPromiseResult(instance, fn, Array.prototype.slice.call(arguments)))
};
proxyInstance(instance, fn, Array.prototype.slice.call(arguments));
};
};
var setUpProxy = function setUpProxy(instance) {
for (var k = 0; k < funcs.length; k++) {
proxyMain(instance, funcs[k], false);
}
for (var l = 0; l < funcsWithPromise.length; l++) {
proxyMain(instance, funcsWithPromise[l], true);
}
};
amplitude.invoked = true;
var as = document.createElement('script');
as.type = 'text/javascript';
as.integrity = 'sha384-18zPhxLBEI71e4+e1MPLbXFlMrz+0vYP5j9jpSRJyYwR+0/dFSAt9yTmuMvbWqn0';
as.crossOrigin = 'anonymous';
as.async = true;
as.src = 'https://cdn.amplitude.com/libs/analytics-browser-2.11.9-min.js.gz';
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);
var Identify = function Identify() {
this._q = [];
return this;
};
var identifyFuncs = ['add', 'append', 'clearAll', 'prepend', 'set', 'setOnce', 'unset', 'preInsert', 'postInsert', 'remove', 'getUserProperties'];
for (var i = 0; i < identifyFuncs.length; i++) {
proxy(Identify, identifyFuncs[i]);
}
amplitude.Identify = Identify;
var Revenue = function Revenue() {
this._q = [];
return this;
};
var revenueFuncs = ['getEventProperties', 'setProductId', 'setQuantity', 'setPrice', 'setRevenue', 'setRevenueType', 'setEventProperties'];
for (var j = 0; j < revenueFuncs.length; j++) {
proxy(Revenue, revenueFuncs[j]);
}
amplitude.Revenue = Revenue;
var funcs = ['getDeviceId', 'setDeviceId', 'getSessionId', 'setSessionId', 'getUserId', 'setUserId', 'setOptOut', 'setTransport', 'reset', 'extendSession'];
var funcsWithPromise = ['init', 'add', 'remove', 'track', 'logEvent', 'identify', 'groupIdentify', 'setGroup', 'revenue', 'flush'];
setUpProxy(amplitude);
amplitude.createInstance = function (instanceName) {
amplitude._iq[instanceName] = {
_q: []
};
setUpProxy(amplitude._iq[instanceName]);
return amplitude._iq[instanceName];
};
window.amplitude = amplitude;
}
}(window, document);