-
Notifications
You must be signed in to change notification settings - Fork 44
/
snippet.js
118 lines (111 loc) · 3.97 KB
/
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function(apiKey) {
// stub mParticle and major mParticle classes EventType, eCommerce, and Identity to exist before full
// mParticle object is initialized
window.mParticle = window.mParticle || {};
window.mParticle.EventType = {
Unknown: 0,
Navigation: 1,
Location: 2,
Search: 3,
Transaction: 4,
UserContent: 5,
UserPreference: 6,
Social: 7,
Other: 8,
};
window.mParticle.eCommerce = { Cart: {} };
window.mParticle.Identity = {};
window.mParticle.config = window.mParticle.config || {};
window.mParticle.config.rq = [];
window.mParticle.config.snippetVersion = 2.3;
window.mParticle.ready = function(f) {
window.mParticle.config.rq.push(f);
};
// methods to be stubbed from the main mParticle object, mParticle.eCommerce, and mParticle.Identity
// methods that return objects are not stubbed
var mainMethods = [
'endSession',
'logError',
'logBaseEvent',
'logEvent',
'logForm',
'logLink',
'logPageView',
'setSessionAttribute',
'setAppName',
'setAppVersion',
'setOptOut',
'setPosition',
'startNewSession',
'startTrackingLocation',
'stopTrackingLocation',
];
var ecommerceMethods = ['setCurrencyCode', 'logCheckout'];
var identityMethods = ['identify', 'login', 'logout', 'modify'];
// iterates through methods above to create stubs
mainMethods.forEach(function(method) {
window.mParticle[method] = preloadMethod(method);
});
ecommerceMethods.forEach(function(method) {
window.mParticle.eCommerce[method] = preloadMethod(method, 'eCommerce');
});
identityMethods.forEach(function(method) {
window.mParticle.Identity[method] = preloadMethod(method, 'Identity');
});
// stubbing function
// pushes an array of 2 arguments into readyQueue: 1. the method, and 2. the arguments passed to the method
// if the method is on the eCommerce or identity object, then the 1st argument is base conatenated with "." and the method name
// ie: Identity.login, eCommerce.setCurrencyCode
// in main.js, the function "processPreloadedItem" will parse and run stubbed methods stored in the readyQueue (config.rq)
function preloadMethod(method, base) {
return function() {
if (base) {
method = base + '.' + method;
}
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
window.mParticle.config.rq.push(args);
};
}
// set data planning query parameters
var dpId,
dpV,
config = window.mParticle.config,
env = config.isDevelopmentMode ? 1 : 0,
dbUrl = '?env=' + env,
dataPlan = window.mParticle.config.dataPlan;
if (dataPlan) {
dpId = dataPlan.planId;
dpV = dataPlan.planVersion;
if (dpId) {
if (dpV && (dpV < 1 || dpV > 1000)) {
dpV = null;
}
dbUrl += '&plan_id=' + dpId + (dpV ? '&plan_version=' + dpV : '');
}
}
// set version query parameters
var versions = window.mParticle.config.versions;
var versionQueryArray = [];
if (versions) {
Object.keys(versions).forEach(function(name) {
versionQueryArray.push(name + '=' + versions[name]);
});
}
// add mParticle script dynamically to the page, insert before the first script tag
var mp = document.createElement('script');
mp.type = 'text/javascript';
mp.async = true;
mp.src =
('https:' == document.location.protocol
? 'https://jssdkcdns'
: 'http://jssdkcdn') +
'.mparticle.com/js/v2/' +
apiKey +
'/mparticle.js' +
dbUrl +
'&' +
versionQueryArray.join('&');
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(mp, s);
})('REPLACE WITH API KEY');