Google Analytics

Google Analytics with Enhanced Ecommerce, add snippet to snippets/script-tags

<!-- Google Analytics -->
<script>
  (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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXX-Y', 'auto');
  ga('set', 'anonymizeIp', true);
  ga('require', 'ec');

 document.addEventListener('appropo:pageview', function(e) {
    console.log("appropo:pageview", e.data);

    ga('set', 'page', window.location.pathname + window.location.search);
    ga('send', 'pageview');
 }, true);

  document.addEventListener('appropo:productview', function(e) {
    console.log("appropo:productview", e.data);

    ga('ec:addImpression', {
      'id': e.data.id,                   // Product ID (string).
      'name': e.data.name, // Product name (string).
      'category': e.data.tag_path,   // Product category (string).
    });
    ga('ec:setAction', 'detail');
    ga('send', 'pageview');

 }, true);

  document.addEventListener('appropo:addtocart', function(e) {
    console.log("appropo:addtocart", e.data);
    ga('ec:addProduct', {
      'id': e.data.id,
      'name': e.data.name,
      'category': e.data.tag_path,
      'price': e.data.unit_price,
      'quantity': e.data.quantity
    });
    ga('ec:setAction', 'add');
    ga('send', 'event', 'UX', 'click', 'add to cart');

 }, true);

  document.addEventListener('appropo:purchased', function(e) {
    console.log("appropo:purchased", e.data);

    ga('ec:setAction', 'purchase', {
      'id': e.data.id,
      'revenue': e.data.total,
    });
    ga('send', 'pageview');

 }, true);

</script>
<!-- End Google Analytics -->

Ready to get started? Get in touch.

Contact