reachGoal

Warning

Working with the JavaScript API requires knowledge of HTML and JavaScript. If you don’t know these languages, contact your website developer or webmaster.

Transmits information about a completed goal.

ym(XXXXXX, 'reachGoal', target[, params[, callback[, ctx]]]);

Parameter

Default value

Type

Description

target*

:

String

ID of the goal. Set on the tag editing page when creating or changing a goal of the “JavaScript event” type.

params

:

Object

Session parameters

callback

:

Function

The callback function to call after sending pageview data

ctx

:

Object

Context accessed by the this keyword in the callback function

Fields for the params object:

order_price or price

:

Double

Goal cost. You can specify revenue in a currency or in conventional units.

currency

:

String

Use this field if you want to pass the goal cost in currency. Yandex Metrica recognizes a three-letter ISO 4217 currency code.

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

If you want to track the same action in multiple locations, simply create one JavaScript event goal and call the reachGoal method with the goal ID each time the goal is completed.

If you have a number of different events, create a separate goal for each event and track them separately. In this case, goals must have different IDs.

Alert

When setting the goal ID, don't use the following symbols: / \ & # ? = ". If you want to add a plus sign to the ID, enter %2B in place of the + character.

Examples

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

Forms
...
<form action="" method="get" onsubmit="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;">
    ...
</form>
...
For a button:
...
<form action="">
    ...
    <input type="button" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;" value="Order" />
</form>
...
Links:
...
<a href="/price.zip" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;">Price list</a>
...
For a link with transmission of user parameters:
...
<script type="text/javascript">
    var goalParams = {myParam: 123};
    function goalCallback () {
        console.log('Request sent to Yandex Metrica');
    }
</script>
<a href="/price.zip" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME', goalParams, goalCallback); return true;">Price list</a>
...
Goal completion on page loading

If you are using an asynchronous code snippet and the goal is called using the script element, embed the following code in any section of the page:

<script type="text/javascript">
    window.onload = function() {
        ym(XXXXXX, 'reachGoal', 'TARGET_NAME')
    }
</script>
<script type="text/javascript">
    $(window).load(function() {
        ym(XXXXXX, 'reachGoal', 'TARGET_NAME')
    });
</script>
Setting revenue by goal on a form
...
<form action="">
    ...
    <input type="button" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME', {order_price: '1000.35', currency: 'RUB'}); return true;" value="Order"/>
</form>
...
How to send revenue data using an attribute selector

To transfer revenue as order_price from all pages of the site where it is set by an attribute selector (for example, class or id), pass the selector name along with the revenue data to Yandex Metrica.

  1. Find the fragment in your site code where a selector of this type is set. Example:

    <div class="ORDER">Order amount: <div class="PRICE">110</div> RUB</div>
    
  2. Add the selector name to the code of the element that will send the revenue by goal to Yandex Metrica. Example:

    <input type="button" onclick="ym(XXXXXX, 'reachGoal', 'BUY', {order_price: document.querySelector('.PRICE')?.textContent}); return true;" value="Order" />
    
Passing revenue by goal dynamically using JQuery
<script type="text/javascript">
    jQuery(document).ready(function () {
      var reachGoalWithDynamicPrice = function () {
        var dynamicPrice = jQuery('#cart .price-input').reduce(function (total, input) {
          var price = parseFloat(input.val())
          
          return isNaN(price) ? total : total + price
        }, 0)
        var goalParams = {
          order_price: dynamicPrice,
          currency: "RUB"
        }
    
        ym(XXXXXX, 'reachGoal', 'TARGET_NAME', goalParams)
        
        return true
      }
      jQuery('#cart').submit(reachGoalWithDynamicPrice)
    }
</script>
<form id="cart" action="/submit_order.php" method="post">
    <div class="item">
      <div class="name">Dakimakura with JoJo</div>
      <div class="price">RUB 3000.5</div>
      <input class="price-input hidden" value="3000.5" />
    </div>
    ...
</form>
  • XXXXXX — Counter number.
  • TARGET_NAME — Goal ID.

Chat with us

Write an email

Select a question to find a solution.

Goals in Yandex Metrica work the same way for any sources. Perhaps there aren’t any users who have completed the goal from this source yet.

The recommendations did not help

Please note: Our support team will never initiate a call to you. Do not follow any instructions of people who call you and introduce themselves as the Yandex Metrica support team.

This might happen for the following reasons:

  • The goal condition in Yandex Metrica doesn't cover all the possibilities. For example, the goal is set as going to the order confirmation page, but the site also allows instant checkout without visiting this page.
  • The site user has a browser plugin that blocks the Yandex Metrica tag, so this user isn’t counted.
  • The user has an antivirus system with strict privacy settings.
  • The user has a slow internet connection, so the Yandex Metrica tag didn’t load on the target page.
The recommendations did not help

Please note: Our support team will never initiate a call to you. Do not follow any instructions of people who call you and introduce themselves as the Yandex Metrica support team.


Required parameter.

Previous