Skip to content

Commit

Permalink
Merge pull request #92 from driveback/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ConstantineYurevich authored Feb 11, 2017
2 parents 0848cf4 + ac145f7 commit db5f637
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(config) {
// slIe8Win7: {
// base: 'SauceLabs',
// browserName: 'internet explorer',
// platform: 'Windows XP',
// platform: 'Windows 7',
// version: '8'
// },
slChromeWinXp: {
Expand Down
2 changes: 0 additions & 2 deletions src/EventDataEnricher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
35 changes: 22 additions & 13 deletions src/EventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -58,9 +59,6 @@ class EventManager {
events[events.length] = event;
};

if (_autoEvents) {
_autoEvents.onInitialize();
}
if (_viewabilityTracker) {
_viewabilityTracker.initialize();
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -281,7 +291,6 @@ class EventManager {
}
_ddListener.push = Array.prototype.push;
_callbacks = {};
_autoEvents = null;
_viewabilityTracker = null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ddManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ ddManager = {
domain: null,
websiteMaxWidth: 'auto',
sessionLength: 3600,
sendViewedPageEvent: false,
}, settings);

if (_isReady) {
Expand All @@ -244,6 +245,7 @@ ddManager = {

// initialize event manager
_eventManager = new EventManager(_digitalData, _ddListener);
_eventManager.setSendViewedPageEvent(settings.sendViewedPageEvent);
_eventManager.setViewabilityTracker(new ViewabilityTracker({
websiteMaxWidth: settings.websiteMaxWidth,
}));
Expand Down
8 changes: 5 additions & 3 deletions src/integrations/OneSignal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}]);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/EventManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions test/ddManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit db5f637

Please sign in to comment.