0
votes

I'm trying to integrate Enhanced Ecommerce into our website. We have "Ecommerce" working, as it tracks orders, but it doesn't track impressions and clicks. I'm trying to get it working, but my test analytics account portal, when I click on one product, it says I click on every product on the page.

Here's the code as it is now:

<% @products.each_with_index do |product, index|
      <% unless user_signed_in? && current_user.admin? %>
        <%= javascript_tag do -%>
        (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');
          ga('create', 'UA-XXXXXXXXX-XX', 'auto');
          ga('require', 'displayfeatures');
          ga('require', 'ec');
          /// ADD PRODUCT
            ga('ec:addProduct', {               // Provide product details in a productFieldObject.
              'name': '<%= product.name %>',
              'id': '<%= product.id %>',  
              'position': '<%= index+1 %>',
            });
            ga('send', 'pageview');    

            $('.product').on('click', function(e) {
                ga('ec:setAction', 'click', {       // click action.
                  'name': '<%= product.name %>',
                  'id': '<%= product.id %>', 
                  'position': '<%= index+1 %>',
                  'list': '<%= request.fullpath %>'          // Product list (string).
                });
            ga('send', 'pageview');
           });

        <% end %>

      <% end %>

I was thinking I would add the products on page load, and then add the click track on .product div click, but I'm a little confused about sending too many 'pageviews' and I don't know why it's tracking clicks on all the products when I click on one product.

THANK YOU

1

1 Answers

1
votes

The implementation is not under the standard. you need to follow this instruction https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce

To send the click you need:

1.- Enable the E-Ecommerce plugins (only the first time) 2.- Add the products 3.- Set the action (to click in this case) 4.- Send the information

In your code

ga('require', 'ec'); //Step 1
$('.product').on('click', function(e) {
ga('ec:addProduct', {   //step 2
    'name': '<%= product.name %>',
    'id': '<%= product.id %>',  
    'position': '<%= index+1 %>'
});
ga('ec:setAction', 'click', {       
'list': 'Search Results'          // Product list for impression or click
});      //Step 3
ga('send', 'pageview');      //step 4
});

Take into consideration that the step 4 can be an event.