2

I want to measure Impression and Clicks of each product using Enhanced E-commerce Google Analytics. I have made two function for Impression and Clicks.

// function to count clicks
<script type="text/javascript">
function product_click(id,name,list){
    var script_open = '<script type="text/javascript">';
    var require = "ga('require', 'ec');";
    var product = "ga('ec:addProduct',{'id':'"+id+"','name':'"+ name+"'});";
    var send = "ga('ec:send');";
    var action = "ga('ec:setAction', 'click', {'list':'"+ list+"'}); </";
    var script_closed = 'script>';
    var clicks = script_open+require+product+send+action+script_closed;
    jQuery('#click_code').html(clicks);

    console.log(clicks);
    return clicks;
}
</script>

<div id="click_code"></div>
<div class="category_thumbArea" onClick="return product_click('abcd','pqrs','Category_Page');">
</div> // call to product_click()

// function to count impressions
function addImpression($itemCode, $itemName, $list) {
    $script_open = '<script type="text/javascript">';
    $code = "ga('require', 'ec');
             ga('ec:addImpression', {
               'id': '".$itemCode."',
               'name': '".$itemName."',
               'list': '".$list."'
             });
             ga('send', 'pageview'); ";
    $script_close = '</script>';

    return $script_open.$code.$script_close;
}

echo $imp = addImpression($itemCode,$itemName,'Category_Page'); // call to addImpression() 

I am able to capture impressions properly but not clicks. If the argument is "Search Results" for product_click(), it will count clicks against "Search Results" but if the argument anything (e.g.: "Category_Page") other than that it can not count the clicks against it ("Category_Page") or "Search Results"

Is there anything wrong in the code or it is restricted for the words "Search Results" ?

1 Answer 1

2

I referred Enhanced Ecommerce Product Click event and it looks like you haven't sent the click event to GA.

  // Send click with an event, then send user to product page.
  ga('send', 'event', 'UX', 'click', 'Results', {
      'hitCallback': function() {
        document.location = '/product_details?id=P12345';
      }
  });

Adding this in your product_click function just before script_closed string would probably trigger it.

You will need to change the document.location source according to your code.

Also, why append static javascript code on your page when you should directly execute it from your function? Replace all that simply with this-

ga('require', 'ec');
ga('ec:addProduct', {'id': id,'name': name});
ga('ec:send');
ga('ec:setAction', 'click', {'list': list});

// Send click with an event, then send user to product page.
ga('send', 'event', 'UX', 'click', 'Results', {
    'hitCallback': function() {
        document.location = '/product_details?id=P12345';
    }
});

Do test and let me know if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.