Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 12 additions & 9 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ var constructor = function () {
return baseUrl + '?extensions=' + extensions.join(',');
}

/**
* Checks if Rokt launcher is available and ready to attach
* @returns {boolean} True if launcher can be attached
*/
function isLauncherReadyToAttach() {
return window.Rokt && typeof window.Rokt.createLauncher === 'function';
}

/**
* Passes attributes to the Rokt Web SDK for client-side hashing
* @see https://docs.rokt.com/developers/integration-guides/web/library/integration-launcher#hash-attributes
Expand Down Expand Up @@ -120,7 +128,9 @@ var constructor = function () {
return;
}

if (!window.Rokt || !(window.Rokt && window.Rokt.currentLauncher)) {
if (isLauncherReadyToAttach()) {
attachLauncher(accountId, launcherOptions);
} else {
var target = document.head || document.body;
var script = document.createElement('script');
script.type = 'text/javascript';
Expand All @@ -131,12 +141,7 @@ var constructor = function () {
script.id = 'rokt-launcher';

script.onload = function () {
// Once the script loads, ensure the Rokt object is available
if (
window.Rokt &&
typeof window.Rokt.createLauncher === 'function' &&
window.Rokt.currentLauncher === undefined
) {
if (isLauncherReadyToAttach()) {
attachLauncher(accountId, launcherOptions);
} else {
console.error(
Expand All @@ -151,8 +156,6 @@ var constructor = function () {

target.appendChild(script);
captureTiming(PerformanceMarks.RoktScriptAppended);
} else {
console.warn('Unable to find Rokt on the page');
}
}
/**
Expand Down
22 changes: 22 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@ describe('Rokt Forwarder', () => {
});

it('should add a performance marker when the script is appended', async () => {
var savedRokt = window.mParticle.Rokt;
window.Rokt = undefined;
window.mParticle.Rokt = {
domain: 'apps.rokt.com',
attachKit: async () => Promise.resolve(),
filters: savedRokt.filters,
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

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

For better test isolation and consistency, consider explicitly initializing the createLauncherCalled flag in the new mParticle.Rokt object. While this test doesn't assert on createLauncherCalled, adding it would match the pattern used in beforeEach (line 542) and prevent potential issues if future tests rely on this flag's state. Example: createLauncherCalled: false

Suggested change
filters: savedRokt.filters,
filters: savedRokt.filters,
createLauncherCalled: false,

Copilot uses AI. Check for mistakes.
};

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
Expand Down Expand Up @@ -724,6 +732,20 @@ describe('Rokt Forwarder', () => {

mockMessageQueue.length.should.equal(0);
});

it('should call createLauncher when launcher is embedded and not yet initialized', async () => {
await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
false,
null,
{}
);

await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);

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

describe('#selectPlacements', () => {
Expand Down
Loading