Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 59 additions & 25 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,34 +318,42 @@ var constructor = function () {
launcherOptions || {}
);

window.Rokt.createLauncher(options)
.then(function (launcher) {
// Assign the launcher to a global variable for later access
window.Rokt.currentLauncher = launcher;
// Locally cache the launcher and filters
self.launcher = launcher;
if(self.isPartnerInLocalLauncherTestGroup()){
var localLauncher = window.Rokt.createLocalLauncher(options);
initRoktLauncher(localLauncher);
}
else {
window.Rokt.createLauncher(options)
.then(initRoktLauncher)
.catch(function (err) {
console.error('Error creating Rokt launcher:', err);
});
}
}

var roktFilters = window.mParticle.Rokt.filters;
function initRoktLauncher(launcher) {

@guillermot guillermot Sep 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a reusable wrapper from createLauncher callback

// Assign the launcher to a global variable for later access
window.Rokt.currentLauncher = launcher;
// Locally cache the launcher and filters
self.launcher = launcher;

if (!roktFilters) {
console.warn('Rokt Kit: No filters have been set.');
} else {
self.filters = roktFilters;
if (!roktFilters.filteredUser) {
console.warn(
'Rokt Kit: No filtered user has been set.'
);
}
}
var roktFilters = window.mParticle.Rokt.filters;

// Kit must be initialized before attaching to the Rokt manager
self.isInitialized = true;
// Attaches the kit to the Rokt manager
window.mParticle.Rokt.attachKit(self);
})
.catch(function (err) {
console.error('Error creating Rokt launcher:', err);
});
if (!roktFilters) {
console.warn('Rokt Kit: No filters have been set.');
} else {
self.filters = roktFilters;
if (!roktFilters.filteredUser) {
console.warn(
'Rokt Kit: No filtered user has been set.'
);
}
}

// Kit must be initialized before attaching to the Rokt manager
self.isInitialized = true;
// Attaches the kit to the Rokt manager
window.mParticle.Rokt.attachKit(self);
}

// mParticle Kit Callback Methods
Expand Down Expand Up @@ -402,6 +410,7 @@ var constructor = function () {
this.setUserAttribute = setUserAttribute;
this.onUserIdentified = onUserIdentified;
this.removeUserAttribute = removeUserAttribute;
this.isPartnerInLocalLauncherTestGroup = isPartnerInLocalLauncherTestGroup;

/**
* Checks if the Rokt kit is ready to use.
Expand All @@ -413,6 +422,31 @@ var constructor = function () {
function isKitReady() {
return !!(self.isInitialized && self.launcher);
}

function isPartnerInLocalLauncherTestGroup() {
var testGroup = [382761173318339093846102813504170n];
var url = new URL(window.location.href);
var { hostname } = url;
var hash = hashString(hostname);

return testGroup.includes(hash);
}

/**
* Generates a 64-bit integer hash from a string using the djb2 algorithm.
* @param {string} str The string to hash.
* @returns {bigint} A 64-bit BigInt representing the hash of the string.
*/
function hashString(str) {
var hash = 5381n;

for (let i = 0; i < str.length; i++) {
var charCode = BigInt(str.charCodeAt(i));
hash = (hash << 5n) + hash + charCode;
}

return hash;
}
};

function generateIntegrationName(customIntegrationName) {
Expand Down
45 changes: 45 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
this.sandbox = null;
this.integrationName = null;
this.createLauncherCalled = false;
this.createLocalLauncherCalled = false;

this.createLauncher = function (options) {
self.accountId = options.accountId;
Expand All @@ -115,6 +116,22 @@
});
};

this.createLocalLauncher = function (options) {
self.accountId = options.accountId;
self.integrationName = options.integrationName;
self.noFunctional = options.noFunctional;
self.noTargeting = options.noTargeting;
self.createLocalLauncherCalled = true;
self.isInitialized = true;
self.sandbox = options.sandbox;

return {
selectPlacements: function () {},
hashAttributes: function () { throw new Error('hashAttributes not implemented'); },

Check failure on line 130 in test/src/tests.js

View workflow job for this annotation

GitHub Actions / Run Web Kit PR Workflow / Build and Test

Replace `·throw·new·Error('hashAttributes·not·implemented');` with `⏎····················throw·new·Error('hashAttributes·not·implemented');⏎···············`
use: function () { throw new Error('use not implemented'); },

Check failure on line 131 in test/src/tests.js

View workflow job for this annotation

GitHub Actions / Run Web Kit PR Workflow / Build and Test

Replace `·throw·new·Error('use·not·implemented');` with `⏎····················throw·new·Error('use·not·implemented');⏎···············`
};
};

this.currentLauncher = function () {};
};

Expand Down Expand Up @@ -544,6 +561,34 @@
},
},
};
window.mParticle.forwarder.isPartnerInLocalLauncherTestGroup = () => false;

Check failure on line 564 in test/src/tests.js

View workflow job for this annotation

GitHub Actions / Run Web Kit PR Workflow / Build and Test

Insert `⏎···············`
});

it('should create a remote launcher if the partner is not in the local launcher test group', async () => {
await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

window.mParticle.Rokt.createLauncherCalled.should.equal(true);
window.mParticle.Rokt.createLocalLauncherCalled.should.equal(false);
});

it('should create a local launcher if the partner is in the local launcher test group', async () => {
window.mParticle.forwarder.isPartnerInLocalLauncherTestGroup = () => true;

Check failure on line 581 in test/src/tests.js

View workflow job for this annotation

GitHub Actions / Run Web Kit PR Workflow / Build and Test

Insert `⏎···············`
await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

window.mParticle.Rokt.createLauncherCalled.should.equal(false);
window.mParticle.Rokt.createLocalLauncherCalled.should.equal(true);
});

it('should call attachKit', async () => {
Expand Down
Loading