Tag initialization
Tip
Working with the JavaScript API requires knowledge of HTML and JavaScript. If you don’t know these languages, contact your website developer or webmaster.
Go to https://mc.yandex.ru/metrika/tag.js and download the JavaScript library, which is required for the tag to work.
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.
To create and manage a tag, use the ym method. It is created in the download code:
(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 requires 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 don’t want to pass parameters to the function, initialization looks 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.
Tag initialization parameters
|
Name |
Default value |
Type |
Description |
|
|
|
Boolean | Number |
Accurate bounce rate The parameter can accept these values:
|
|
|
|
Boolean |
Whether to record iframe contents without a tag in a child window. |
|
|
|
Boolean |
Whether to collect data for a click map |
|
|
|
Boolean |
Whether to disable automatically sending data during tag initialization. |
|
|
|
Boolean | String | Array |
E-commerce data collection.
|
|
|
— |
Object | Array |
Session parameters transmitted during tag initialization. To transmit session parameters at any other time, use the params method. |
|
|
— |
Object |
Parameters of site users that are transmitted when initializing the tag. To transmit user parameters at any other time, use the userParams method. |
|
|
|
Boolean |
Tracking of hash changes in the browser’s address bar |
|
|
|
Boolean |
Track clicks on outbound links. |
|
|
— |
Array |
Indicates a trusted domain for recording the contents of a child iframe Contains the domain address of the parent window |
|
|
|
Number |
Tag type. 1 for YAN. |
|
|
|
Boolean |
Whether to use Session Replay |
|
|
|
Boolean |
Whether to check if the tag is ready. |
|
|
|
Boolean |
Title records. If titles contain private data, the parameter should be set to |
|
|
|
Boolean |
A technical parameter used by the ad tag. |
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:
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('tag yaCounterXXXXXX ready');
});
|
Useful links |
Online training |