Skip to content

Instantly share code, notes, and snippets.

@meded90
Last active March 31, 2019 16:22
Show Gist options
  • Save meded90/0f1e233d9925177a60b6 to your computer and use it in GitHub Desktop.
Save meded90/0f1e233d9925177a60b6 to your computer and use it in GitHub Desktop.
универсальный триггер цели для метрики и гугл аналитикс
<script>
// ищим ид метрики и создем уневерсальый обробочик для нескольких ледингов
var noscript = document.getElementsByTagName('noscript');
var idMetrick = 0;
var idMail = 0;
if (noscript.length) {
for (var i = 0; i < noscript.length; i++) {
var el = noscript[i];
var mactchMetric = el.innerText.match(/mc\.yandex\.ru\/watch\/\d*/i);
if (mactchMetric && mactchMetric.length) {
idMetrick = mactchMetric[0].replace(/\D+/g, "")
}
var mactchMail = el.innerText.match(/mail\.ru\/counter\?id=\d*/i);
if (mactchMail && mactchMail.length) {
idMail = mactchMail[0].replace(/\D+/g, "")
}
}
}
window.triggerEventCounter = function (target, parametr) {
window.yaCounter = window["yaCounter" + idMetrick];
if (yaCounter) {
yaCounter.reachGoal(target, parametr)
}
if (window.ga) {
window.ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'lp', // Required.
'eventAction': 'target', // Required.
'eventLabel': target || 0
});
}
}
document.load = function () {
window.yaCounter = window["yaCounter" + idMetrick]
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment