This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
forked from mParticle/mparticle-web-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet.js
87 lines (83 loc) · 3.1 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
(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.2;
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);
};
}
// 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';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(mp, s);
})('REPLACE WITH API KEY');