Reference of methods in the previous version

The code snippet for tags has been updated in Yandex Metrica, along with methods of the JavaScript API. The previous version of the methods is shown below.

addFileExtension

Tracks downloads of files with the specified extensions.

addFileExtension(extensions)

The extensions parameter is the same as the parameter in the new version of the method.

extLink

Sends information about clicks on outbound links.

extLink(url[, options])

The url and options parameters are the same as the parameters in the new version of the method.

file

Sends information about file downloads.

file(url[, options])

The url and options parameters are the same as the parameters in the new version of the method.

getClientID

Gets the site user ID assigned by Yandex Metrica.

yaCounterXXXXXX.getClientID()
hit

Sends data about a pageview.

hit(url[, options])

Example:

var yaCounterXXXXXX = new Ya.Metrika({id: XXXXXX});
//...
yaCounterXXXXXX.hit('#!contacts', {
    title: 'Contact information',
    referer: 'http://example.com/#!main'
});

The url and options parameters are the same as the parameters in the new version of the method.

notBounce

Sends information about non-bounces.

notBounce([options])

The options parameter is the same as the parameter in the new version of the method.

params

Transmits custom session parameters.

params(parameters)

Examples of transmitting session parameters:

User status
var yaParams = {
    x: 10,
    y: "unregistered user"
}

var yaCounterXXXXXX = new Ya.Metrika({id: XXXXXX, params: window.yaParams||{}});
A/B testing

Let’s say we need to conduct an experiment to determine how the color of the Buy button affects the conversion rate for a “View basket” goal.

If the user is shown a red button, we use the following parameter:
var yaParams = {ab_test: "red button"};

If the user is shown a green button, we use the following parameter:

var yaParams = {ab_test: "green button"};

This parameter is transmitted in any convenient way. For example, it can be passed when initializing the tag on product pages or using the reachGoal method that is called when the Buy button is clicked.

Transmitting a hierarchical data structure
var yaParams = {
    "level1":{"level2":["level3_1","level3_2"]}}
...

The parameters parameter is the same as the parameter in the new version of the method.

reachGoal

Transmits information about a completed goal.

reachGoal(target[, params[, callback[, ctx]]])

Examples of setting goals:

For a form
...
<form action="" method="get" onsubmit="yaCounterXXXXXX.reachGoal('TARGET_NAME'); return true;">
    ...
</form>
...
For a button
...
<form action="">
    ...
    <input type="button" onclick="yaCounterXXXXXX.reachGoal('TARGET_NAME'); return true;" value="Заказать" />
</form>
...
For a link
...
<a href="/price.zip" onclick="yaCounterXXXXXX.reachGoal('TARGET_NAME'); return true;">Price</a>
...
For a link with transmission of user parameters
|||UNTRANSLATED_CONTENT_START|||...
<script type="text/javascript">
    var goalParams = {myParam: 123};
    function goalCallback () {
        console.log('запрос в Метрику успешно отправлен');
    }
</script>
<a href="/price.zip" onclick="yaCounterXXXXXX.reachGoal('TARGET_NAME', goalParams, goalCallback); return true;">Прайс</a>
...|||UNTRANSLATED_CONTENT_END|||
Goal completion on page loading

If you are using an asynchronous code snippet and the goal is called using the script element, insert the following code on any part of the page:

Solution 1
<script type="text/javascript">
    window.onload = function() {
        yaCounterXXXXXX.reachGoal('TARGET_NAME')
    }
</script>
Solution 2 (for jQuery)
<script type="text/javascript">
    $(window).load(function() {
        yaCounterXXXXXX.reachGoal('TARGET_NAME')
    });
</script>
Setting the value of a goal for a form
...
<script type="text/javascript">
    var goalParams =
    {
       order_price: 1000.35,
       currency: "RUB"
    }
</script>
<form action="" method="get" onsubmit="yaCounterXXXXXX.reachGoal('TARGET_NAME', goalParams); return true;">
    ...
</form>
...

The target, params, callback and ctx parameters are the same as in the new version of the method.

setUserID

Transmitting the site user ID set by the site owner.

yaCounterXXXXXX.setUserID("12345")
userParams

Transmitting custom parameters of site users.

userParams(parameters)

The parameters parameter is the same as the parameter in the new version of the method.