diff --git a/karma.conf.js b/karma.conf.js index 997e6bf..0691f96 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -21,7 +21,7 @@ module.exports = function(config) { // slIe8Win7: { // base: 'SauceLabs', // browserName: 'internet explorer', - // platform: 'Windows XP', + // platform: 'Windows 7', // version: '8' // }, slChromeWinXp: { diff --git a/src/EventDataEnricher.js b/src/EventDataEnricher.js index 992ef33..8376197 100644 --- a/src/EventDataEnricher.js +++ b/src/EventDataEnricher.js @@ -31,7 +31,6 @@ class EventDataEnricher } static enrichIntegrationData(event, digitalData, integration) { - const eventName = event.name; let enrichedEvent = clone(event); const enrichableProps = integration.getEnrichableEventProps(event); for (const prop of enrichableProps) { @@ -56,7 +55,6 @@ class EventDataEnricher } static overrideEventProducts(event, integration) { - const eventName = event.name; if (event.product) { integration.getProductOverrideFunction()(event.product); } else if (event.listing && event.listing.items) { diff --git a/src/EventManager.js b/src/EventManager.js index a288873..18167b4 100644 --- a/src/EventManager.js +++ b/src/EventManager.js @@ -8,15 +8,16 @@ import after from './functions/after.js'; import jsonIsEqual from './functions/jsonIsEqual.js'; import DDHelper from './DDHelper.js'; import EventDataEnricher from './EventDataEnricher.js'; +import { VIEWED_PAGE } from './events'; let _callbacks = {}; let _ddListener = []; let _previousDigitalData = {}; let _digitalData = {}; let _checkForChangesIntervalId; -let _autoEvents; let _viewabilityTracker; let _isInitialized = false; +let _sendViewedPageEvent = false; const _callbackOnComplete = (error) => { if (error) { @@ -58,9 +59,6 @@ class EventManager { events[events.length] = event; }; - if (_autoEvents) { - _autoEvents.onInitialize(); - } if (_viewabilityTracker) { _viewabilityTracker.initialize(); } @@ -72,18 +70,16 @@ class EventManager { _isInitialized = true; } - setAutoEvents(autoEvents) { - _autoEvents = autoEvents; - _autoEvents.setDigitalData(_digitalData); - _autoEvents.setDDListener(_ddListener); + setSendViewedPageEvent(sendViewedPageEvent) { + _sendViewedPageEvent = sendViewedPageEvent; } - setViewabilityTracker(viewabilityTracker) { - _viewabilityTracker = viewabilityTracker; + getSendViewedPageEvent() { + return _sendViewedPageEvent; } - getAutoEvents() { - return _autoEvents; + setViewabilityTracker(viewabilityTracker) { + _viewabilityTracker = viewabilityTracker; } checkForChanges() { @@ -256,6 +252,20 @@ class EventManager { fireUnfiredEvents() { const events = _digitalData.events; let event; + + if (_sendViewedPageEvent) { + let viewedPageEventIsSent = false; + for (event of events) { + if (event.name === VIEWED_PAGE) { + viewedPageEventIsSent = true; + break; + } + } + if (!viewedPageEventIsSent) { + events.unshift({ name: VIEWED_PAGE }); + } + } + for (event of events) { if (!event.hasFired) { this.fireEvent(event); @@ -281,7 +291,6 @@ class EventManager { } _ddListener.push = Array.prototype.push; _callbacks = {}; - _autoEvents = null; _viewabilityTracker = null; } } diff --git a/src/ddManager.js b/src/ddManager.js index e5457fa..c852134 100644 --- a/src/ddManager.js +++ b/src/ddManager.js @@ -225,6 +225,7 @@ ddManager = { domain: null, websiteMaxWidth: 'auto', sessionLength: 3600, + sendViewedPageEvent: false, }, settings); if (_isReady) { @@ -244,6 +245,7 @@ ddManager = { // initialize event manager _eventManager = new EventManager(_digitalData, _ddListener); + _eventManager.setSendViewedPageEvent(settings.sendViewedPageEvent); _eventManager.setViewabilityTracker(new ViewabilityTracker({ websiteMaxWidth: settings.websiteMaxWidth, })); diff --git a/src/integrations/OneSignal.js b/src/integrations/OneSignal.js index 06b78bd..d18bd3f 100644 --- a/src/integrations/OneSignal.js +++ b/src/integrations/OneSignal.js @@ -302,9 +302,11 @@ class OneSignal extends Integration { if (event.name === VIEWED_PAGE || event.name === SUBSCRIBED) { const user = event.user; if (user && user.email) { - window.OneSignal.push(() => { - window.OneSignal.syncHashedEmail(user.email); - }); + window.OneSignal.push(['getRegistrationId', (registrationId) => { + if (registrationId) { // This operation can only be performed after the user is subscribed + window.OneSignal.syncHashedEmail(user.email); + } + }]); } } diff --git a/test/EventManagerSpec.js b/test/EventManagerSpec.js index 6a4ba4a..183ab2d 100644 --- a/test/EventManagerSpec.js +++ b/test/EventManagerSpec.js @@ -177,6 +177,29 @@ describe('EventManager', () => { }); }); + it('should add Viewed Page event if sendViewedPageEvent setting is enabled', () => { + _digitalData.events.push({ name: 'Viewed Product Detail' }); + + _eventManager.setSendViewedPageEvent(true); + _eventManager.initialize(); + + assert.equal(_digitalData.events[0].name, 'Viewed Page'); + assert.equal(_digitalData.events[1].name, 'Viewed Product Detail'); + assert.equal(_digitalData.events.length, 2); + }); + + it('should not add Viewed Page event if sendViewedPageEvent setting is enabled, but Viewed Page already sent', () => { + _digitalData.events.push({ name: 'Viewed Page' }); + _digitalData.events.push({ name: 'Viewed Product Detail' }); + + _eventManager.setSendViewedPageEvent(true); + _eventManager.initialize(); + + assert.equal(_digitalData.events[0].name, 'Viewed Page'); + assert.equal(_digitalData.events[1].name, 'Viewed Product Detail'); + assert.equal(_digitalData.events.length, 2); + }); + it('should enrich product data from DDL', (done) => { _digitalData.product = { id: '123', diff --git a/test/ddManagerSpec.js b/test/ddManagerSpec.js index c43d4ec..34477e4 100644 --- a/test/ddManagerSpec.js +++ b/test/ddManagerSpec.js @@ -110,6 +110,16 @@ describe('DDManager', () => { assert.ok(ddManager.getIntegration('Google Tag Manager') instanceof Integration); }); + it('shoud set EventManager\'s sendViewedPageEvent value to true', () => { + ddManager.initialize({ sendViewedPageEvent: true }); + assert.ok(ddManager.getEventManager().getSendViewedPageEvent()); + }); + + it('shoud set EventManager\'s sendViewedPageEvent value to false', () => { + ddManager.initialize(); + assert.ok(!ddManager.getEventManager().getSendViewedPageEvent()); + }); + it('it should fire on("ready") event even if ddManager was ready before', (done) => { ddManager.initialize(); if (ddManager.isReady()) {