google-analytics.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* jscs:disable */
  6. /* eslint-disable */
  7. define([
  8. 'jquery',
  9. 'mage/cookies'
  10. ], function ($) {
  11. 'use strict';
  12. /**
  13. * @param {Object} config
  14. */
  15. return function (config) {
  16. var allowServices = false,
  17. allowedCookies,
  18. allowedWebsites;
  19. if (config.isCookieRestrictionModeEnabled) {
  20. allowedCookies = $.mage.cookies.get(config.cookieName);
  21. if (allowedCookies !== null) {
  22. allowedWebsites = JSON.parse(allowedCookies);
  23. if (allowedWebsites[config.currentWebsite] === 1) {
  24. allowServices = true;
  25. }
  26. }
  27. } else {
  28. allowServices = true;
  29. }
  30. if (allowServices) {
  31. (function (i, s, o, g, r, a, m) {
  32. i.GoogleAnalyticsObject = r;
  33. i[r] = i[r] || function () {
  34. (i[r].q = i[r].q || []).push(arguments)
  35. }, i[r].l = 1 * new Date();
  36. a = s.createElement(o),
  37. m = s.getElementsByTagName(o)[0];
  38. a.async = 1;
  39. a.src = g;
  40. m.parentNode.insertBefore(a, m)
  41. })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
  42. // Process page info
  43. ga('create', config.pageTrackingData.accountId, 'auto');
  44. if (config.pageTrackingData.isAnonymizedIpActive) {
  45. ga('set', 'anonymizeIp', true);
  46. }
  47. // Process orders data
  48. if (config.ordersTrackingData.hasOwnProperty('currency')) {
  49. ga('require', 'ec', 'ec.js');
  50. ga('set', 'currencyCode', config.ordersTrackingData.currency);
  51. // Collect product data for GA
  52. if (config.ordersTrackingData.products) {
  53. $.each(config.ordersTrackingData.products, function (index, value) {
  54. ga('ec:addProduct', value);
  55. });
  56. }
  57. // Collect orders data for GA
  58. if (config.ordersTrackingData.orders) {
  59. $.each(config.ordersTrackingData.orders, function (index, value) {
  60. ga('ec:setAction', 'purchase', value);
  61. });
  62. }
  63. ga('send', 'pageview');
  64. } else {
  65. // Process Data if not orders
  66. ga('send', 'pageview' + config.pageTrackingData.optPageUrl);
  67. }
  68. }
  69. }
  70. });