Metrica

reachGoal

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.

Transmits information about a completed goal.

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

Parameter

Default value

Type

Description

target *
[no-highlight[

* Required parameter.

]no-highlight]

String

Goal name. Set on the counter editing page when creating or changing a JavaScript event type of goal.

callback

Function

The callback function to call after sending pageview data. Example

ctx

Object

The context accessed by the this keyword in the callback function. Example

params

Object

Session parameters

* Required parameter.

Use the following method if you need to customize when to register goal completion:

  1. On the counter editing page, create a goal with the JavaScript Event type and assign an ID to it.

  2. Call this function when the goal is completed:

    yaCounterXXXXXX.reachGoal('TARGET_NAME');

    where:

    • XXXXXX is your counter number.
    • TARGET_NAME — Goal ID.
Attention! 

The goal ID (TARGET_NAME) must not be a part of the URL for any of the site's pages. Otherwise, statistics will also include session information for those pages that contain the goal name in the URL.

When setting the goal ID, do not use the following symbols: /, &, #, ?, =.

Examples

Options for embedding goals in the source code of your site:

Forms:
<html>
    ...
    <form action="" method="get" onsubmit="yaCounterXXXXXX.reachGoal('ORDER', function () {
    alert('Данные успешно отправлены');
}, <Контекст, доступный по ключевому слову this внутри callback-функции>); return true;">
        ...
    </form>
    ...
</html>
Buttons:
<html>
    ...
    <form action="">
        ...
        <input type="button" onclick="yaCounterXXXXXX.reachGoal('ORDER'); return true;" value="Order" />
    </form>
    ...
</html>
Links:
<html>
    ...
    <a href="/price.zip" onclick="yaCounterXXXXXX.reachGoal('ORDER'); return true;">Price list</a>
    ...
</html>
Links with transmission of user parameters:
<html>
    ...
    <script type="text/javascript">
        var goalParams = {myParam: 123};
        function goalCallback () {
            console.log('request successfully sent to Yandex.Metrica');
        }
    </script>
    <a href="/price.zip" onclick="yaCounterXXXXXX.reachGoal('ORDER', goalParams, goalCallback); return true;">Price list</a>
    ...
</html>
Goal completion on page loading

If you are using asynchronous counter code 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() {
        yaCounterCOUNTER_ID.reachGoal('TARGET_NAME')
    }
</script>
Solution 2 (for jQuery)
<script type="text/javascript">
    $(window).load(function() {
        yaCounterCOUNTER_ID.reachGoal('TARGET_NAME')
    });
</script>

Goal price (value)

The goal price can be transmitted even if the site is not an online store. This amount will be transmitted each time the goal is completed. This helps you take the goal's commercial viability into account.

For example, your email distribution has a 10% conversion rate, and the order amount for a service or product is 10,000 rubles. In this case, you can set the goal value at 1000 rubles. You can also use points or symbolic units for designating the value: 1 is low, 10 is high.

This information will be available in reports if you add the Income goal metric
[no-highlight[

A numerical value based on the conversion attribute. This amount can be the total or average. For example, conversions are calculated as the total of all values, and conversion per user is calculated as the average value.

]no-highlight]
in the Yandex.Metrica interface. For more information, see Using goals in reports.

Parameter

Default value

Type

Description

order_price

Double

Goal price

currency

String

Three-letter ISO 4217 currency code.

Supported currencies
  • EUR — Euro
  • USD — United States dollar
  • GBP — British pound
  • CAD — Canadian dollar
  • AUD — Australian dollar
  • RUB — Russian ruble
  • UAH — Ukrainian hryvnia
  • BYN — Belarusian ruble
  • TRY — Turkish lira
  • CNY — Chinese yuan
  • KZT — Kazakhstani tenge
  • TMT — Turkmenistan manat
  • LVL — Latvian lats
  • MDL — Moldavan leu
  • CHF — Swiss franc
  • THB — Thai baht
  • YND — Yandex units

If a different currency is passed, null values will be sent instead of currencies and amounts.

...
var goalParams =
{
   order_price: 1000.35,
   currency: "RUB"
}
...