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.

  1. In Yandex Metrica, use the left menu to go to the Goals page and click Add goal.

  2. In the Name field, enter a name for the goal to create.

  3. Choose the JavaScript event goal type.

  4. 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 the reachGoal method on site pages:

    ...
    <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 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" />
      
    • 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.
  5. 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.

  6. 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.

Example

If the condition is button, the goal will be completed for the goal-button, button-goal, and goal-buy-button IDs.

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).

Example

If you want to track clicking a particular button with the ID that contains button or buy, you can specify the following condition: button|buy.

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

Chat with us

Write an email

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.