mod: вынести cookies и счётчики в static js
This commit is contained in:
39
public/static/js/accept-cookies.js
Normal file
39
public/static/js/accept-cookies.js
Normal file
@@ -0,0 +1,39 @@
|
||||
(function (window, document) {
|
||||
'use strict';
|
||||
|
||||
// Защита от повторного подключения, если файл случайно вставят дважды.
|
||||
if (window.__cadpointAcceptCookiesLoaded) {
|
||||
return;
|
||||
}
|
||||
window.__cadpointAcceptCookiesLoaded = true;
|
||||
|
||||
var COOKIE_NAME = 'cookie_accept';
|
||||
var COOKIE_VALUE = 'yes';
|
||||
var COOKIE_TTL_MS = 7_948_800_000;
|
||||
var bannerId = 'cookies_accept';
|
||||
var buttonId = 'cookies_accept_button';
|
||||
|
||||
function acceptCookies() {
|
||||
// Сохраняем согласие тем же ключом, что и раньше, чтобы серверная логика не менялась.
|
||||
var cookieAcceptDate = new Date();
|
||||
cookieAcceptDate.setTime(cookieAcceptDate.getTime() + COOKIE_TTL_MS);
|
||||
document.cookie = COOKIE_NAME + '=' + COOKIE_VALUE + ';expires=' + cookieAcceptDate;
|
||||
|
||||
var banner = document.getElementById(bannerId);
|
||||
if (banner) {
|
||||
banner.remove();
|
||||
}
|
||||
}
|
||||
|
||||
function bindAcceptButton() {
|
||||
var button = document.getElementById(buttonId);
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
|
||||
button.addEventListener('click', acceptCookies);
|
||||
}
|
||||
|
||||
bindAcceptButton();
|
||||
})(window, document);
|
||||
|
||||
67
public/static/js/footer-counters.js
Normal file
67
public/static/js/footer-counters.js
Normal file
@@ -0,0 +1,67 @@
|
||||
(function (window, document) {
|
||||
'use strict';
|
||||
|
||||
// Защита от повторной инициализации, если файл случайно подключат дважды.
|
||||
if (window.__cadpointFooterCountersLoaded) {
|
||||
return;
|
||||
}
|
||||
window.__cadpointFooterCountersLoaded = true;
|
||||
|
||||
function appendScript(src, id) {
|
||||
// Не создаём дубликаты, если скрипт уже есть в DOM.
|
||||
if (id && document.getElementById(id)) {
|
||||
return null;
|
||||
}
|
||||
var script = document.createElement('script');
|
||||
script.async = true;
|
||||
script.src = src;
|
||||
if (id) {
|
||||
script.id = id;
|
||||
}
|
||||
(document.head || document.body).appendChild(script);
|
||||
return script;
|
||||
}
|
||||
function initGoogleAnalytics() {
|
||||
// Поддерживаем старый UA-код, чтобы не ломать текущую статистику.
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
window.gtag = window.gtag || function gtag() {
|
||||
window.dataLayer.push(arguments);
|
||||
};
|
||||
window.gtag('js', new Date());
|
||||
window.gtag('config', 'UA-9116991-1');
|
||||
appendScript('https://www.googletagmanager.com/gtag/js?id=UA-9116991-1', 'cadpoint-gtag-js');
|
||||
}
|
||||
|
||||
function initYandexMetrika() {
|
||||
// Повторяем стандартный паттерн Metrika: сначала очередь вызовов, потом загрузка внешнего файла.
|
||||
window.ym = window.ym || function ym() {
|
||||
(window.ym.a = window.ym.a || []).push(arguments);
|
||||
};
|
||||
window.ym.l = 1 * new Date();
|
||||
window.ym(198477, 'init', {
|
||||
clickmap: true,
|
||||
trackLinks: true,
|
||||
accurateTrackBounce: true,
|
||||
webvisor: true,
|
||||
});
|
||||
appendScript('https://mc.yandex.ru/metrika/tag.js', 'cadpoint-yandex-metrika-js');
|
||||
}
|
||||
|
||||
function initMailRuCounter() {
|
||||
// Rating Mail.ru тоже любит сначала получить очередь событий, а затем уже внешний код.
|
||||
window._tmr = window._tmr || [];
|
||||
window._tmr.push({
|
||||
id: '1612438',
|
||||
type: 'pageView',
|
||||
start: (new Date()).getTime(),
|
||||
});
|
||||
appendScript('https://top-fwz1.mail.ru/js/code.js', 'topmailru-code');
|
||||
}
|
||||
|
||||
// Вызываем функции-счетчики
|
||||
initGoogleAnalytics();
|
||||
initYandexMetrika();
|
||||
initMailRuCounter();
|
||||
|
||||
})(window, document);
|
||||
|
||||
Reference in New Issue
Block a user