Tag 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 tag operation from the URL https://mc.yandex.ru/metrika/watch.js.

The code for enabling the library is available on the tag 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 tag. We strongly recommend creating an object of this class in the way that it is implemented in the code on the tag editing page. This is the proven way that works correctly in almost all cases.

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

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

The part of the code containing the main initialization parameters is formed using the constructor on the tag 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 tag initialization

Name

Default value

Type

Description

id *

Number

Tag ID.

accurateTrackBounce

false

Boolean | Number

Accurate bounce rate 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.

clickmap

false

Boolean

Whether to collect data for a click map

defer

false

Boolean

Whether to disable automatically sending data during tag initialization

ecommercefalse

Boolean | String | Array

Collecting data for e-commerce.

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

params

Object | Array

Session parameters transmitted during tag initialization.

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

userParams

Object

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

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

trackHash

false

Boolean

Hash tracking in the browser's address bar

trackLinks

false

Boolean

External link tracking

type

0

Number

Tag type. 1 for YAN

ut

String

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.

webvisor

false

Boolean

Whether to use Session Replay

triggerEvent

false

Boolean

Whether to check if the tag is ready

* Обязательный параметр.

Checking tag initialization

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

jQuery(document).on('yacounterXXXXXinited', function () {    console.log('the yaCounterXXXXX tag can be used');});

Enabling a tag using the tag management system

Yandex.Metrica tags 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 tags: JavaScript and image. Yandex.Metrica supports both types of tags. Use the following code to integrate tags using tag management systems.

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

Here, XXXXXX is the Yandex.Metrica tag ID.

The data collected may vary depending on the type of code snippet. For more information, see Data collected by the Yandex.Metrica tag.

You can enable multiple tags at once inside each tag, in the same way as described in Installing multiple tags on a site.