D
D
dmitry2511902017-04-05 09:10:53
JavaScript
dmitry251190, 2017-04-05 09:10:53

How to fix this code so that a report is generated with the correct information in Yandex Metrica?

Good day to all! Help fix. Very necessary. There is a site with a Yandex counter code for js installed on it:

<!-- Yandex.Metrika counter -->
    <script type="text/javascript">
        (function (d, w, c) {
            (w[c] = w[c] || []).push(function() {
                try {
                    w.yaCounter20722519 = new Ya.Metrika({
                        id:20722519,
                        clickmap:true,
                        trackLinks:true,
                        accurateTrackBounce:true,
                        webvisor:true,
                        trackHash:true,
                        ecommerce:"dataLayer"
                    });
                } catch(e) { }
            });
    
            var n = d.getElementsByTagName("script")[0],
                s = d.createElement("script"),
                f = function () { n.parentNode.insertBefore(s, n); };
            s.type = "text/javascript";
            s.async = true;
            s.src = "https://mc.yandex.ru/metrika/watch.js";
    
            if (w.opera == "[object Opera]") {
                                d.addEventListener("DOMContentLoaded", f, false);
                        } else { f(); }
                })(document, window, "yandex_metrika_callbacks");
                window.dataLayer = window.dataLayer || [];
    </script>
    <noscript><div><img src="https://mc.yandex.ru/watch/20722519" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
  <!-- /Yandex.Metrika counter -->


Also on this site there is a code for collecting information about adding a product to the cart:
<script>
function add_ecommerce() {
  window.dataLayer.push({
                "ecommerce": {
                                "add": {
                                                "products": [
                                                                {
                                                                                "id": "{GOODS_ID}",
                                                                                "name": "{GOODS_NAME}",
                                                                                "price": {GOODS_MOD_PRICE_NOW},
                                                                                "quantity": $('.quantity').val()
                                                                }
                                                ]
                                }
                }
});
}
$(document).ready(function(){
$('.add-cart.btn.btn-success.available-now').click(function(){
        add_ecommerce()
});
});
</script>


<script>
function add_detail() {
  window.dataLayer.push({
                "ecommerce": {
                                 "purchase": {
                                                "actionField": {
                                "id" : "{ORDER_NUM}",
                                },
                        "products":[ {%FOR order_lines%}
                        {% IFNOT order_lines.TYPE_ID=2 %}
                                                                {          "id": "{order_lines.MOD_ID}",
                                                                                "name" : "{order_lines.NAME}",
                                                                                "price": {order_lines.LINE_PRICE},
                                                                                "quantity": {order_lines.QUANTITY}
                                                                  
                                                                }{%IF order_lines.last%}{%ELSE%},{%ENDIF%}{%ENDIF%}
                                                                {%ENDFOR%}
                                                ]
                                }
                }
});
}
$(document).ready(function(){
  add_detail();
});
</script>

Now this code does not work correctly, when you add products to the shopping cart on the site, the products are added, but the report on the Yandex.metrics website does not display information about newly added products, how can I fix the code so that the correct information is displayed, i.e. new added products in the report on the Yandex.metrics website?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question