diff --git a/src/Rokt-Kit.js b/src/Rokt-Kit.js index 1e11709..ecd5440 100644 --- a/src/Rokt-Kit.js +++ b/src/Rokt-Kit.js @@ -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 @@ -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'; @@ -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( @@ -151,8 +156,6 @@ var constructor = function () { target.appendChild(script); captureTiming(PerformanceMarks.RoktScriptAppended); - } else { - console.warn('Unable to find Rokt on the page'); } } /** diff --git a/test/src/tests.js b/test/src/tests.js index 0b75846..ada98e0 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -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, + }; + await window.mParticle.forwarder.init( { accountId: '123456' }, reportService.cb, @@ -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', () => {