Skip to content

Commit b1ff2d3

Browse files
chore: Add Rokt Snippet (#1042)
1 parent 2dfb756 commit b1ff2d3

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test/test-bundle.js.map
88
test/stub/test-stub-bundle.js
99
**/dist
1010
snippet.min.js
11+
snippet.rokt.min.js
1112
test/lib
1213
mparticle.js
1314
test/

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"build:npm": "cross-env ENVIRONMENT=prod BUILD=cjs rollup --config rollup.config.js",
2929
"build:esm": "cross-env ENVIRONMENT=prod BUILD=esm rollup --config rollup.config.js",
3030
"build:stub": "cross-env ENVIRONMENT=prod BUILD=stub rollup --config rollup.config.js",
31-
"build:snippet": "uglifyjs snippet.js -m -o snippet.min.js",
31+
"build:snippet": "npm run build:snippet:mp && npm run build:snippet:rokt",
32+
"build:snippet:mp": "uglifyjs snippet.js -m -o snippet.min.js",
33+
"build:snippet:rokt": "uglifyjs snippet.rokt.js -m -o snippet.rokt.min.js",
3234
"build:tests": "cross-env ENVIRONMENT=prod TESTTYPE=main rollup --config rollup.test.config.js",
3335
"build:test-bundle": "cross-env TESTTYPE=main ENVIRONMENT=prod rollup --config rollup.test.config.js",
3436
"build:browserify:cjs": "browserify test/integrations/cjs/browserify/index.js -o test/integrations/cjs/dist/browserify-output.js",

snippet.rokt.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
(function(apiKey) {
2+
// stub mParticle and major mParticle classes EventType, eCommerce, and Identity to exist before full
3+
// mParticle object is initialized
4+
window.mParticle = window.mParticle || {};
5+
window.mParticle.EventType = {
6+
Unknown: 0,
7+
Navigation: 1,
8+
Location: 2,
9+
Search: 3,
10+
Transaction: 4,
11+
UserContent: 5,
12+
UserPreference: 6,
13+
Social: 7,
14+
Other: 8,
15+
Media: 9,
16+
};
17+
window.mParticle.eCommerce = { Cart: {} };
18+
window.mParticle.Identity = {};
19+
window.mParticle.Rokt = {};
20+
window.mParticle.config = window.mParticle.config || {};
21+
window.mParticle.config.rq = [];
22+
window.mParticle.config.snippetVersion = 2.6;
23+
window.mParticle.ready = function(f) {
24+
window.mParticle.config.rq.push(f);
25+
};
26+
27+
// methods to be stubbed from the main mParticle object, mParticle.eCommerce, mParticle.Identity, and mParticle.Rokt
28+
// methods that return objects are not stubbed
29+
var mainMethods = [
30+
'endSession',
31+
'logError',
32+
'logBaseEvent',
33+
'logEvent',
34+
'logForm',
35+
'logLink',
36+
'logPageView',
37+
'setSessionAttribute',
38+
'setAppName',
39+
'setAppVersion',
40+
'setOptOut',
41+
'setPosition',
42+
'startNewSession',
43+
'startTrackingLocation',
44+
'stopTrackingLocation',
45+
];
46+
var ecommerceMethods = ['setCurrencyCode', 'logCheckout'];
47+
var identityMethods = ['identify', 'login', 'logout', 'modify'];
48+
var roktMethods = [
49+
'selectPlacements',
50+
'hashAttributes',
51+
'setExtensionData',
52+
];
53+
54+
// iterates through methods above to create stubs
55+
mainMethods.forEach(function(method) {
56+
window.mParticle[method] = preloadMethod(method);
57+
});
58+
ecommerceMethods.forEach(function(method) {
59+
window.mParticle.eCommerce[method] = preloadMethod(method, 'eCommerce');
60+
});
61+
identityMethods.forEach(function(method) {
62+
window.mParticle.Identity[method] = preloadMethod(method, 'Identity');
63+
});
64+
roktMethods.forEach(function(method) {
65+
window.mParticle.Rokt[method] = preloadMethod(method, 'Rokt');
66+
});
67+
68+
// stubbing function
69+
// pushes an array of 2 arguments into readyQueue: 1. the method, and 2. the arguments passed to the method
70+
// if the method is on the eCommerce, identity, or rokt object, then the 1st argument is base concatenated with "." and the method name
71+
// ie: Identity.login, eCommerce.setCurrencyCode, Rokt.selectPlacements
72+
// in main.js, the function "processPreloadedItem" will parse and run stubbed methods stored in the readyQueue (config.rq)
73+
function preloadMethod(method, base) {
74+
return function() {
75+
if (base) {
76+
method = base + '.' + method;
77+
}
78+
var args = Array.prototype.slice.call(arguments);
79+
args.unshift(method);
80+
window.mParticle.config.rq.push(args);
81+
};
82+
}
83+
84+
// set data planning query parameters
85+
var config = window.mParticle.config,
86+
env = config.isDevelopmentMode ? 1 : 0,
87+
dbUrl = '?env=' + env,
88+
dataPlan = config.dataPlan;
89+
90+
if (dataPlan) {
91+
var dpId = dataPlan.planId,
92+
dpV = dataPlan.planVersion;
93+
if (dpId) {
94+
if (dpV && (dpV < 1 || dpV > 1000)) {
95+
dpV = null;
96+
}
97+
dbUrl += '&plan_id=' + dpId + (dpV ? '&plan_version=' + dpV : '');
98+
}
99+
}
100+
101+
// set version query parameters
102+
var versions = config.versions;
103+
var versionQueryArray = [];
104+
if (versions) {
105+
Object.keys(versions).forEach(function(name) {
106+
versionQueryArray.push(name + '=' + versions[name]);
107+
});
108+
}
109+
110+
// add Rokt script dynamically to the page, insert before the first script tag
111+
var script = document.createElement('script');
112+
script.type = 'text/javascript';
113+
script.async = true;
114+
window.ROKT_DOMAIN = ROKT_DOMAIN || 'https://apps.rokt-api.com';
115+
window.mParticle.config.domain = ROKT_DOMAIN.split('//')[1];
116+
script.src =
117+
ROKT_DOMAIN +
118+
'/js/v2/' +
119+
apiKey +
120+
'/app.js' +
121+
dbUrl +
122+
'&' +
123+
versionQueryArray.join('&');
124+
var firstScript = document.getElementsByTagName('script')[0];
125+
firstScript.parentNode.insertBefore(script, firstScript);
126+
})('REPLACE WITH API KEY');

snippet.rokt.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)