Counter initialization

Tip. Working with the JavaScript API requires knowledge of HTML and JavaScript. If you don't have these skills, contact your website developer or webmaster.

Download the JavaScript library that is responsible for counter operation from the URL

The code for enabling the library is available on the counter editing page. The library can be loaded asynchronously (recommended) or synchronously, but its functionality remains the same.

Use the Ya.Metrika class for creating a counter. We strongly recommend creating an object of this class in the way that it is implemented in the code on the counter editing page. This is the proven way that works correctly in almost all cases.

Counter initialization involves creating an object of the Ya.Metrika class. The class constructor accepts a JavaScript object ( settings ) containing counter initialization parameters as fields.

var yaCounterXXXXXX = new Ya.Metrika(settings);
The only required parameter is the counter number, which is set in the id field:
var yaCounterXXXXXX = new Ya.Metrika({id: XXXXXX}); // XXXXXX - counter number

The part of the code containing the main initialization parameters is formed using the constructor on the counter editing page. The full list of initialization parameters is shown in the table.

Methods of a Ya.Metrika class object are described in the methods reference.

Parameters of counter initialization


Default value



id *


Counter number



Boolean | Number

The parameter can accept these values:

  • true — Enable the accurate bounce rate, with a non-bounce event registered after 15000 ms (15 s).
  • false — Do not enable the accurate bounce rate.
  • <N> (integer) — Enable the accurate bounce rate, with a non-bounce event registered after <N> ms.




Whether to collect data for a click map




Whether to disable automatically sending data during counter initialization

ecommerce false

Boolean | String | Array

Collecting data for ecommerce.

  • true — Enable ecommerce data collection. Data is transmitted via a JavaScript array named dataLayer in the global namespace (window.dataLayer).
  • false — Disable ecommerce data collection.
  • <objectName> (String) — Enable ecommerce data collection. Data is transmitted via a JavaScript array named objectName> in the global namespace (window.<objectName>).
  • <array> (Array) — Enable ecommerce data collection. Data is transmitted via the JavaScript array array>


Object | Array

Session parameters transmitted during counter initialization.

To transmit session parameters at any other time, use the params method.



Parameters of site users that are transmitted when initializing the counter.

To transmit user parameters at any other time, use the userParams method.




Hash tracking in the browser's address bar




External link tracking




Counter type. 1 for YAN



The only possible value is "noindex", which prohibits sending information about the page's existence to Yandex search robots.

Using this parameter doesn't guarantee that the page won't be indexed. A robot can get information about a page's existence from other sources.




Whether to use Session Replay




Whether to check if the counter is ready

* Required parameter.

Checking counter initialization

You can use a special JavaScript event to find out if the counter is ready to operate. However, you must add the triggerEvent parameter set to true in the counter code. For jQuery, you can use the following code sample up to the moment of counter initialization:

jQuery(document).on('yacounterXXXXXinited', function () {
    console.log('счетчик yaCounterXXXXX можно использовать');

Installing multiple counters

Sometimes you may need to put multiple counters on a page at once. In this case, it is preferable not to duplicate the entire counter initialization code. It is better to group the code for creating all objects of the Ya.Metrika class inside the try ... catch section, using the complete code of one of the counters as a template. Similarly, you can put the image insertion code that is used when JavaScript is disabled inside a single noscript element.

<!-- Yandex.Metrika counter -->
<script type="text/javascript">
    (function (d, w, c) {
        (w[c] = w[c] || []).push(function() {
            try {
                w.yaCounterXXXXXX = new Ya.Metrika({id: XXXXXX});
                w.yaCounterYYYYYY = new Ya.Metrika({id: YYYYYY, webvisor: true});
            } catch(e) { }

    })(document, window, "yandex_metrika_callbacks");
    <img src="//" style="..." alt="" />
    <img src="//" style="..." alt="" />
<!-- /Yandex.Metrika counter -->

Here XXXXXX and YYYYYY are the numbers of Yandex.Metrica counters.

Enabling a counter using the tag management system

Yandex.Metrica counters can also be enabled using tag management systems (also called tag dispatchers or tag containers). These systems let you store all the necessary HTML/JavaScript code on an external server and connect it to webpages using small code snippets. This means you can make changes to the connected code without changing the code of webpages.

Tag management systems usually support two types of integrated code for counters: JavaScript and image. Yandex.Metrica supports both types of counters. Use the following code to integrate counters using tag management systems.

<script src="//" type="text/javascript"></script>
<script type="text/javascript">
  var yaCounterXXXXXX = new Ya.Metrika({id: XXXXXX[, <initialization parameters>]});

Here, XXXXXX is the Yandex.Metrica counter number.

The data collected may vary depending on the type of counter code. For more information, see Information collected.

You can enable multiple counters at once inside each tag, in the same way as described in Installing multiple counters.