Skip to content

Commit

Permalink
Merge pull request #72 from driveback/feature/custom-enrichments
Browse files Browse the repository at this point in the history
Feature/custom enrichments
  • Loading branch information
ConstantineYurevich authored Dec 23, 2016
2 parents e77c154 + 11a0cc6 commit 9621da4
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 12 deletions.
326 changes: 322 additions & 4 deletions build/dd-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16309,6 +16309,7 @@ var EventDataEnricher = function () {
}
}
}
integration.overrideEvent(enrichedEvent);
return enrichedEvent;
};

Expand Down Expand Up @@ -16912,13 +16913,33 @@ var Integration = function (_EventEmitter) {
var _this = _possibleConstructorReturn(this, _EventEmitter.call(this));

_this.options = options;
if (options && options.overrideFunctions) {
_this.defineOverrideFunctions(options.overrideFunctions);
}
_this.digitalData = digitalData;
_this.tags = tags || {};
_this.onLoad = _this.onLoad.bind(_this);
_this._isEnriched = false;
return _this;
}

Integration.prototype.defineOverrideFunctions = function defineOverrideFunctions(overrideFunctions) {
if (overrideFunctions.event) {
this.overrideEvent = overrideFunctions.event.bind(this);
}
if (overrideFunctions.product) {
this.overrideProduct = overrideFunctions.product.bind(this);
}
};

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

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

Integration.prototype.initialize = function initialize() {
var onLoad = this.onLoad;
_async2['default'].nextTick(onLoad);
Expand Down Expand Up @@ -18849,9 +18870,10 @@ function calculateLineItemSubtotal(lineItem) {
return price * quantity;
}

function mapLineItems(lineItems) {
return lineItems.map(function mapLineItem(lineItem) {
function mapLineItems(lineItems, overrideProduct) {
return lineItems.map(function (lineItem) {
var product = lineItem.product;
overrideProduct(product);
var lineItemSubtotal = lineItem.subtotal || calculateLineItemSubtotal(lineItem);
return {
item: product.id || product.skuCode,
Expand Down Expand Up @@ -18975,7 +18997,7 @@ var Emarsys = function (_Integration) {
window.ScarabQueue.push(['setCustomerId', user.userId]);
}
if (cart.lineItems && cart.lineItems.length > 0) {
window.ScarabQueue.push(['cart', mapLineItems(cart.lineItems)]);
window.ScarabQueue.push(['cart', mapLineItems(cart.lineItems, this.overrideProduct)]);
} else {
window.ScarabQueue.push(['cart', []]);
}
Expand All @@ -19000,6 +19022,7 @@ var Emarsys = function (_Integration) {

Emarsys.prototype.onViewedProductDetail = function onViewedProductDetail(event) {
var product = event.product || {};
this.overrideProduct(product);
if (product.id || product.skuCode) {
window.ScarabQueue.push(['view', product.id || product.skuCode]);
}
Expand All @@ -19019,7 +19042,7 @@ var Emarsys = function (_Integration) {
if (transaction.orderId && transaction.lineItems) {
window.ScarabQueue.push(['purchase', {
orderId: transaction.orderId,
items: mapLineItems(transaction.lineItems)
items: mapLineItems(transaction.lineItems, this.overrideProduct)
}]);
}
go();
Expand Down Expand Up @@ -21956,6 +21979,260 @@ 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 @@ -23002,6 +23279,10 @@ var _EventDataEnricher = require('./../src/EventDataEnricher.js');

var _EventDataEnricher2 = _interopRequireDefault(_EventDataEnricher);

var _Emarsys = require('./../src/integrations/Emarsys');

var _Emarsys2 = _interopRequireDefault(_Emarsys);

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { 'default': obj };
}
Expand Down Expand Up @@ -23427,6 +23708,43 @@ describe('EventDataEnricher', function () {
_assert2['default'].ok(_digitalData.campaigns[0].category === 'Banner', 'digitalData.campaigns[0].category is not equal to "Banner"');
});
});

describe('#enrichIntegrationData', function () {
var emarsys = new _Emarsys2['default'](_digitalData, {
merchantId: 'XXX',
overrideFunctions: {
product: function product(_product) {
_product.id = 's/' + _product.id;
},
event: function event(_event) {
if (_event.name === 'Test') {
_event.prop1 = 'test2';
}
}
}
});

it('should override event data', function () {
var event = {
name: 'Test',
prop1: 'test1'
};
var enrichedEvent = _EventDataEnricher2['default'].enrichIntegrationData(event, _digitalData, emarsys);
_assert2['default'].equal(enrichedEvent.prop1, 'test2');
});

it('should override product data', function () {
var event = {
name: 'Viewed Product Detail',
product: {
id: '123'
}
};
emarsys.initialize();
emarsys.trackEvent(event);
_assert2['default'].equal(window.ScarabQueue[0][1], 's/123');
});
});
});

},{"./../src/EventDataEnricher.js":105,"./../src/functions/deleteProperty.js":114,"assert":1}],151:[function(require,module,exports){
Expand Down
Loading

0 comments on commit 9621da4

Please sign in to comment.