reachGoal

  1. Examples
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.

Transmits information about a completed goal.

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

Parameter

Default value

Type

Description

target *

string

Goal ID. The ID is set on the tag editing page when creating or changing a “JavaScript event” goal

params

Object

Session parameters

callback

Function

The callback function to call after sending pageview data

ctx

Object

The context accessed by the this keyword in the callback function

params object fields:
order_price

Double

Goal cost. You can set the cost in currency or Yandex units

currency

string

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

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

  • * Required parameter.

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

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.

Attention. The target ID value must not be a part of the URL for any of the pages on the site. Otherwise, statistics also include session information for pages that contain the goal name in the URL.

When setting the goal ID, don't use the following symbols: /, &, #, ?, =.

Examples

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

For a form
...
<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>
...
For a link
...
<a href="/price.zip" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;">Price</a>
...
For a link with transmission of user parameters
...
<script type="text/javascript">
    var goalParams = {myParam: 123};
    function goalCallback () {
        console.log('request successfully sent to Yandex.Metrica');
    }
</script>
<a href="/price.zip" onclick="ym(XXXXXX, reachGoal, 'TARGET_NAME', goalParams, goalCallback); return true;">Price</a>
...
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:

<script type="text/javascript">
    window.onload = function() {
        ym(XXXXXX, '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="ym(XXXXXX, 'reachGoal', 'TARGET_NAME', goalParams); return true;">
    ...
</form>
Transferring a goal cost 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">JoJo Dakimakura</div>
      <div class="price">RUB 3000.5</div>
      <input class="price-input hidden" value="3000.5" />
    </div>
    ...
</form>

  • XXXXXX is the tag ID.
  • TARGET_NAME is the goal ID.

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.

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 quick orders 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.