// Use a simplified approach with just a Set for tracking const processedEvents = new Set(); // Listen for events from PartyTown window.addEventListener("gtm-forward", function (e) { const payload = e.detail; // Only process events from PartyTown that are marked as forwarded if (payload && payload._forwarded) { try { // Only forward native GTM events (those with an event property starting with 'gtm.') // Skip custom events as they're already being added to the dataLayer if (payload.event && payload.event.startsWith('gtm.')) { // Create an event identifier to avoid duplicates const eventId = JSON.stringify(payload); // Only process if we haven't seen this event before if (!processedEvents.has(eventId)) { // Track that we've seen this event processedEvents.add(eventId); // Clean up the payload const cleanPayload = { ...payload }; delete cleanPayload._forwarded; // Push native GTM event to main thread dataLayer window.dataLayer.push(cleanPayload); // Prevent memory leaks by limiting Set size if (processedEvents.size > 200) { processedEvents.clear(); } } } else { } } catch (e) { } } });