function detectPageType() { const pageElement = document.querySelector('.page'); if (pageElement) { return pageElement.classList.contains('artGroup') ? 'plp' : pageElement.classList.contains('brand') ? 'brand' : 'Unknown page type'; } return 'Unknown page type'; } function initializeBackNavigationTracking() { let historyStack = [document.location.href]; // Initialize with the current URL let isBackNavigation = false; let lastPageType = null; // Internal variable to track the last page type // Track the last page type based on dataLayer events function trackLastPageTypeFromDataLayer() { window.dataLayer = window.dataLayer || []; // Function to update the lastPageType variable based on dataLayer events function updatePageType(event) { if (event.event === 'plp.loaded') { lastPageType = 'plp'; } else if (event.event === 'pdp.loaded') { lastPageType = 'pdp'; } } // Listen for new events pushed to the dataLayer const originalPush = window.dataLayer.push; window.dataLayer.push = function() { const args = Array.prototype.slice.call(arguments); args.forEach(event => updatePageType(event)); return originalPush.apply(this, args); }; // Check existing dataLayer entries (in case the events have already occurred) window.dataLayer.forEach(event => updatePageType(event)); } function trackBackNavigation(event) { const currentUrl = document.location.href; const pageType = detectPageType(); // Get the current page type if (isBackNavigation) { // Track the page type from dataLayer during back navigation trackLastPageTypeFromDataLayer(); console.log('Back navigation from page type:', lastPageType); window._uxa = window._uxa || []; if(lastPageType == 'pdp'){ window._uxa.push(['setCustomVariable', 1, 'pagetype', pageType , 4]); window._uxa.push(['trackPageview', currentUrl]); } isBackNavigation = false; // Reset after tracking } else { // Normal forward or new navigation historyStack.push(currentUrl); } } // Listen for popstate events to detect back navigation window.addEventListener('popstate', function(event) { isBackNavigation = true; trackBackNavigation(event); }); // Override pushState and replaceState to track forward/new navigations (function(history) { const pushState = history.pushState; history.pushState = function(state, title, url) { const result = pushState.apply(history, arguments); isBackNavigation = false; // Set to false because it's a forward navigation trackBackNavigation({ state }); return result; }; const replaceState = history.replaceState; history.replaceState = function(state, title, url) { const result = replaceState.apply(history, arguments); isBackNavigation = false; // Set to false because it's a forward navigation trackBackNavigation({ state }); return result; }; })(window.history); } (function() { let isInitialized = false; function initialize() { if (isInitialized) return; // Prevent multiple initializations isInitialized = true; initializeBackNavigationTracking(); } // Trigger the initialization on DOM ready document.addEventListener('DOMContentLoaded', initialize); // Trigger the initialization on pdp_ajax_loaded and plp_ajax_loaded events window.addEventListener('pdp_ajax_loaded', initialize); window.addEventListener('plp_ajax_loaded', initialize); })();