templates/block/ga_consent.html.twig line 1

Open in your IDE?
  1. {% if env.site == env.mix or env.site == env.mixopt %}
  2. <script>
  3.   window.dataLayer = window.dataLayer || [];
  4.   function gtag(){ dataLayer.push(arguments); }
  5.   // Инициализация по умолчанию
  6.   gtag('consent', 'default', {
  7.     'ad_storage': 'denied',
  8.     'ad_user_data': 'denied',
  9.     'ad_personalization': 'denied',
  10.     'analytics_storage': 'denied'
  11.   });
  12.   function setConsent(consent) {
  13.     document.cookie = "my_consent=" + consent + "; path=/; max-age=" + (60*60*24*365);
  14.     if (consent === 'granted') {
  15.       gtag('consent', 'update', {
  16.         'ad_storage': 'granted',
  17.         'ad_user_data': 'granted',
  18.         'ad_personalization': 'granted',
  19.         'analytics_storage': 'granted'
  20.       });
  21.     } else {
  22.       gtag('consent', 'update', {
  23.         'ad_storage': 'denied',
  24.         'ad_user_data': 'denied',
  25.         'ad_personalization': 'denied',
  26.         'analytics_storage': 'denied'
  27.       });
  28.     }
  29.     //document.getElementById('cookie-banner').style.display = 'none';
  30.   }
  31.   function getConsent() {
  32.     const name = "my_consent=";
  33.     const decoded = decodeURIComponent(document.cookie);
  34.     const ca = decoded.split(';');
  35.     for(let i = 0; i < ca.length; i++) {
  36.       let c = ca[i];
  37.       while (c.charAt(0) === ' ') {
  38.         c = c.substring(1);
  39.       }
  40.       if (c.indexOf(name) == 0) {
  41.         return c.substring(name.length, c.length);
  42.       }
  43.     }
  44.     return null;
  45.   }
  46.   document.addEventListener('DOMContentLoaded', function() {
  47.     const consent = getConsent();
  48.     if (consent) {
  49.       // уже есть выбор — обновим consent mode
  50.       setConsent(consent);
  51.     } else {
  52.       // показать баннер
  53.       //document.getElementById('cookie-banner').style.display = 'block';
  54.     }
  55.   });
  56. </script>
  57. {% endif %}