Skip to content

Commit

Permalink
Merge pull request #20 from driveback/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ConstantineYurevich committed Mar 29, 2016
2 parents 548401e + f9845ae commit ee9d225
Show file tree
Hide file tree
Showing 16 changed files with 740 additions and 1,169 deletions.
1,286 changes: 300 additions & 986 deletions dist/dd-manager.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/dd-manager.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "digital-data-manager",
"description": "The hassle-free way to integrate Digital Data Layer on your website.",
"author": "Driveback LLC <[email protected]>",
"version": "1.0.7",
"version": "1.0.9",
"license": "MIT",
"main": "dist/dd-manager.js",
"directories": {
Expand Down
14 changes: 9 additions & 5 deletions src/DOMComponentsTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ class DOMComponentsTracking
const _defineDocBoundaries = () => {
this.docViewTop = $window.scrollTop();
this.docViewBottom = this.docViewTop + $window.height();
this.docViewLeft = 0;
this.docViewRight = $window.width();
this.docViewLeft = $window.scrollLeft();
this.docViewRight = this.docViewLeft + $window.width();

const maxWebsiteWidth = this.options.maxWebsiteWidth;
if (maxWebsiteWidth && maxWebsiteWidth < this.docViewRight) {
if (maxWebsiteWidth && maxWebsiteWidth < this.docViewRight && this.docViewLeft === 0) {
this.docViewLeft = (this.docViewRight - maxWebsiteWidth) / 2;
this.docViewRight = this.docViewLeft + maxWebsiteWidth;
}
Expand Down Expand Up @@ -204,6 +204,7 @@ class DOMComponentsTracking
*/
isVisible($elem) {
const el = $elem[0];
const $window = window.jQuery(window);

const elemOffset = $elem.offset();
const elemWidth = $elem.width();
Expand Down Expand Up @@ -233,15 +234,18 @@ class DOMComponentsTracking
}

let elementFromPoint = document.elementFromPoint(
elemLeft - this.docViewLeft + elemWidth / 2,
elemTop - this.docViewTop + elemHeight / 2
elemLeft - $window.scrollLeft() + elemWidth / 2,
elemTop - $window.scrollTop() + elemHeight / 2
);

while (elementFromPoint && elementFromPoint !== el && elementFromPoint.parentNode !== document) {
elementFromPoint = elementFromPoint.parentNode;
}

return (!!elementFromPoint && elementFromPoint === el);
}


/**
* Find elements by data attribute name
*
Expand Down
34 changes: 1 addition & 33 deletions src/DigitalDataEnricher.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import htmlGlobals from './functions/htmlGlobals.js';
import uuid from 'uuid';

class DigitalDataEnricher
{
constructor(digitalData, storage, options) {
constructor(digitalData) {
this.digitalData = digitalData;
this.storage = storage;
this.options = Object.assign({
sessionLength: 3600,
}, options);
}

setDigitalData(digitalData) {
this.digitalData = digitalData;
}

setStorage(storage) {
this.storage = storage;
}

getStorage() {
return this.storage;
}

enrichDigitalData() {
this.enrichPageData();
this.enrichUserData();
this.enrichContextData();
}

Expand All @@ -40,11 +26,6 @@ class DigitalDataEnricher
page.hash = page.hash || this.getHtmlGlobals().getLocation().hash;
}

enrichUserData() {
const user = this.digitalData.user;
user.anonymousId = this.getUserAnonymousId();
}

enrichContextData() {
const context = this.digitalData.context;
context.userAgent = this.getHtmlGlobals().getNavigator().userAgent;
Expand All @@ -57,19 +38,6 @@ class DigitalDataEnricher
getHtmlGlobals() {
return htmlGlobals;
}

getUserAnonymousId() {
let anonymousId = this.storage.get('user.anonymousId');
if (!anonymousId) {
anonymousId = uuid();
this.storage.set('user.anonymousId', anonymousId);
}
return anonymousId;
}

getOption(name) {
return this.options[name];
}
}

export default DigitalDataEnricher;
4 changes: 4 additions & 0 deletions src/Integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class Integration extends EventEmitter
// abstract
}

enrichDigitalData() {
// abstract
}

trackEvent() {
// abstract
}
Expand Down
2 changes: 2 additions & 0 deletions src/availableIntegrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import GoogleTagManager from './integrations/GoogleTagManager.js';
import Driveback from './integrations/Driveback.js';
import RetailRocket from './integrations/RetailRocket.js';
import FacebookPixel from './integrations/FacebookPixel.js';
import SegmentStream from './integrations/SegmentStream.js';

const integrations = {
[GoogleAnalytics.getName()]: GoogleAnalytics,
[GoogleTagManager.getName()]: GoogleTagManager,
[FacebookPixel.getName()]: FacebookPixel,
[Driveback.getName()]: Driveback,
[RetailRocket.getName()]: RetailRocket,
[SegmentStream.getName()]: SegmentStream,
};

export default integrations;
88 changes: 41 additions & 47 deletions src/ddManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import EventManager from './EventManager.js';
import AutoEvents from './AutoEvents.js';
import DDHelper from './DDHelper.js';
import DigitalDataEnricher from './DigitalDataEnricher.js';
import Storage from './Storage.js';

let ddManager;

/**
* @type {string}
Expand Down Expand Up @@ -41,12 +42,6 @@ let _digitalData = {};
*/
let _ddListener = [];

/**
* @type {Storage}
* @private
*/
let _storage;

/**
* @type {Object}
* @private
Expand Down Expand Up @@ -99,9 +94,44 @@ function _prepareGlobals() {
}
}

const ddManager = {
function _initializeIntegrations(settings, onReady) {
const ready = after(size(_integrations), onReady);

if (settings && typeof settings === 'object') {
const integrationSettings = settings.integrations;
if (integrationSettings) {
each(integrationSettings, (name, options) => {
if (typeof _availableIntegrations[name] === 'function') {
const integration = new _availableIntegrations[name](_digitalData, clone(options));
ddManager.addIntegration(integration);
}
});
}

if (size(_integrations) > 0) {
each(_integrations, (name, integration) => {
if (!integration.isLoaded() || integration.getOption('noConflict')) {
integration.once('ready', () => {
integration.enrichDigitalData();
_eventManager.addCallback(['on', 'event', (event) => {
integration.trackEvent(event);
}]);
ready();
});
integration.initialize();
} else {
ready();
}
});
} else {
ready();
}
}
}

ddManager = {

VERSION: '1.0.7',
VERSION: '1.0.9',

setAvailableIntegrations: (availableIntegrations) => {
_availableIntegrations = availableIntegrations;
Expand Down Expand Up @@ -168,15 +198,8 @@ const ddManager = {

_prepareGlobals();

// initialize storage
_storage = new Storage({
cookieDomain: settings.domain,
});

// initialize digital data enricher
const digitalDataEnricher = new DigitalDataEnricher(_digitalData, _storage, {
sessionLength: settings.sessionLength,
});
const digitalDataEnricher = new DigitalDataEnricher(_digitalData);
digitalDataEnricher.enrichDigitalData();

// initialize event manager
Expand All @@ -185,41 +208,12 @@ const ddManager = {
_eventManager.setAutoEvents(new AutoEvents(settings.autoEvents));
}

if (settings && typeof settings === 'object') {
const integrationSettings = settings.integrations;
if (integrationSettings) {
each(integrationSettings, (name, options) => {
if (typeof _availableIntegrations[name] === 'function') {
const integration = new _availableIntegrations[name](_digitalData, clone(options));
ddManager.addIntegration(integration);
}
});
}
}

const ready = after(size(_integrations), () => {
_initializeIntegrations(settings, () => {
_eventManager.initialize();
_isReady = true;
ddManager.emit('ready');
});

if (size(_integrations) > 0) {
each(_integrations, (name, integration) => {
if (!integration.isLoaded() || integration.getOption('noConflict')) {
integration.once('ready', ready);
integration.initialize();
} else {
ready();
}
// add event listeners for integration
_eventManager.addCallback(['on', 'event', (event) => {
integration.trackEvent(event);
}]);
});
} else {
ready();
}

_isInitialized = true;
ddManager.emit('initialize', settings);
},
Expand Down
92 changes: 41 additions & 51 deletions src/integrations/GoogleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class GoogleAnalytics extends Integration {
metrics: {},
dimensions: {},
contentGroupings: {},
namespace: undefined,
namespace: 'ddl',
noConflict: false,
filterEvents: [],
}, options);

super(digitalData, optionsWithDefaults);
Expand Down Expand Up @@ -104,7 +105,7 @@ class GoogleAnalytics extends Integration {
cookieDomain: this.getOption('domain'),
siteSpeedSampleRate: this.getOption('siteSpeedSampleRate'),
allowLinker: true,
name: this.getOption('namespace'),
name: this.getOption('namespace') ? this.getOption('namespace') : undefined,
});

// display advertising
Expand Down Expand Up @@ -208,59 +209,48 @@ class GoogleAnalytics extends Integration {
}

trackEvent(event) {
if (this.getOption('trackOnlyCustomEvents')) {
if ([
'Viewed Page',
'Viewed Product',
'Clicked Product',
'Viewed Product Detail',
'Added Product',
'Removed Product',
'Completed Transaction',
'Refunded Transaction',
'Viewed Product Category',
'Viewed Checkout Step',
'Completed Checkout Step',
].indexOf(event.name) < 0) {
const filterEvents = this.getOption('filterEvents') || [];
if (filterEvents.indexOf(event.name) >= 0) {
return;
}

if (event.name === 'Viewed Page') {
if (!this.getOption('noConflict')) {
this.onViewedPage(event);
}
} else if (this.getOption('enhancedEcommerce')) {
if (event.name === 'Viewed Product') {
this.onViewedProduct(event);
} else if (event.name === 'Clicked Product') {
this.onClickedProduct(event);
} else if (event.name === 'Viewed Product Detail') {
this.onViewedProductDetail(event);
} else if (event.name === 'Added Product') {
this.onAddedProduct(event);
} else if (event.name === 'Removed Product') {
this.onRemovedProduct(event);
} else if (event.name === 'Completed Transaction') {
this.onCompletedTransactionEnhanced(event);
} else if (event.name === 'Refunded Transaction') {
this.onRefundedTransaction(event);
} else if (event.name === 'Viewed Product Category') {
this.onViewedProductCategory(event);
} else if (event.name === 'Viewed Campaign') {
this.onViewedCampaign(event);
} else if (event.name === 'Clicked Campaign') {
this.onClickedCampaign(event);
} else if (event.name === 'Viewed Checkout Step') {
this.onViewedCheckoutStep(event);
} else if (event.name === 'Completed Checkout Step') {
this.onCompletedCheckoutStep(event);
} else {
this.onCustomEvent(event);
}
} else {
if (event.name === 'Viewed Page') {
this.onViewedPage(event);
} else if (this.getOption('enhancedEcommerce')) {
if (event.name === 'Viewed Product') {
this.onViewedProduct(event);
} else if (event.name === 'Clicked Product') {
this.onClickedProduct(event);
} else if (event.name === 'Viewed Product Detail') {
this.onViewedProductDetail(event);
} else if (event.name === 'Added Product') {
this.onAddedProduct(event);
} else if (event.name === 'Removed Product') {
this.onRemovedProduct(event);
} else if (event.name === 'Completed Transaction') {
this.onCompletedTransactionEnhanced(event);
} else if (event.name === 'Refunded Transaction') {
this.onRefundedTransaction(event);
} else if (event.name === 'Viewed Product Category') {
this.onViewedProductCategory(event);
} else if (event.name === 'Viewed Campaign') {
this.onViewedCampaign(event);
} else if (event.name === 'Clicked Campaign') {
this.onClickedCampaign(event);
} else if (event.name === 'Viewed Checkout Step') {
this.onViewedCheckoutStep(event);
} else if (event.name === 'Completed Checkout Step') {
this.onCompletedCheckoutStep(event);
} else {
this.onCustomEvent(event);
}
if (event.name === 'Completed Transaction') {
this.onCompletedTransaction(event);
} else {
if (event.name === 'Completed Transaction') {
this.onCompletedTransaction(event);
} else {
this.onCustomEvent(event);
}
this.onCustomEvent(event);
}
}
}
Expand Down
Loading

0 comments on commit ee9d225

Please sign in to comment.