Add visitor’s IP address to Yandex metrics report

Presetting

To add the IP address of a site visitor to the Yandex metrics report, you need to slightly modify the counter code.

We will use the service l2.iowhich allows you to get the ip address.

Add a script to the metric counter

<!-- получаем ip адрес -->
<script type="text/javascript" src="https://www.l2.io/ip.js?var=userip"></script>

Now we just need to pass the ip parameter to the report, for this we add the params: {‘ip’: userip} line to the main metric code.

As a result, we get the counter code:

<!-- получаем ip адрес -->
<script type="text/javascript" src="https://www.l2.io/ip.js?var=userip"></script>
<!-- Yandex.Metrika counter -->
<script type="text/javascript" >
    (function (d, w, c) {
        (w[c] = w[c] || []).push(function() {
            try {
                w.yaCounter99999999 = new Ya.Metrika2({
                    id:99999999,
                    clickmap:true,
                    trackLinks:true,
                    accurateTrackBounce:true,
                    webvisor:true,
                    params:{'ip': userip}
                });
            } 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/tag.js";

        if (w.opera == "[object Opera]") {
            d.addEventListener("DOMContentLoaded", f, false);
        } else { f(); }
    })(document, window, "yandex_metrika_callbacks2");
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/99999999" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->

where 99999999 is your Yandex metric ID

Displaying IP in reports

Go to Metrica – Webvisor, click “Configure Columns” and add a column “Visit Settings”

As a result, after some time, after new visitors to the site, the web visor will display the following picture:

Add the visitor's IP address to the Yandex Metrics 2 report

Also, reports on IP addresses can be viewed: Reports – Standard reports – Contents – Visit parameters

UPD 28.11.2018

As suggested in the comments, the service l2.io works intermittently, because of this we will use a different way to determine ip

It is necessary to add javascript before the metric counter in the page code:

<script type="text/javascript">
var yaParams = {ip_adress: "<? echo $_SERVER['REMOTE_ADDR'];?>"};
</script>

And in the code of the metric counter (see above) instead of params: {‘ip’: userip} need to insert params: window.yaParams

UPD 06.06.2019

I noticed that a new counter code has appeared in Yandex Metrika, with support for Webvisor 2.0

I tested for it the definition of the visitor’s ip-address using the l2.io service, the ip is determined correctly This is how the updated counter code will look like:

<!-- Get user ip -->
<script type="text/javascript" src="https://www.l2.io/ip.js?var=userip"></script>
<!-- /Get user ip -->
<!-- Yandex.Metrika counter -->
<script type="text/javascript" >
   (function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
   m[i].l=1*new Date();k=e.createElement
   (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");

   ym(12345678, "init", {
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true,
        webvisor:true,
        params:{'ip': userip}
   });
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/12345678" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->

Where 12345678 – counter number. It must be changed to yours (in 2 places)

Sidebar