Skip to content

Commit f485b50

Browse files
authored
feat: skip attaching the launcher when its embedded and update tests (#53)
1 parent e1a52c2 commit f485b50

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/Rokt-Kit.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ var constructor = function () {
5858
return baseUrl + '?extensions=' + extensions.join(',');
5959
}
6060

61+
/**
62+
* Checks if Rokt launcher is available and ready to attach
63+
* @returns {boolean} True if launcher can be attached
64+
*/
65+
function isLauncherReadyToAttach() {
66+
return window.Rokt && typeof window.Rokt.createLauncher === 'function';
67+
}
68+
6169
/**
6270
* Passes attributes to the Rokt Web SDK for client-side hashing
6371
* @see https://docs.rokt.com/developers/integration-guides/web/library/integration-launcher#hash-attributes
@@ -120,7 +128,9 @@ var constructor = function () {
120128
return;
121129
}
122130

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

133143
script.onload = function () {
134-
// Once the script loads, ensure the Rokt object is available
135-
if (
136-
window.Rokt &&
137-
typeof window.Rokt.createLauncher === 'function' &&
138-
window.Rokt.currentLauncher === undefined
139-
) {
144+
if (isLauncherReadyToAttach()) {
140145
attachLauncher(accountId, launcherOptions);
141146
} else {
142147
console.error(
@@ -151,8 +156,6 @@ var constructor = function () {
151156

152157
target.appendChild(script);
153158
captureTiming(PerformanceMarks.RoktScriptAppended);
154-
} else {
155-
console.warn('Unable to find Rokt on the page');
156159
}
157160
}
158161
/**

test/src/tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ describe('Rokt Forwarder', () => {
577577
});
578578

579579
it('should add a performance marker when the script is appended', async () => {
580+
var savedRokt = window.mParticle.Rokt;
581+
window.Rokt = undefined;
582+
window.mParticle.Rokt = {
583+
domain: 'apps.rokt.com',
584+
attachKit: async () => Promise.resolve(),
585+
filters: savedRokt.filters,
586+
};
587+
580588
await window.mParticle.forwarder.init(
581589
{ accountId: '123456' },
582590
reportService.cb,
@@ -724,6 +732,20 @@ describe('Rokt Forwarder', () => {
724732

725733
mockMessageQueue.length.should.equal(0);
726734
});
735+
736+
it('should call createLauncher when launcher is embedded and not yet initialized', async () => {
737+
await window.mParticle.forwarder.init(
738+
{ accountId: '123456' },
739+
reportService.cb,
740+
false,
741+
null,
742+
{}
743+
);
744+
745+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
746+
747+
window.mParticle.Rokt.createLauncherCalled.should.equal(true);
748+
});
727749
});
728750

729751
describe('#selectPlacements', () => {

0 commit comments

Comments
 (0)