From aac3b70192ae29b6ee2b254b4a936aae3156cdd8 Mon Sep 17 00:00:00 2001 From: Artemiy Rodionov Date: Wed, 19 Feb 2020 07:32:33 +0300 Subject: [PATCH] Drop GATracker. Add meta info to YATracker payload. (#351) --- ecommerce/front/trackers.es6 | 73 +++++++++--------------------------- 1 file changed, 17 insertions(+), 56 deletions(-) diff --git a/ecommerce/front/trackers.es6 b/ecommerce/front/trackers.es6 index b17a0b1..7ce51e6 100644 --- a/ecommerce/front/trackers.es6 +++ b/ecommerce/front/trackers.es6 @@ -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; -}