-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop GATracker. Add meta info to YATracker payload. (#351)
- Loading branch information
1 parent
a33eec0
commit aac3b70
Showing
1 changed file
with
17 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |