M
M
mediagenia2014-06-19 21:07:25
AMD
mediagenia, 2014-06-19 21:07:25

How to connect Yandex.Metrica in RequireJS?

Colleagues, tell me how to load Yandex.Metrika in RequireJS ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2014-09-20
@mediagenia

Just wondered about this too. I tried this, it seems to work:
1. in the HTML body, only

<noscript><div><img src="//mc.yandex.ru/watch/XXXXXX?ut=noindex" style="position:absolute; left:-9999px;" alt="" /></div></noscript>

2. in the require config in paths, add the path for the metric script (I suggested the name "yametrika", you can name this "module" whatever you like): "yametrika": "//mc.yandex.ru/metrika/watch"
3. in the first loaded module, you need to specify this "yametrika" in the dependencies, but it is not necessary to assign his response variable:
define([ 'backbone','jquery', ... ,"yametrika"], function(...){}

4. in the function that is called there, insert the initialization of the Metrics object:
try {
  window.yaCounterXXXXXX = new Ya.Metrika(
    {
      id:XXXXXX
      , trackHash:true
      , ut:"noindex"
    }
  );
} catch(e) { }

Everything, then you can, for example, indicate the achievement of goals:
Upd. some visitors have script blockers enabled that cut off the loading of Yandex.Metrica. Feel free to anticipate the error in loading this script.
For example, replacing it, in case of failure, with a “dummy” loaded from your server, specifying it in paths:
where the metric methods are described:
...
return {
  addFileExtension:function(){}
  , clickmap: function(){}
  , enableAll: function(){}
  , extLink: function(){}
  , file:  function(){}
  , hit:  function(){}
  , replacePhones:function(){}
  , notBounce: function(){}
  , reachGoal: function(){}
  , trackLinks: function(){}
  , params: function(){}
};

or by simply handling the error in the require config:
requirejs.onError = function(err){ 
    if( err.requireModules.length == 1  &&  err.requireModules[0] == "yametrika") { ... } else throw err;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question