Skip to content

Commit 5e2c179

Browse files
author
Guillermo Tamanaha
committed
feat: Enable test groups for Rokt Local Launcher
1 parent 22ec8a9 commit 5e2c179

2 files changed

Lines changed: 104 additions & 25 deletions

File tree

src/Rokt-Kit.js

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -318,34 +318,42 @@ var constructor = function () {
318318
launcherOptions || {}
319319
);
320320

321-
window.Rokt.createLauncher(options)
322-
.then(function (launcher) {
323-
// Assign the launcher to a global variable for later access
324-
window.Rokt.currentLauncher = launcher;
325-
// Locally cache the launcher and filters
326-
self.launcher = launcher;
321+
if(self.isPartnerInLocalLauncherTestGroup()){
322+
var localLauncher = window.Rokt.createLocalLauncher(options);
323+
initRoktLauncher(localLauncher);
324+
}
325+
else {
326+
window.Rokt.createLauncher(options)
327+
.then(initRoktLauncher)
328+
.catch(function (err) {
329+
console.error('Error creating Rokt launcher:', err);
330+
});
331+
}
332+
}
327333

328-
var roktFilters = window.mParticle.Rokt.filters;
334+
function initRoktLauncher(launcher) {
335+
// Assign the launcher to a global variable for later access
336+
window.Rokt.currentLauncher = launcher;
337+
// Locally cache the launcher and filters
338+
self.launcher = launcher;
329339

330-
if (!roktFilters) {
331-
console.warn('Rokt Kit: No filters have been set.');
332-
} else {
333-
self.filters = roktFilters;
334-
if (!roktFilters.filteredUser) {
335-
console.warn(
336-
'Rokt Kit: No filtered user has been set.'
337-
);
338-
}
339-
}
340+
var roktFilters = window.mParticle.Rokt.filters;
340341

341-
// Kit must be initialized before attaching to the Rokt manager
342-
self.isInitialized = true;
343-
// Attaches the kit to the Rokt manager
344-
window.mParticle.Rokt.attachKit(self);
345-
})
346-
.catch(function (err) {
347-
console.error('Error creating Rokt launcher:', err);
348-
});
342+
if (!roktFilters) {
343+
console.warn('Rokt Kit: No filters have been set.');
344+
} else {
345+
self.filters = roktFilters;
346+
if (!roktFilters.filteredUser) {
347+
console.warn(
348+
'Rokt Kit: No filtered user has been set.'
349+
);
350+
}
351+
}
352+
353+
// Kit must be initialized before attaching to the Rokt manager
354+
self.isInitialized = true;
355+
// Attaches the kit to the Rokt manager
356+
window.mParticle.Rokt.attachKit(self);
349357
}
350358

351359
// mParticle Kit Callback Methods
@@ -402,6 +410,7 @@ var constructor = function () {
402410
this.setUserAttribute = setUserAttribute;
403411
this.onUserIdentified = onUserIdentified;
404412
this.removeUserAttribute = removeUserAttribute;
413+
this.isPartnerInLocalLauncherTestGroup = isPartnerInLocalLauncherTestGroup;
405414

406415
/**
407416
* Checks if the Rokt kit is ready to use.
@@ -413,6 +422,31 @@ var constructor = function () {
413422
function isKitReady() {
414423
return !!(self.isInitialized && self.launcher);
415424
}
425+
426+
function isPartnerInLocalLauncherTestGroup() {
427+
var testGroup = [382761173318339093846102813504170n];
428+
var url = new URL(window.location.href);
429+
var { hostname } = url;
430+
var hash = hashString(hostname);
431+
432+
return testGroup.includes(hash);
433+
}
434+
435+
/**
436+
* Generates a 64-bit integer hash from a string using the djb2 algorithm.
437+
* @param {string} str The string to hash.
438+
* @returns {bigint} A 64-bit BigInt representing the hash of the string.
439+
*/
440+
function hashString(str) {
441+
var hash = 5381n;
442+
443+
for (let i = 0; i < str.length; i++) {
444+
var charCode = BigInt(str.charCodeAt(i));
445+
hash = (hash << 5n) + hash + charCode;
446+
}
447+
448+
return hash;
449+
}
416450
};
417451

418452
function generateIntegrationName(customIntegrationName) {

test/src/tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('Rokt Forwarder', () => {
9898
this.sandbox = null;
9999
this.integrationName = null;
100100
this.createLauncherCalled = false;
101+
this.createLocalLauncherCalled = false;
101102

102103
this.createLauncher = function (options) {
103104
self.accountId = options.accountId;
@@ -115,6 +116,22 @@ describe('Rokt Forwarder', () => {
115116
});
116117
};
117118

119+
this.createLocalLauncher = function (options) {
120+
self.accountId = options.accountId;
121+
self.integrationName = options.integrationName;
122+
self.noFunctional = options.noFunctional;
123+
self.noTargeting = options.noTargeting;
124+
self.createLocalLauncherCalled = true;
125+
self.isInitialized = true;
126+
self.sandbox = options.sandbox;
127+
128+
return {
129+
selectPlacements: function () {},
130+
hashAttributes: function () { throw new Error('hashAttributes not implemented'); },
131+
use: function () { throw new Error('use not implemented'); },
132+
};
133+
};
134+
118135
this.currentLauncher = function () {};
119136
};
120137

@@ -544,6 +561,34 @@ describe('Rokt Forwarder', () => {
544561
},
545562
},
546563
};
564+
window.mParticle.forwarder.isPartnerInLocalLauncherTestGroup = () => false;
565+
});
566+
567+
it('should create a remote launcher if the partner is not in the local launcher test group', async () => {
568+
await window.mParticle.forwarder.init(
569+
{ accountId: '123456' },
570+
reportService.cb,
571+
true,
572+
null,
573+
{}
574+
);
575+
576+
window.mParticle.Rokt.createLauncherCalled.should.equal(true);
577+
window.mParticle.Rokt.createLocalLauncherCalled.should.equal(false);
578+
});
579+
580+
it('should create a local launcher if the partner is in the local launcher test group', async () => {
581+
window.mParticle.forwarder.isPartnerInLocalLauncherTestGroup = () => true;
582+
await window.mParticle.forwarder.init(
583+
{ accountId: '123456' },
584+
reportService.cb,
585+
true,
586+
null,
587+
{}
588+
);
589+
590+
window.mParticle.Rokt.createLauncherCalled.should.equal(false);
591+
window.mParticle.Rokt.createLocalLauncherCalled.should.equal(true);
547592
});
548593

549594
it('should call attachKit', async () => {

0 commit comments

Comments
 (0)