/** * Google One Tap Initialization * Handles Google One Tap login and triggers seamless login events */ (function () { // Track Google Analytics events function trackGoogleAnalytics(eventName, category, label) { if (typeof window.googleOneTapConfig !== 'undefined' && window.googleOneTapConfig.analytics && window.googleOneTapConfig.analytics.enabled) { // Google Analytics 4 (gtag) if (typeof window.gtag !== 'undefined') { window.gtag('event', eventName, { event_category: category, event_label: label, }); } else if (typeof window.ga !== 'undefined') { // Universal Analytics (ga) window.ga('send', 'event', category, eventName, label); } } } // Handle Google One Tap credential response function handleCredentialResponse(response) { trackGoogleAnalytics('google_one_tap_login_attempt', 'One Tap Login', 'Login Attempt'); if (!response.credential) { trackGoogleAnalytics('google_one_tap_login_failed', 'One Tap Login', 'No Credential'); return; } // Send credential to server for verification const xhr = new XMLHttpRequest(); xhr.open('POST', window.googleOneTapConfig.ajax_url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); const data = new URLSearchParams({ action: 'google_one_tap_login', nonce: window.googleOneTapConfig.nonce, credential: response.credential, }); xhr.onload = function () { if (xhr.status === 200) { try { const ajaxResponse = JSON.parse(xhr.responseText); if (ajaxResponse.success) { trackGoogleAnalytics('google_one_tap_login_success', 'One Tap Login', 'Success'); // Check if user is admin - if so, refresh page to show admin bar if (ajaxResponse.data.user.is_admin) { window.location.reload(); return; } // For non-admin users, trigger seamless login const event = new CustomEvent('userLoggedIn', { detail: { user: ajaxResponse.data.user, }, }); document.dispatchEvent(event); // Reset login strip opacity const loginStrip = document.getElementById('unified-strip'); if (loginStrip) { loginStrip.style.opacity = '1'; } } else { trackGoogleAnalytics('google_one_tap_login_failed', 'One Tap Login', ajaxResponse.data || 'Unknown Error'); } } catch (e) { trackGoogleAnalytics('google_one_tap_login_error', 'One Tap Login', 'Parse Error'); } } else { trackGoogleAnalytics('google_one_tap_login_error', 'One Tap Login', 'AJAX Error'); } }; xhr.onerror = function () { trackGoogleAnalytics('google_one_tap_login_error', 'One Tap Login', 'Network Error'); }; xhr.send(data); } // Initialize Google One Tap function initGoogleOneTap() { // Check if user is already logged in (client-side check) if (document.body.classList.contains('logged-in')) { return; } // Check if Google One Tap is available if (typeof window.google === 'undefined' || !window.google.accounts || !window.google.accounts.id) { // Retry after a short delay in case the script is still loading setTimeout(initGoogleOneTap, 1000); return; } // Check if client ID is available if (!window.googleOneTapConfig || !window.googleOneTapConfig.client_id) { trackGoogleAnalytics('google_one_tap_config_error', 'One Tap Login', 'Missing Client ID'); return; } // Initialize Google One Tap window.google.accounts.id.initialize({ client_id: window.googleOneTapConfig.client_id, callback: handleCredentialResponse, auto_select: false, cancel_on_tap_outside: true, use_fedcm_for_prompt: true, // Enable FedCM for proper prompt handling on_display() { trackGoogleAnalytics('google_one_tap_prompt_displayed', 'One Tap Login', 'Prompt Displayed'); }, }); // Show the prompt window.google.accounts.id.prompt((notification) => { if (notification.isDisplayed()) { trackGoogleAnalytics('google_one_tap_prompt_displayed', 'One Tap Login', 'Success'); } else if (notification.isNotDisplayed()) { const reason = notification.getNotDisplayedReason(); trackGoogleAnalytics('google_one_tap_prompt_not_displayed', 'One Tap Login', reason); } else if (notification.isSkippedMoment()) { trackGoogleAnalytics('google_one_tap_prompt_skipped', 'One Tap Login', 'Skipped'); } }); } // Make initGoogleOneTap globally accessible window.initGoogleOneTap = initGoogleOneTap; // Initialize when DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initGoogleOneTap); } else { initGoogleOneTap(); } }()); ; /** * Google Discover and Top Stories Click Tracking * Tracks clicks on Discover and Top Stories buttons and sends events to Google Analytics */ (function () { /** * Send event to Google Analytics * * @param {string} eventName - The name of the event * @param {string} eventCategory - The category of the event * @param {string} eventLabel - The label for the event */ function sendGAEvent(eventName, eventCategory, eventLabel) { // Check if gtag is available if (typeof window.gtag === 'function') { window.gtag('event', eventName, { event_category: eventCategory, event_label: eventLabel, }); } } /** * Initialize tracking for all Google Discover and Top Stories links */ function initializeTracking() { // Track navigation social links (top bar) const navDiscoverLink = document.querySelector('.wp-social-link-google-discover a'); const navTopPostsLink = document.querySelector('.wp-social-link-google-top-posts a'); if (navDiscoverLink) { navDiscoverLink.addEventListener('click', () => { sendGAEvent('N Discover Clicked', 'Navigation', 'Google Discover'); }); } if (navTopPostsLink) { navTopPostsLink.addEventListener('click', () => { sendGAEvent('N Top Stories Clicked', 'Navigation', 'Google Top Stories'); }); } // Track single post buttons const postButtons = document.querySelectorAll('.wp-block-button--google-cta a'); postButtons.forEach((button) => { const ariaLabel = button.getAttribute('aria-label'); // Check if it's a Google Discover button if (ariaLabel === 'Google Discover') { button.addEventListener('click', () => { sendGAEvent('SP Follow On Discover Clicked', 'Single Post', 'Google Discover'); }); } // Check if it's a Google Top Posts button if (ariaLabel === 'Google Top Posts') { button.addEventListener('click', () => { sendGAEvent('SP Mark In Top Stories Clicked', 'Single Post', 'Google Top Stories'); }); } }); } // Initialize tracking when DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initializeTracking); } else { initializeTracking(); } }()); ;