jroakes

GSX Enhanced Ecommerce

Mar 4th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // More developer details: https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-checkout
  2.  
  3. // On each page//////////////////////////////////
  4. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  5.  
  6. ga('create', 'UA-6378212-24', 'auto', 'gsxTracker', {'allowLinker': true});
  7. ga('gsxTracker.require', 'ec');
  8. ga('gsxTracker.require', 'linker');
  9. ga('gsxTracker.linker:autoLink', ['gsx.org', 'asisonline.org', 'wyndhamjade.com'] );
  10. ga('gsxTracker.send', 'pageview'); //(On each page unless sent at step below)
  11.  
  12.  
  13. // Adding Products //////////////////////////////////
  14. function addToCart(product) {
  15.   ga('gsxTracker.ec:addProduct', {
  16.     'id': product.id,
  17.     'name': product.name,
  18.     'category': product.category,
  19.     'brand': product.brand,
  20.     'variant': product.variant,
  21.     'price': product.price,
  22.     'quantity': product.qty
  23.   });
  24.   ga('gsxTracker.ec:setAction', 'add');
  25.   ga('gsxTracker.send', 'event', 'UX', 'click', 'add to cart');     // Send data using an event.
  26. }
  27.  
  28.  
  29. // Checkout //////////////////////////////////
  30. /**
  31.  * Called when the user begins the checkout process.
  32.  * @param {Array} cart An array representing the user's shopping cart.
  33.  */
  34. function checkout(cart) {
  35.   for(var i = 0; i < cart.length; i++) {
  36.     var product = cart[i];
  37.     ga('gsxTracker.ec:addProduct', {
  38.       'id': product.id,
  39.       'name': product.name,
  40.       'category': product.category,
  41.       'brand': product.brand,
  42.       'variant':  product.variant,
  43.       'price': product.price,
  44.       'quantity': product.qty
  45.     });
  46.   }
  47. }
  48.  
  49.  
  50. // Purchase Steps //////////////////////////////////
  51.  
  52. 1. Registration /asis2019/registration.html
  53. ga('gsxTracker.ec:setAction','checkout', {
  54.     'step': 1,
  55.     'option': 'Registration'
  56. });
  57. ga('gsxTracker.send', 'pageview');
  58.  
  59.  
  60. 2. Profile /asis2019/profile.html
  61. ga('gsxTracker.ec:setAction','checkout', {
  62.     'step': 2,            
  63.     'option': 'Profile'
  64. });
  65. ga('gsxTracker.send', 'pageview');
  66.  
  67.  
  68. 3. Survey /asis2019/survey.html
  69. ga('gsxTracker.ec:setAction','checkout', {
  70.     'step': 3,            
  71.     'option': 'Survey'
  72. });
  73. ga('gsxTracker.send', 'pageview');
  74.  
  75.  
  76. 4. Payment /asis2019/payment.html
  77. ga('gsxTracker.ec:setAction','checkout', {
  78.     'step': 4,            
  79.     'option': 'Payment'
  80. });
  81. ga('gsxTracker.send', 'pageview');
  82.  
  83.  
  84. // Purchase / Confirmation //////////////////////////////////
  85.  
  86. ga('gsxTracker.ec:addProduct', {
  87.   'id': 'P12345',
  88.   'name': 'Android Warhol T-Shirt',
  89.   'category': 'Apparel',
  90.   'brand': 'Google',
  91.   'variant': 'black',
  92.   'price': '29.20',
  93.   'quantity': 1
  94. });
  95.  
  96. // Transaction level information is provided via an actionFieldObject.
  97. ga('gsxTracker.ec:setAction', 'purchase', {
  98.   'id': 'T12345',
  99.   'affiliation': 'GXS Registration',
  100.   'revenue': '37.39',
  101.   'tax': '2.85',
  102.   'shipping': '5.34',
  103.   'coupon': 'SUMMER2013'    // User added a coupon at checkout.
  104. });
  105.  
  106. ga('gsxTracker.send', 'pageview');
Add Comment
Please, Sign In to add comment