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