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
10 changes: 7 additions & 3 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ var constructor = function () {
return;
}

if (!window.Rokt || !(window.Rokt && window.Rokt.currentLauncher)) {
if (window.Rokt && typeof window.Rokt.createLauncher === 'function') {
if (!window.Rokt.currentLauncher) {
attachLauncher(accountId, launcherOptions);
} else {
initRoktLauncher(window.Rokt.currentLauncher);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question: How does the launcher get the accountId and the launcherOptions in this flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this else block, reusing an existing launcher that was already created with accountId and launcherOptions by a previous initialization. The launcher object stores its configuration internally, so we just attach our forwarder instance to it via initRoktLauncher().

Below is my understanding:

  • First init: window.Rokt.currentLauncher is undefinedattachLauncher(accountId, launcherOptions) creates a new launcher via createLauncher({accountId, ...launcherOptions}) → launcher stores this config internally

  • Subsequent (this else block): currentLauncher already exists → we skip creating a new one and just call initRoktLauncher(existingLauncher) to attach the current forwarder instance to it

  • The accountId and launcherOptions should already be baked into the existing launcher from the first init. Since mParticle workspaces have one Rokt kit configuration, all forwarder instances would be sharing the same accountId.

}
} else {
var target = document.head || document.body;
var script = document.createElement('script');
script.type = 'text/javascript';
Expand Down Expand Up @@ -151,8 +157,6 @@ var constructor = function () {

target.appendChild(script);
captureTiming(PerformanceMarks.RoktScriptAppended);
} else {
console.warn('Unable to find Rokt on the page');
}
}
/**
Expand Down
50 changes: 50 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,48 @@ 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);
});

it('should attach to existing launcher when currentLauncher already exists', async () => {
var existingLauncher = {
selectPlacements: function () {
return Promise.resolve({});
},
hashAttributes: function (attrs) {
return attrs;
},
};
window.Rokt.currentLauncher = existingLauncher;

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
false,
null,
{}
);

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

// Should not call createLauncher since launcher exists
window.mParticle.Rokt.createLauncherCalled.should.equal(false);

// Forwarder should be initialized and functional
window.mParticle.forwarder.isInitialized.should.equal(true);
});
});

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