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 |
|
: |
String |
ID of the goal. Set on the tag editing page when creating or changing a goal of the “JavaScript event” type. |
|
: |
Object |
|
|
: |
Function |
The callback function to call after sending pageview data |
|
: |
Object |
Context accessed by the |
Fields for the |
|||
|
: |
Double |
Goal cost. You can specify revenue in a currency or in conventional units. |
|
: |
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:General solutionSolution for jQuery
<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
orid
), pass the selector name along with the revenue data to Yandex Metrica.
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>
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.
Useful links |
Online training |
Required parameter.