-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathsnippet.rokt.js
More file actions
130 lines (123 loc) · 4.38 KB
/
snippet.rokt.js
File metadata and controls
130 lines (123 loc) · 4.38 KB
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
(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,
Media: 9,
};
window.mParticle.eCommerce = { Cart: {} };
window.mParticle.Identity = {};
window.mParticle.Rokt = {};
window.mParticle.config = window.mParticle.config || {};
window.mParticle.config.rq = [];
window.mParticle.config.snippetVersion = 2.8;
window.mParticle.ready = function(f) {
window.mParticle.config.rq.push(f);
};
// methods to be stubbed from the main mParticle object, mParticle.eCommerce, mParticle.Identity, and mParticle.Rokt
// 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'];
var roktMethods = [
'selectPlacements',
'hashAttributes',
'hashSha256',
'setExtensionData',
'use',
'getVersion',
'terminate',
];
// 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');
});
roktMethods.forEach(function(method) {
window.mParticle.Rokt[method] = preloadMethod(method, 'Rokt');
});
// 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, identity, or rokt object, then the 1st argument is base concatenated with "." and the method name
// ie: Identity.login, eCommerce.setCurrencyCode, Rokt.selectPlacements
// 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 config = window.mParticle.config,
env = config.isDevelopmentMode ? 1 : 0,
dbUrl = '?env=' + env,
dataPlan = config.dataPlan;
if (dataPlan) {
var 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 = config.versions;
var versionQueryArray = [];
if (versions) {
Object.keys(versions).forEach(function(name) {
versionQueryArray.push(name + '=' + versions[name]);
});
}
// add Rokt script dynamically to the page, insert before the first script tag
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
window.ROKT_DOMAIN = ROKT_DOMAIN || 'https://apps.rokt-api.com';
window.mParticle.config.domain = ROKT_DOMAIN.split('//')[1];
script.src =
ROKT_DOMAIN +
'/js/v2/' +
apiKey +
'/app.js' +
dbUrl +
'&' +
versionQueryArray.join('&');
var firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
})('REPLACE WITH API KEY');