Goal via “JavaScript event”
This type of goal lets you track events on a site (such as button clicks and form submissions) that occur without the page URL changing. If the URL changes, use the Page view goal.
When this type of goal is completed, the information is transmitted to Yandex Metrica using JavaScript. This lets you track almost any custom event. This type of goal can also be used on Turbo pages.
In Ecommerce, use the goal_id field to transmit information about a conversion along with data about the action.
Tip
If you do not have programming skills, use the Form submission goal.
How to create a “JavaScript event” goal
Creating a goal includes specifying its identifier. When a goal is completed, the reachGoal method is called, and the specified ID is passed to it.
Note
Goal identifiers for matches and contains conditions must not contain the following characters: / \ & # ? = “. If you want to add a plus sign to the ID, enter %2B
in place of the + character.
-
In Yandex Metrica, use the left menu to go to the Goals page and click Add goal.
-
In the Name field, enter a name for the goal to create.
-
Choose the JavaScript event goal type.
-
Specify the desired condition and goal identifier. Below the identifier field, the Goal code for site line will appear. Copy the code. You will need it to configure the reachGoal method on your site.
Examples of using thereachGoal
method on site pages:Clicking a buttonFilling in a formSending revenue by goal... <form action=""> ... <input type="button" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;" value="Order" /> </form> ...
... <form action="" method="get" onsubmit="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;"> ... </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" />
XXXXXX
— Your tag number.TARGET_NAME
— Goal ID.order_price
— Revenue by goal. You can specify revenue in a currency or in conventional units.currency
— Currency of the revenue by goal. Yandex Metrica recognizes a three-letter ISO 4217 currency code.
-
-
Click Add goal. The created goal will appear in the list of goals. Yandex.Metrica will start collecting statistics on it within a few minutes.
-
Check whether the goal is working correctly.
Conditions for tracking a goal
There are several types of conditions available when creating a goal. Conditions are combined with the OR operator, so the goal is considered completed if at least one of the set conditions is met.
Criterion |
Description |
matches |
A full goal identifier is specified. |
contains |
A part of the ID is specified. Use this option if there are multiple goals and they can be combined with a single condition. Specify as much of the ID as possible so that only the desired goals are achieved. ExampleIf the condition is |
regular expression |
This is used for tracking goal identifiers that match a custom template. When setting up the condition, the regular expression must only contain the identifier value (without any domain or website protocol). ExampleIf you want to track clicking a particular button with the ID that contains |
Restrictions
-
A maximum of 200 goals can be set for each tag.
-
The service registers a user reaching the same goal on the same counter no more than once per second.
-
During a single user session, the service can register up to 1000 offline conversions and 400 online conversions created for the tag.
-
If you edit the tag or a goal, all the previously collected information does not change.
-
If you delete a goal, information collected for it is no longer available in reports.
Learn more
Useful links |
Online training |