📜 ⬆️ ⬇️

How to duplicate goals from Yandex. Metrics to Google Analytics

What to do if you set up a large number of goals in Yandex.Metrica through Google Tag Manager (GTM), and you need to urgently transfer these goals as events in Google Analytics (GA)? Of course, you can reconfigure each tag to send events to the GA and to the Metric in parallel, but if there are a lot of goals in the Metric, this will take time.

The reverse situation is possible, when the targets were not configured by you, but by the developers in the site code itself. And you, as a company analyst, need to set up sending the same events to GA, and you have only GTM at your fingertips. In this case, even with a small number of goals, difficulties may arise. Triggers of events in GTM will need to be customized as much as possible, so that goals in Metric and events in GA will follow the same logic, and there are no significant differences in different analytics systems.

In both cases, more than one hour will need to spend, setting up everything manually. But you can find a more practical option. There is a function that allows you to intercept the sending of goals to the Metric and simultaneously send the push to the dataLayer. Thus, you will not need to remember or understand how the sending of goals to Yandex.Metrica was set up - all you have to do is to use the necessary function. The working version of the function will depend on which version of the Yandex.Metrica counter you have installed.

How to distinguish the new counter Metrics from the old


To determine the working version of the function, you need to understand which version of the counter is installed on your site.
')
If your meter was installed earlier than 10.12.2018 and was not updated later, then you have the old version:

<script type="text/javascript" > (function(d, w, c) { (w[c] = w[c] || []).push(function() { try { w.yaCounterXXXXXXXX = new Ya.Metrika({ id: XXXXXXXX, clickmap: true, trackLinks: true, accurateTrackBounce: true, webvisor: true }); } catch (e) {} }); var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function() { n.parentNode.insertBefore(s, n); }; s.type = "text/javascript"; s.async = true; s.src = "https://mc.yandex.ru/metrika/watch.js"; if (w.opera == "[object Opera]") { d.addEventListener("DOMContentLoaded", f, false); } else { f(); })(document, window, "yandex_metrika_callbacks"); </script> 

Where:

XXXXXXXX - the number of your counter.
w.yaCounterXXXXXXXX in the code makes it clear that this is the old version of the counter.
An example of sending a target in the old counter:

 <script> yaCounterXXXXXXXX.reachGoal('TARGET_NAME'); </script> 

Where:

XXXXXXXX - your counter number;
TARGET_NAME is the target ID.

The new version of the counter appeared 10/12/2018.

It looks like this:

 <script type="text/javascript" > (function(m, e, t, r, i, k, a) { m[i] = m[i] || function() { (m[i].a = m[i].a || []).push(arguments) }; m[i].l = 1 * new Date(); k = e.createElement(t), a = e.getElementsByTagName(t)[0], k.async = 1, k.src = r, a.parentNode.insertBefore(k, a) })(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym"); ym(XXXXXXXX, "init", { clickmap: true, trackLinks: true, accurateTrackBounce: true, webvisor: true }); </script> 

Where:

XXXXXXXX - the number of your counter.
ym (XXXXXXXX in the code makes it clear that this is the new version of the counter.
An example of sending a target in a new counter:

 <script> ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); </script> 

Where:

XXXXXXXX - your counter number;
TARGET_NAME is the target ID.

The function for the old version of Yandex. Metric


If you have an old version of the Yandex.Metrica counter installed (see above), then the function of intercepting sending targets will look like this:

 <script> function mainTagFunction(reachGoal) { yaCounterXXXXXXXX.reachGoal = function() { var param = arguments[0]; reachGoal.apply(this, arguments); //     dataLayer.push({ 'event': 'metrika', 'event_param': param }); }; } var yCounter = 0; function ySearch() { if (window.yaCounterXXXXXXXX) mainTagFunction(yaCounterXXXXXXXX.reachGoal); else { if (yCounter < 100) { setTimeout(ySearch, 100); yCounter++; } else console.log(' .  '); } } ySearch(); </script> 

Where:

XXXXXXXX - your counter number;

The code is triggered by each call to the yaCounterXXXXXXXX function with the reachGoal method. The function produces a push in the dataLayer, when pushing, the event name is passed (metrika) and the parameter, the target identifier is passed in the parameter.

The function for the new version of Yandex. Metric


If you have installed a new version of the Yandex.Metrica counter, then the function of intercepting sending targets looks like this:

 <script> function mainTagFunction(reachGoal) { ym = function() { var param = arguments[2]; var method = arguments[1]; reachGoal.apply(this, arguments); //     if (method == 'reachGoal') dataLayer.push({ 'event': 'metrika', 'event_param': param }); }; } var yCounter = 0; function ySearch() { if (window.ym) mainTagFunction(ym); else { if (yCounter < 100) { setTimeout(ySearch, 100); yCounter++; } else console.log(' .  '); } } ySearch(); </script> 

It is triggered each time the ym () function is called and, if the reachgoal method was called, sends a push to the dataLayer. The name of the event (metrika) and the parameter are passed to the dataLayer, and the target identifier is passed in the event_param parameter.

Setting up to send events to GA via GTM


In order for GTM to set up sending events with Metric goals in GA, you must:

1. Create a trigger " View page - DOM is ready ", in addition, you can specify on which pages you need to duplicate goals from Metrics to GA



2. Create the “ Custom HTML ” tag and insert the necessary function code into it. On this tag the trigger created earlier is installed.



3. Create a trigger " Custom event " with the name of the event metrika.



4. Create a variable " Data level variable " with the name as shown in the example:



5. Create a Google Analytics - Universal Analytics tag with a trigger and a variable that were created earlier.



Where:

UA-XXXXXXXX-XX - your Analytics counter number

6. Publish a GTM container.



With these settings, Google Analytics will receive events with an event category - Metrika, and the actions for these events will be goal identifiers from Yandex.Metrica.



Check settings


To check the correctness of the settings for sending events to GA, enter the following code in the console of your browser:

a) For the new Metrics counter

 ym(XXXXXX, 'reachGoal', 'TARGET_NAME') 

Where:

XXXXXXXX - the number of your counter in Yandex.Metrica.

b) For the old Metrics counter

 yaCounterXXXXXXXX.reachGoal('TARGET_NAME'); 

Where:

XXXXXXXX - the number of your counter in Yandex.Metrica.

The console in each browser opens in its own way, for example, for Chrome, the console opens with the key combination Ctrl + Shift + j :



After pressing Enter, your event should go not only to Metric, but also to GA.

To make sure of this, one should open the “Events” report in the “ Real-time ” section:



If everything was configured correctly, then you will see a line with the values ​​Metrika and TARGET_NAME.

Congratulations, you've set up goal transfer from Metrics to Google Analytics!

The material was prepared for the portal Cossa.

Source: https://habr.com/ru/post/458328/


All Articles