Examples of sending session parameters

Transmitting traffic indicators
Let's say that your site contains various articles and you want to know which authors bring the biggest audience to your site. To do this, you can transmit data about author views in the parameters:
<script type="text/javascript">
window.yaParams = { "Author": 123 };
ym(XXXXXX, 'params', window.yaParams||{});
</script>
Or, for example, you may want to know what categories of articles users view the most. You can transmit data about category views in the parameters:
<script type="text/javascript">
window.yaParams = { "Category": 123 };
ym(XXXXXX, 'params', window.yaParams||{});
</script>
Transmitting a hierarchical data structure

For instance, we need to transmit data and show it in a report in a tree view.

<script type="text/javascript">
window.yaParams = {
    "level1":{"level2":["level3_1","level3_2"]}}
...
ym(XXXXXX, 'params', window.yaParams||{});
</script>
AB testing

Let’s say we need to conduct an experiment to determine how the color of the Buy button affects the conversion rate for a «View basket» goal.

If the user is shown a red button, we use the following parameter:

window.yaParams = {ab_test: "red"};

If the user is shown a green button, we use the following parameter:

window.yaParams = {ab_test: "green"};

This parameter is transmitted in any convenient way. For example, it can be passed when initializing the tag on product pages or using the reachGoal method that is called when the Buy button is clicked.

After this, we will be able to use the ab_test parameter value in the Yandex.Metrica interface for selecting the corresponding data.

Report on authorized and unauthorized users

Use the following code sample to analyze differences in the behavior of registered and non-registered site users:

<script type="text/javascript">
window.yaParams = {'Логин': '[% вставка логина посетителя из шаблонизатора сайта %]' || 'Гость'};
ym(XXXXXX, 'params', window.yaParams||{});
</script>

where XXXXXX is the tag ID.

Transmitting an additional parameter when a goal is achieved

For example, if you have a single form on every page of a site, you can track the pages where it's filled in most often. To do this, create a JavaScript event (form submission) goal and configure the transmission of the page address.

The user's browser knows and stores in the document.location.href parameter information about which page the visitor is currently on, so you can get this information and send it to Yandex.Metrica when the goal is completed:
ym(XXXXXX, 'reachGoal', 'TARGET', {URL: document.location.href})
where
  • XXXXXX is the tag ID.
  • TARGET is the ID of the «JavaScript event» goal.
  • URL is the first-level session parameter. Any name can be set.
  • document.location.href is the second-level session parameter that will be used to transmit the address of the page where the user performed the target action.

If you want to track different actions, such as clicking on two different phone numbers, create two goals (one for each number) and transmit each with its own document.location.href parameter.

ym(XXXXXX, 'reachGoal', 'TARGET', {URL: document.location.href})
...
ym(XXXXXX, 'reachGoal', 'TARGET2', {URL2: document.location.href})