Tag initialization
Download the JavaScript library that is responsible for tag operation from the URL https://mc.yandex.ru/metrika/tag.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.
(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(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, 'script', 'https://mc.yandex.ru/metrika/tag.js', 'ym')
Required arguments for ym are the tag ID and the name of the function being called. The method can also be passed arguments of the invoked function. For example, to initialize a tag, you need to pass the tag ID, the name of the init function, and an array of parameters:
ym(XXXXXX, 'init', {clickmap: true, webvisor: true});
If you do not want to pass parameters to the function, initialization will look like this:
ym(XXXXXX, 'init', {});
The part of the code containing the main initialization parameters is generated on the Settings page. The full list of initialization parameters is shown in the table.
Functions that can be called via the ym method are described in the methods reference.
Name | Default value | Type | Description |
---|---|---|---|
accurateTrackBounce |
| Boolean | Number | Accurate bounce rate The parameter can accept these values:
|
clickmap |
| Boolean | Whether to collect data for a click map |
defer |
| Boolean | Whether to disable automatically sending data during tag initialization |
ecommerce | false | Boolean | String | Array | Collecting data for e-commerce.
|
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 |
| Boolean | Hash tracking in the browser's address bar |
trackLinks |
| Boolean | Track clicks on outbound links |
type |
| 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 |
| Boolean | Whether to use Session Replay |
triggerEvent |
| Boolean | Whether to check if the tag is ready |
Name | Default value | Type | Description |
---|---|---|---|
accurateTrackBounce |
| Boolean | Number | Accurate bounce rate The parameter can accept these values:
|
clickmap |
| Boolean | Whether to collect data for a click map |
defer |
| Boolean | Whether to disable automatically sending data during tag initialization |
ecommerce | false | Boolean | String | Array | Collecting data for e-commerce.
|
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 |
| Boolean | Hash tracking in the browser's address bar |
trackLinks |
| Boolean | Track clicks on outbound links |
type |
| 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 |
| Boolean | Whether to use Session Replay |
triggerEvent |
| Boolean | Whether to check if the tag is ready |
Checking tag initialization
true
in the code snippet.ym(XXXXXX, 'init', {triggerEvent: true});
For jQuery, you can use the following code sample up to the moment of tag initialization:jQuery(document).on('yacounterXXXXXXinited', function () {
console.log('yaCounterXXXXXX tag is ready to use');
});
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/tag.js" type="text/javascript" async></script>
<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(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, 'script', 'https://mc.yandex.ru/metrika/tag.js', 'ym')
ym(XXXXXX, 'init'[, <initialization parameters>]);
</script>
<img src="https://mc.yandex.ru/watch/XXXXXX" style="position:absolute; left:-9999px;" alt="" />
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.