import { useEffect, useRef, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { Cookie, Check, X } from 'lucide-react';
import {
hasExplicitConsentChoice,
initAnalytics,
setConsent,
track,
trackPageView,
} from '../lib/analytics';
/**
* AnalyticsBridge — the single page_view seam for the SPA.
*
* Mount it once, inside the Router, as a sibling of . It also boots GA4
* on first mount so early page_views are not lost.
*/
export function AnalyticsBridge() {
const location = useLocation();
const prevPath = useRef(null);
useEffect(() => {
initAnalytics();
}, []);
useEffect(() => {
const path = `${location.pathname}${location.search}`;
if (prevPath.current === path) return;
const referrer = prevPath.current === null ? document.referrer : '';
prevPath.current = path;
trackPageView(path, document.title || 'GetCert', referrer);
}, [location.pathname, location.search]);
return null;
}
declare global {
interface Window {
/** Set by scripts/prerender.mjs so the generated HTML has no UI chrome. */
__GETCERT_PRERENDER__?: boolean;
}
}
/**
* ConsentBanner — shown until the visitor makes an explicit choice.
*
* `initAnalytics` runs with DEFAULT_CONSENT, so a visitor who ignores the
* banner is still tracked (opt-out). Pressing Decline revokes it: Google
* consent mode flips to denied and first-party events stop being sent.
*/
export function ConsentBanner() {
// Resolved during the first render so the banner never flashes into the
// prerendered HTML (script/chrome on every page would dilute the copy).
const [visible, setVisible] = useState(
() => !window.__GETCERT_PRERENDER__ && !hasExplicitConsentChoice(),
);
const [dismissed, setDismissed] = useState(false);
if (!visible || dismissed) return null;
const decide = (state: 'granted' | 'denied') => {
// Decline must be recorded while tracking is still on, otherwise the
// event is swallowed by the consent gate.
if (state === 'denied') track('consent_declined');
setConsent(state);
if (state === 'granted') track('consent_accepted');
setVisible(false);
setDismissed(true);
};
return (
We use cookies. GetCert uses
analytics cookies to understand which courses and pages are useful. You can decline and
keep using the site. See our{' '}
privacy policy
.