reachGoal
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 | |
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 or 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. |
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 | |
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 or 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.
When setting the goal ID, don't use the following symbols:/ \ & # ? = ".
Examples
Options for embedding goals in the source code of your site:
...
<form action="" method="get" onsubmit="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;">
...
</form>
...
...
<form action="">
...
<input type="button" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;" value="Заказать" />
</form>
...
...
<a href="/price.zip" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;">Price</a>
...
|||UNTRANSLATED_CONTENT_START|||...
<script type="text/javascript">
var goalParams = {myParam: 123};
function goalCallback () {
console.log('запрос в Метрику успешно отправлен');
}
</script>
<a href="/price.zip" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME', goalParams, goalCallback); return true;">Прайс</a>
...|||UNTRANSLATED_CONTENT_END|||
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>
<script type="text/javascript">
$(window).load(function() {
ym(XXXXXX, 'reachGoal', 'TARGET_NAME')
});
</script>
<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>
<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">Body pillow with JoJo</div>
<div class="price">3000.50 rub</div>
<input class="price-input hidden" value="3000.5" />
</div>
...
</form>
XXXXXX
is the tag ID.TARGET_NAME
is the goal ID.