// 1. Inicjalizacja obu warstw danych window.dataLayer = window.dataLayer || []; window.dataLayer_GTM = window.dataLayer_GTM || []; // 2. Zachowanie oryginalnej funkcji push domyślnego dataLayer var originalPush = window.dataLayer.push; // 3. Nadpisanie funkcji push z wbudowanym filtrem window.dataLayer.push = function() { // ZAWSZE wysyłaj dane do domyślnego dataLayer (niech wtyczka GA działa tam po swojemu) originalPush.apply(window.dataLayer, arguments); var item = arguments[0]; var shouldClone = false; // Reguła A: Przechwytywanie komend Google (Consent Mode) // Funkcja gtag() wrzuca do dataLayer specjalne obiekty (Arguments). // Sprawdzamy, czy pierwszym elementem jest np. 'consent'. if (item && item.length !== undefined && typeof item[0] === 'string') { var cmd = item[0]; if (cmd === 'consent' || cmd === 'set' || cmd === 'js' || cmd === 'config') { shouldClone = true; } } // Reguła B: Przechwytywanie eventów OneTrust // Sprawdzamy, czy wrzucany obiekt ma parametr 'event' zawierający słowo OneTrust if (item && typeof item === 'object' && item.event && typeof item.event === 'string') { var eventName = item.event; if (eventName.indexOf('OneTrust') !== -1 || eventName === 'OptanonLoaded') { shouldClone = true; } } // Jeśli to Consent Mode lub OneTrust -> skopiuj do dataLayer_GTM // Jeśli to 'purchase' lub 'add_to_cart' z wtyczki GA -> zignoruj (shouldClone pozostaje false) if (shouldClone) { window.dataLayer_GTM.push.apply(window.dataLayer_GTM, arguments); } }; function OptanonWrapper() { }