Skip to content

Commit

Permalink
Merge pull request #76 from driveback/feature/custom-enrichments
Browse files Browse the repository at this point in the history
Feature/custom enrichments
  • Loading branch information
ConstantineYurevich authored Jan 23, 2017
2 parents 9621da4 + c7de015 commit 309d12e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 567 deletions.
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module.exports = function(grunt) {

grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-rename');
grunt.loadNpmTasks('grunt-aws-s3');

// Default task(s).
Expand Down
300 changes: 27 additions & 273 deletions build/dd-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7716,20 +7716,20 @@ function useColors() {
// NB: In an Electron preload script, document will be defined but not fully
// initialized. Since we know we're in Chrome, we'll just detect this case
// explicitly
if (typeof window !== 'undefined' && typeof window.process !== 'undefined' && window.process.type === 'renderer') {
if (typeof window !== 'undefined' && window && typeof window.process !== 'undefined' && window.process.type === 'renderer') {
return true;
}

// is webkit? http://stackoverflow.com/a/16459606/376773
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
return (typeof document !== 'undefined' && 'WebkitAppearance' in document.documentElement.style) ||
return (typeof document !== 'undefined' && document && 'WebkitAppearance' in document.documentElement.style) ||
// is firebug? http://stackoverflow.com/a/398120/376773
(typeof window !== 'undefined' && window.console && (console.firebug || (console.exception && console.table))) ||
(typeof window !== 'undefined' && window && window.console && (console.firebug || (console.exception && console.table))) ||
// is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
(navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
(typeof navigator !== 'undefined' && navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
// double check webkit in userAgent just in case we are in a worker
(navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
(typeof navigator !== 'undefined' && navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
}

/**
Expand Down Expand Up @@ -7857,11 +7857,6 @@ function localstorage() {
} catch (e) {}
}

/** Attach to Window*/
if (window) {
window.debug = exports;
}

}).call(this,require('_process'))

},{"./debug":67,"_process":73}],67:[function(require,module,exports){
Expand Down Expand Up @@ -12029,11 +12024,15 @@ var sinon = (function () { // eslint-disable-line no-unused-vars
}

if (types) {
// A new descriptor is needed here because we can only wrap functions
// By passing the original descriptor we would end up trying to spy non-function properties
var descriptor = {};
var methodDesc = sinon.getPropertyDescriptor(object, property);

for (var i = 0; i < types.length; i++) {
methodDesc[types[i]] = spy.create(methodDesc[types[i]]);
descriptor[types[i]] = spy.create(methodDesc[types[i]]);
}
return sinon.wrapMethod(object, property, methodDesc);
return sinon.wrapMethod(object, property, descriptor);
}

return sinon.wrapMethod(object, property, spy.create(object[property]));
Expand Down Expand Up @@ -13082,7 +13081,8 @@ var sinon = (function () { // eslint-disable-line no-unused-vars

// IE 8 does not support hasOwnProperty on the window object and Firefox has a problem
// when using hasOwn.call on objects from other frames.
var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);
var owned = (object.hasOwnProperty && object.hasOwnProperty === hasOwn) ?
object.hasOwnProperty(property) : hasOwn.call(object, property);

if (hasES5Support) {
var methodDesc = (typeof method === "function") ? {value: method} : method;
Expand Down Expand Up @@ -13146,9 +13146,17 @@ var sinon = (function () { // eslint-disable-line no-unused-vars
Object.defineProperty(object, property, wrappedMethodDesc);
}

// this only supports ES5 getter/setter, for ES3.1 and lower
// __lookupSetter__ / __lookupGetter__ should be integrated
if (hasES5Support) {
var checkDesc = sinon.getPropertyDescriptor(object, property);
if (checkDesc.value === method) {
object[property] = wrappedMethod;
}

// Use strict equality comparison to check failures then force a reset
// via direct assignment.
if (object[property] === method) {
} else if (object[property] === method) {
object[property] = wrappedMethod;
}
};
Expand Down Expand Up @@ -14944,7 +14952,7 @@ if (typeof sinon === "undefined") {
}

Object.getOwnPropertyNames(obj).forEach(function (k) {
if (!seen[k]) {
if (seen[k] !== true) {
seen[k] = true;
var target = typeof Object.getOwnPropertyDescriptor(obj, k).get === "function" ?
originalObj : obj;
Expand Down Expand Up @@ -16932,11 +16940,11 @@ var Integration = function (_EventEmitter) {
}
};

Integration.prototype.overrideProduct = function overrideProduct(product) {
Integration.prototype.overrideProduct = function overrideProduct() {
// abstract
};

Integration.prototype.overrideEvent = function overrideEvent(event) {
Integration.prototype.overrideEvent = function overrideEvent() {
// abstract
};

Expand Down Expand Up @@ -19958,7 +19966,7 @@ var GoogleAnalytics = function (_Integration) {
if (event.name === _events.COMPLETED_TRANSACTION && !this.getOption('noConflict')) {
this.onCompletedTransaction(event);
} else {
if ([_events.VIEWED_PRODUCT_DETAIL, _events.VIEWED_PRODUCT_CATEGORY, _events.SEARCHED_PRODUCTS, _events.COMPLETED_TRANSACTION, _events.VIEWED_CART].indexOf(event.name) < 0) {
if ([_events.VIEWED_PRODUCT_DETAIL, _events.VIEWED_PRODUCT_CATEGORY, _events.SEARCHED_PRODUCTS, _events.COMPLETED_TRANSACTION, _events.VIEWED_CART, _events.VIEWED_PRODUCT, _events.CLICKED_PRODUCT, _events.ADDED_PRODUCT, _events.REMOVED_PRODUCT, _events.VIEWED_CHECKOUT_STEP, _events.COMPLETED_CHECKOUT_STEP].indexOf(event.name) < 0) {
this.onCustomEvent(event);
}
}
Expand Down Expand Up @@ -21979,260 +21987,6 @@ var _Integration2 = require('./../Integration.js');

var _Integration3 = _interopRequireDefault(_Integration2);

var _deleteProperty = require('./../functions/deleteProperty.js');

var _deleteProperty2 = _interopRequireDefault(_deleteProperty);

var _events = require('./../events');

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { 'default': obj };
}

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}

function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}return call && ((typeof call === 'undefined' ? 'undefined' : _typeof(call)) === "object" || typeof call === "function") ? call : self;
}

function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + (typeof superClass === 'undefined' ? 'undefined' : _typeof(superClass)));
}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

function lineItemsToSociomanticsItems(lineItems) {
var products = [];
for (var i = 0, length = lineItems.length; i < length; i++) {
var lineItem = lineItems[i];
if (lineItem && lineItem.product) {
var productId = lineItem.product.id || lineItem.product.skuCode;
if (productId) {
var product = {
identifier: productId,
amount: lineItem.product.unitSalePrice || lineItem.product.unitPrice || 0,
quantity: lineItem.quantity || 1,
currency: lineItem.product.currency || ''
};
products.push(product);
}
}
}
return products;
}

function deleteEmptyProperties(objName) {
var keys = Object.keys(window[objName]);
keys.map(function (key) {
if (window[objName][key] === '') {
(0, _deleteProperty2['default'])(window[objName], key);
}
});
}

var Sociomantic = function (_Integration) {
_inherits(Sociomantic, _Integration);

function Sociomantic(digitalData, options) {
_classCallCheck(this, Sociomantic);

var optionsWithDefaults = Object.assign({
region: '',
adpanId: '',
prefix: ''
}, options);

var _this = _possibleConstructorReturn(this, _Integration.call(this, digitalData, optionsWithDefaults));

var region = _this.getOption('region') || '';
var regionPrefix = region ? region + '-' : '';
var adpanId = _this.getOption('adpanId');
var src = '//' + regionPrefix + 'sonar.sociomantic.com/js/2010-07-01/adpan/' + adpanId;

_this.addTag({
type: 'script',
attr: {
type: 'text/javascript',
async: true,
src: src
}
});

_this._isLoaded = false;
_this.trackingScriptCalled = false;
return _this;
}

Sociomantic.prototype.initialize = function initialize() {
this._isLoaded = true;
this.onLoad();
};

Sociomantic.prototype.isLoaded = function isLoaded() {
var adpanId = this.getOption('adpanId');
return window.sociomantic && window.sociomantic.sonar && window.sociomantic.sonar.adv[adpanId];
};

Sociomantic.prototype.loadTrackingScript = function loadTrackingScript() {
var adpanId = this.getOption('adpanId');
if (this.isLoaded()) {
window.sociomantic.sonar.adv[adpanId].enable();
} else {
this.load();
}
this.trackingScriptCalled = true;
};

Sociomantic.prototype.reset = function reset() {
(0, _deleteProperty2['default'])(window, 'sociomantic');
};

Sociomantic.prototype.getEnrichableEventProps = function getEnrichableEventProps(event) {
var enrichableProps = [];
switch (event.name) {
case _events.VIEWED_PAGE:
enrichableProps = ['page.type', 'user.userId', 'cart.lineItems'];
break;
case _events.VIEWED_PRODUCT_DETAIL:
enrichableProps = ['product'];
break;
case _events.VIEWED_PRODUCT_CATEGORY:
enrichableProps = ['listing.category'];
break;
case _events.SEARCHED_PRODUCTS:
enrichableProps = ['listing.category'];
break;
case _events.COMPLETED_TRANSACTION:
enrichableProps = ['transaction'];
break;
default:
// do nothing
}

return enrichableProps;
};

Sociomantic.prototype.trackEvent = function trackEvent(event) {
var _methods;

var methods = (_methods = {}, _methods[_events.VIEWED_PAGE] = 'onViewedPage', _methods[_events.VIEWED_PRODUCT_DETAIL] = 'onViewedProductDetail', _methods[_events.VIEWED_PRODUCT_CATEGORY] = 'onViewedProductListing', _methods[_events.VIEWED_CART] = 'onViewedCart', _methods[_events.COMPLETED_TRANSACTION] = 'onCompletedTransaction', _methods[_events.SEARCHED_PRODUCTS] = 'onViewedProductListing', _methods);

var method = methods[event.name];
if (method) {
this[method](event);
}
};

Sociomantic.prototype.onViewedPage = function onViewedPage(event) {
var _this2 = this;

var prefix = this.getOption('prefix');
var trackingObjectCustomerName = prefix + 'customer';
var trackingObjectBasketName = prefix + 'basket';
var user = event.user;
var page = event.page;
var specialPages = ['product', 'category', 'search', 'confirmation'];
var cart = event.cart;

if (user && user.userId) {
window[trackingObjectCustomerName] = {
identifier: user.userId
};
}

if (cart && cart.lineItems) {
var products = lineItemsToSociomanticsItems(cart.lineItems);
window[trackingObjectBasketName] = {
products: products
};
}
if (page && specialPages.indexOf(page.type) < 0) {
this.loadTrackingScript();
} else {
setTimeout(function () {
if (!_this2.trackingScriptCalled) {
_this2.loadTrackingScript();
}
}, 100);
}
};

Sociomantic.prototype.onViewedProductDetail = function onViewedProductDetail(event) {
var prefix = this.getOption('prefix');
var trackingObjectName = prefix + 'product';
var product = event.product;

if (product && (product.id || product.skuCode)) {
window[trackingObjectName] = {
identifier: product.id || product.skuCode
};
this.loadTrackingScript();
}
};

Sociomantic.prototype.onViewedProductListing = function onViewedProductListing(event) {
var prefix = this.getOption('prefix');
var trackingObjectName = prefix + 'product';
var listing = event.listing;

if (listing && listing.category) {
window[trackingObjectName] = {
category: listing.category
};
this.loadTrackingScript();
}
};

Sociomantic.prototype.onViewedCart = function onViewedCart() {
// Assigning basket object on every pages - see onViewedPage()
};

Sociomantic.prototype.onCompletedTransaction = function onCompletedTransaction(event) {
var prefix = this.getOption('prefix');
var trackingObjectSaleName = prefix + 'sale';
var trackingObjectBasketName = prefix + 'basket';
var transaction = event.transaction;

window[trackingObjectSaleName] = {
confirmed: true
};

if (transaction && transaction.lineItems) {
var products = lineItemsToSociomanticsItems(transaction.lineItems);
window[trackingObjectBasketName] = {
products: products,
transaction: transaction.orderId || '',
amount: transaction.total || '',
currency: transaction.currency || ''
};
deleteEmptyProperties(trackingObjectBasketName);
}

this.loadTrackingScript();
};

return Sociomantic;
}(_Integration3['default']);

exports['default'] = Sociomantic;

},{"./../Integration.js":105,"./../events":110,"./../functions/deleteProperty.js":112}],140:[function(require,module,exports){
'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

exports.__esModule = true;

var _Integration2 = require('./../Integration.js');

var _Integration3 = _interopRequireDefault(_Integration2);

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { 'default': obj };
}
Expand Down Expand Up @@ -23747,7 +23501,7 @@ describe('EventDataEnricher', function () {
});
});

},{"./../src/EventDataEnricher.js":105,"./../src/functions/deleteProperty.js":114,"assert":1}],151:[function(require,module,exports){
},{"./../src/EventDataEnricher.js":105,"./../src/functions/deleteProperty.js":114,"./../src/integrations/Emarsys":131,"assert":1}],151:[function(require,module,exports){
'use strict';

var _assert = require('assert');
Expand Down
20 changes: 10 additions & 10 deletions build/dd-manager-test.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 309d12e

Please sign in to comment.