Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
64 changes: 39 additions & 25 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,34 +318,39 @@ 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 (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 +407,7 @@ var constructor = function () {
this.setUserAttribute = setUserAttribute;
this.onUserIdentified = onUserIdentified;
this.removeUserAttribute = removeUserAttribute;
this.testGroup = [403659147];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

since this will be more soon, let's make it plural

Suggested change
this.testGroup = [403659147];
this.testGroups = [403659147];

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.

Makes sense. Updated!


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

function isPartnerInLocalLauncherTestGroup() {
var url = new URL(window.location.href);
var hostname = url.hostname;
var hash = mParticle.generateHash(hostname);

return self.testGroup.includes(hash);
}
};

function generateIntegrationName(customIntegrationName) {
Expand Down
51 changes: 51 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Rokt Forwarder', () => {
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,26 @@ describe('Rokt Forwarder', () => {
});
};

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');
},
use: function () {
throw new Error('use not implemented');
},
};
};

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

Expand Down Expand Up @@ -544,6 +565,36 @@ describe('Rokt Forwarder', () => {
},
},
};

window.mParticle.forwarder.testGroup = [];
});

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.testGroup = ['hashed-<localhost>-value'];

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