Skip to content

Instantly share code, notes, and snippets.

@antonlukin
Last active December 17, 2021 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonlukin/2f94168af6e86c7ae9f14d57d3b1e639 to your computer and use it in GitHub Desktop.
Save antonlukin/2f94168af6e86c7ae9f14d57d3b1e639 to your computer and use it in GitHub Desktop.
How to send ga events for special in knife.media

Для отслеживания события нужно добавить в глобальную переменную dataLayer специальный объект вида:

dataLayer = window.dataLayer || [];
dataLayer.push({
  'event': 'event-to-ga',
  'eventCategory': category,
  'eventAction': action,
  'eventLabel': label,
  'eventValue': value,
});
  • category – для спецпроектов всегда Special actions,
  • action – место и действие (например, Header click – клик по хедеру или Results share или Document scroll)
  • label – опционально. Уточняющее место действия (например, logo)
  • value – опционально. Численное значение, связанное с событием (например, 42 – можно использовать для результата)

Регистр названий важен! Для категорий и действия первое слово начинается с прописной, для label – со строчной.

(function () {
dataLayer = window.dataLayer || [];
function sendEvent(category, action, label) {
dataLayer.push({
'event': 'event-to-ga',
'eventCategory': category,
'eventAction': action,
'eventLabel': label
});
}
document.querySelector('.header__logo-askona').addEventListener('click', function () {
sendEvent('Special actions', 'Header click', 'logo');
});
document.querySelector('.about__content-button').addEventListener('click', function () {
sendEvent('Special actions', 'Landing click', 'more');
});
document.querySelector('.cover__description-button').addEventListener('click', function () {
sendEvent('Special actions', 'Landing click', 'start');
});
document.getElementById('sharing__arrow').addEventListener('click', function () {
sendEvent('Special actions', 'Results click', 'again');
});
document.querySelector('.result__description-button').addEventListener('click', function () {
sendEvent('Special actions', 'Results click', 'more');
});
document.addEventListener('click', function(e) {
var target = e.target || e.srcElement;
if (target.hasAttribute('data-ga')) {
sendEvent('Special actions', 'Promo click', target.getAttribute('data-ga'));
}
});
var nexts = document.querySelectorAll('.next');
for (var i = 0; i < nexts.length; i++) {
nexts[i].addEventListener('click', function () {
sendEvent('Special actions', 'Quiz click', this.getAttribute('data-quiz'));
});
}
var social = document.querySelectorAll('.footer .social__link');
for (var i = 0; i < social.length; i++) {
social[i].addEventListener('click', function () {
sendEvent('Special actions', 'Social click', this.getAttribute('data-label'));
});
}
var sharing = document.querySelectorAll('.result__share .sharing__link');
for (var i = 0; i < sharing.length; i++) {
sharing[i].addEventListener('click', function () {
sendEvent('Special actions', 'Results share', this.getAttribute('data-label'));
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment