Skip to content

Commit

Permalink
#759 google ecommerce (#760)
Browse files Browse the repository at this point in the history
* Remove dangling todo

* Dispatch gtm_loaded event

* Create PublishedGATracker class for GATracker

* Fix typo

* Review fixes

* Move class declaration upper than instantiation

* Apply linter rules
  • Loading branch information
ArtemijRodionov authored Mar 7, 2019
1 parent d6d9e44 commit 987327e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
61 changes: 49 additions & 12 deletions front/js/shared/tracking.es6
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,58 @@
$purchasedOrder: $('.js-purchased-order'),
};

// Sync container for yaTracker
window.dataLayer = window.dataLayer || [];
// Load ecommerce plugin for gaTracker
try {
ga('require', 'ecommerce'); // Ignore ESLintBear (block-scoped-var)
} catch (e) {
Sentry.captureException(e); // Ignore ESLintBear (no-undef)
var ga = console.log; // Ignore ESLintBear (no-var)
console.error(`GaTracker failed to load. Traceback: ${e}`);
// @todo #759:30m Move LoadedGATracker to the refarm.

/**
* Reliably submit a purchase transaction.
*
* The tracker depends on Google Analytics scripts (GA), those are loading by
* the Google tag manager (GTM).
* In case GA scripts are unloaded the tracker can't submit a transaction,
* so it will wait GTM onload event.
*/
class LoadedGATracker {
constructor() {
this.purchased = false;
}

purchase(productsData, txData) {
const purchaseOnce = () => {
if (this.purchased) return;

// Load ecommerce plugin for gaTracker
ga('require', 'ecommerce'); // Ignore ESLintBear (block-scoped-var)
const tracker = new GATracker(ga, 'ecommerce'); // Ignore ESLintBear (block-scoped-var)
tracker.purchase(productsData, txData);

this.purchased = true;
};

window.addEventListener('gtm_loaded', () => {
try {
purchaseOnce();
} catch (e) {
Sentry.captureException(e); // Ignore ESLintBear (no-undef)
console.error(e);
}
});

try {
purchaseOnce();
} catch (e) {
// Error occured because of unloaded Google tag manager.
// listener of `gtm_loaded` event will try again.
}
}
}

const yaTracker = new YATracker(window.dataLayer, 'RUB'); // Ignore ESLintBear (no-undef)
const gaTracker = new GATracker(ga, 'ecommerce'); // Ignore ESLintBear (block-scoped-var)
// @todo #759:60m Create tests for eCommerce tracking.
// Test all events, these perform tracking operations.

// @todo #504:30m Send info about product's brand to YA and GA.
// Sync container for yaTracker
window.dataLayer = window.dataLayer || [];
const yaTracker = new YATracker(window.dataLayer, 'RUB'); // Ignore ESLintBear (no-undef)
const gaTracker = new LoadedGATracker(); // Ignore ESLintBear (block-scoped-var)

const init = () => {
setUpListeners();
Expand Down
9 changes: 8 additions & 1 deletion templates/layout/google_tag_manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
'https://www.googletagmanager.com/gtm.js?id='+i+dl;

j.addEventListener('load', function() {
// notify listeners about loaded gtm
d.dispatchEvent(new CustomEvent('gtm_loaded', { bubbles: true }));
});

f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-P6RH2S');
</script>
<!-- End Google Tag Manager -->
Expand Down

3 comments on commit 987327e

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 987327e Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 504-f7f52310 disappeared from front/js/shared/tracking.es6, that's why I closed #525. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 987327e Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 759-bc6cfc4f discovered in front/js/shared/tracking.es6 and submitted as #761. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 987327e Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 759-e8d84e6c discovered in front/js/shared/tracking.es6 and submitted as #762. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.