Installing and configuring the tag for SPA sites

  1. Tag initialization
  2. Sending conversions
  3. Transmitting session parameters and user parameters
  4. Transmitting e-commerce data
  5. Enabling Session Replay, click maps, link maps, and Form Analysis

Tag initialization

To use a Yandex.Metrica tag on SPA sites, follow these steps:

  1. When initializing the tag, set the defer parameter to true. This is required to disable the automatic submission of data on views. To register page impressions, use the hit function, as described in point 3.

    Example of initializing the tag:

    ym(XXXXXX, 'init', {
        defer: true,
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true
    })
  2. In order for Yandex.Metrica to register important changes to a page, analyze the logic of your site and insert a hit function in the appropriate places so it’s triggered each time you believe the page has changed.

    Call the hit function:

    ym(XXXXXX, 'hit', url[, options]);
    Parameters that can be passed in the hit function:

    Parameter

    Default value

    Type

    Description

    url *

    string

    Current page URL

    options

    Object

    options fields
    options.callback

    Function

    The callback function to call after sending pageview data

    options.ctx

    Object

    Context accessed by the this keyword in the callback function

    options.params

    Object

    Session parameters

    options.referer

    string

    The URL that the user loaded the current page contents from

    options.title document.title

    string

    Title of the current page

    options.params 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

    Example of calling the hit function
    ym(XXXXXX, 'init', {});
    //...
    ym(XXXXXX, 'hit', '#contacts', {
        title: 'Contact information',
        referer: 'http://example.com/#main'
    });

Sending conversions

To track events on the site that do not change the page URL, use the reachGoal function:
ym(XXXXXX, 'reachGoal', 'TARGET_NAME);

Examples of setting goals in the code snippet.

Transmitting session parameters and user parameters

ym(XXXXXX, 'params', {param1: 'param_value1'})

Transmitting e-commerce data

To transmit e-commerce data, enable this feature in Yandex.Metrica: Enabling E-commerce.

To collect e-commerce data correctly, you need to insert the hit function once when the user goes to a new page.
Example of sending e-commerce data
dataLayer.push({
    "ecommerce": {
        "purchase": {
            "actionField": {
                "id" : "TRX987"
            },
            "products": [
                {
                    "id": "25341",
                    "name": "Yandex men's sweatshirt",
                    "price": 1345.26,
                    "brand": "Yandex",
                    "category": "Clothing/Mens clothing/Hoodies and sweatshirts",
                    "variant": "Orange"
                },
                {
                    "id": "25314",
                    "name": "Yandex women's sweatshirt",
                    "price": 1543.62,
                    "brand": "Yandex",
                    "category": "Clothing/Women's clothing/Sweatshirts and hoodies",
                    "variant": "White",
                    "quantity": 3
                }
            ]
        }
    }
});

Enabling Session Replay, click maps, link maps, and Form Analysis

You can connect Session Replay 2.0, click maps and link maps. The scroll map, form analysis, and Session Replay 1.0 are not supported.

If you were unable to independently check the tag, follow the recommendations below.

This might happen for the following reasons:

  • The tag is installed incorrectly. For example, the CMS modified the code snippet. Reinstall the tag or contact the support service for your CMS.
  • Broken scripts are preventing the Yandex.Metrica tag from working on the site. You can check this in the browser console.

This means that information is being transmitted to Yandex.Metrica. However, the data might not be shown in reports for any of the following reasons:

  • Data is sent to a tag with a different number.
  • The Filters tab in the tag settings has overly strict filters defined. Remove unneeded filters.
  • The Filters tab in the tag settings has the Don't count my sessions filter enabled. This means that the tag doesn't register your own sessions. Try accessing the site with your browser in “incognito” mode.