Trying to set up Yandex.metrika with e-commerce.

Successfully made an account viewing products. Now I see popular products, categories, brands on the Metric - i.e. counter installed correctly! Everything cool.

But he stumbled into the problem of accounting for goods added to the basket.
The fact is that adding to the basket is done via AJAX. Those. it is clear that dataLayer.push({}) needs to be inserted into the AJAX response, but for some reason it does not work.

AJAX looks like this:

 function addToCart(itemID, el) { $.ajax({ type: 'POST', url: aPath, data: 'do=addToCart&itemID=' + itemID, success: function(data, status) { console.log(data); /* ecommerce push for yandex metrika */ window.dataLayer = window.dataLayer || []; dataLayer.push({ 'event': 'addToCart', "ecommerce": { 'currencyCode': 'RUB', "add": { "products": [{ "id": itemID, "name": data.name, "price": data.price, "brand": data.brand, "category": data.category, "quantity": 1, "variant" : data.variant }] } } }); }, dataType:'json' }); 

console.log(data); it gives all the necessary information to the console ... Everything that needs to happen on the site when added to the basket happens as it should ... but Metric does not want: nothing appears in the metric statistics ...

Any suggestions are accepted!

    1 answer 1

    And how is your view implemented? I think we need to put this window above window.dataLayer = window.dataLayer || [];