(function() { /* Guard to prevent multiple execution */ if (window.__pegaAnalyticsInitialized) return; window.__pegaAnalyticsInitialized = true; /** * Main initialization function once PCore is confirmed to be ready. */ function initializePegaAnalytics() { console.log("Pega PCore found! Initializing analytics event listeners..."); try { const PCore = window.PCore; const pubSubUtils = PCore.getPubSubUtils(); const eventRegistry = PCore.getEvents(); /* Collect all event constants from standard categories */ const eventCategories = [ eventRegistry.getCaseEvent?.(), eventRegistry.getContainerEvent?.(), eventRegistry.getDataEvent?.() ].filter(Boolean); /* Extract unique event strings across all categories */ const caseEvents = eventRegistry.getCaseEvent?.(); const excludedEvents = new Set([ caseEvents?.CASE_OPENED ].filter(Boolean)); const allEvents = new Set(); eventCategories.forEach(category => { Object.keys(category).forEach(key => { if (typeof category[key] === 'string' && !excludedEvents.has(category[key])) { allEvents.add(category[key]); } }); }); if (allEvents.size === 0) { console.warn("No standard Pega PCore events found in event registry categories."); } else { console.log(`Subscribing to ${allEvents.size} discovered Pega events...`); } /* Dynamically subscribe to each discovered event type */ allEvents.forEach(eventType => { /* Unique tracking name for this individual subscription item */ const subscriptionItemName = `Analytics_${eventType}`; /** * PubSub callback executed when Pega triggers an event. * @param {Object} payload - Event payload containing context, state, metadata etc. */ const handlePegaEvent = (payload) => { console.log(`[Pega Event Captured] Type: ${eventType}`, payload ); /* --- PUSH TO YOUR ANALYTICS SOLUTION HERE --- */ }; /* Signature: subscribe(eventType, subscriptionItem, subscriptionItemName, subscribeOnce, contextName) */ pubSubUtils.subscribe( eventType, handlePegaEvent, subscriptionItemName, false /* subscribeOnce = false, to continuously capture events */ ); }); /* URL_NAVIGATION and SHOW_PAGE_DONE live under PUB_SUB_EVENTS constants, not in the standard event registry categories above. */ const { URL_NAVIGATION } = PCore.getConstants().PUB_SUB_EVENTS; const { SHOW_PAGE_DONE } = PCore.getConstants().PUB_SUB_EVENTS.UI_EVENTS; pubSubUtils.subscribe( URL_NAVIGATION, () => { const navigationUtils = PCore.getNavigationUtils?.(); const searchTerm = navigationUtils?.getURLParam?.('searchTerm'); console.log(`[Pega Event Captured] Type: ${URL_NAVIGATION}`, { eventType: URL_NAVIGATION, context: { searchTerm: searchTerm ?? null } }); /* --- PUSH TO YOUR ANALYTICS SOLUTION HERE --- */ }, 'Analytics_URL_NAVIGATION', false ); pubSubUtils.subscribe( SHOW_PAGE_DONE, ({ target, pageName, pageClass }) => { if (target !== 'app/primary' || !pageName) return; console.log(`[Pega Event Captured] Type: ${SHOW_PAGE_DONE}`, { eventType: SHOW_PAGE_DONE, context: { target, pageName, pageClass, urlPath: window.location.pathname + window.location.search } }); /* --- PUSH TO YOUR ANALYTICS SOLUTION HERE --- */ }, 'Analytics_SHOW_PAGE_DONE', false ); console.log("Successfully registered all Pega PubSub event listeners (including URL_NAVIGATION and SHOW_PAGE_DONE)."); } catch (error) { console.error("Error setting up Pega event subscriptions:", error); } } /** * Safely checks and waits for PCore object initialization. */ function waitForPCore() { const maxAttempts = 50; /* Safeguard limit (~10 seconds) */ let attempts = 0; const interval = setInterval(() => { attempts++; /* Check if PCore and its internal event/pubsub architectures are ready */ if (window.PCore && window.PCore.getEvents && window.PCore.getPubSubUtils) { clearInterval(interval); initializePegaAnalytics(); } else if (attempts >= maxAttempts) { clearInterval(interval); console.error("Pega PCore failed to load within the expected timeframe."); } }, 200); } /* Handle document lifecycle states */ if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", waitForPCore); } else { /* DOM is already parsed, jump straight to looking for PCore */ waitForPCore(); } })();