Skip to content

Commit

Permalink
Drop GATracker. Add meta info to YATracker payload. (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemijRodionov authored Feb 19, 2020
1 parent a33eec0 commit aac3b70
Showing 1 changed file with 17 additions and 56 deletions.
73 changes: 17 additions & 56 deletions ecommerce/front/trackers.es6
Original file line number Diff line number Diff line change
@@ -1,77 +1,38 @@
class Tracker {
constructor(transport) {
this.transport = transport;
}
track() {
throw new Error('Track does not implemented');
}
// Got these names from SEO department.
const eventNames = {
detail: 'Detail',
add: 'addToCart',
remove: 'removeFromCart',
purchase: 'Purchase',
}

class YATracker extends Tracker {
class YATracker {
/**
* Yandex analytics ecommerce tracker.
* docs: https://yandex.ru/support/metrika/data/e-commerce.html
**/
constructor(transport, currencyCode) {
super(transport);
this.transport = transport;
this.currencyCode = currencyCode;
}
detail(productsData) {
this.track({detail: {products: productsData}});
this.track('detail', {products: productsData});
}
add(productsData) {
this.track({add: {products: productsData}});
this.track('add', {products: productsData});
}
remove(productsData) {
this.track({remove: {products: productsData}});
this.track('remove', {products: productsData});
}
purchase(productsData, actionData) {
this.track({purchase: {products: productsData, actionField: actionData}});
this.track('purchase', {products: productsData, actionField: actionData});
}
track(data) {
track(event, data) {
let payload = {};
payload[event] = data;
this.transport.push({
ecommerce: Object.assign({currencyCode: this.currencyCode}, data),
event: eventNames[event],
ecommerce: Object.assign({currencyCode: this.currencyCode}, payload),
});
}
}

class GATracker extends Tracker {
/**
* Google analytics ecommerce tracker.
* docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce?hl=en
**/
constructor(transport, name) {
super(transport);
this.name = name;
}
purchase(productsData, txData) {
this.track('addTransaction', txData);
for (let data of productsData)
this.track('addItem', data);
this.track('send');
}
track(actionName, data) {
this.transport(`${this.name}:${actionName}`, data);
}
}


/**
* Reliable loader of Google Analytics scripts.
*
* Google Analytics scripts (GA) are loading by the Google tag manager (GTM).
* If GA is still not loaded, the function will delay execution until it is loaded
* with GA command queue.
*
* See this for details about a command queue:
* http://bit.ly/rf-ga-command-queue
*/
function loadGaTransport() {
if (!window['ga']) {
window.ga = (...args) => {
(ga.q = ga.q || []).push(args);
};
ga.l = 1*new Date();
}
return ga;
}

0 comments on commit aac3b70

Please sign in to comment.