getcert-backend / src /components /AnalyticsBridge.tsx
ggop213's picture
feat: deploy Gemma 4 optimizations and backend updates to HF Spaces
b69dd51
Raw History Blame Contribute Delete
4.11 kB
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 <App />. It also boots GA4
* on first mount so early page_views are not lost.
*/
export function AnalyticsBridge() {
const location = useLocation();
const prevPath = useRef<string | null>(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 (
<div
role="region"
aria-label="Cookie and analytics consent"
className="fixed inset-x-0 bottom-0 z-[95] border-t border-slate-200 bg-white/95 backdrop-blur px-4 py-4 sm:px-6"
style={{ boxShadow: '0 -8px 30px rgba(15, 23, 42, 0.12)' }}
>
<div className="mx-auto flex max-w-5xl flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<div className="flex items-start gap-3">
<Cookie className="mt-0.5 h-4 w-4 shrink-0 text-[#1d4ed8]" aria-hidden="true" />
<p className="text-xs leading-relaxed text-slate-600">
<span className="font-semibold text-slate-900">We use cookies.</span> GetCert uses
analytics cookies to understand which courses and pages are useful. You can decline and
keep using the site. See our{' '}
<a href="/privacy" className="font-medium text-[#1d4ed8] underline underline-offset-2">
privacy policy
</a>
.
</p>
</div>
<div className="flex shrink-0 items-center gap-2">
<button
type="button"
onClick={() => decide('denied')}
className="inline-flex items-center gap-1.5 rounded-xl border border-slate-300 bg-white px-3.5 py-2 text-xs font-semibold text-slate-700 transition-colors hover:bg-slate-50"
>
<X className="h-3.5 w-3.5" aria-hidden="true" />
Decline
</button>
<button
type="button"
onClick={() => decide('granted')}
className="inline-flex items-center gap-1.5 rounded-xl bg-[#1d4ed8] px-3.5 py-2 text-xs font-semibold text-white transition-colors hover:bg-[#1743c4]"
>
<Check className="h-3.5 w-3.5" aria-hidden="true" />
Accept
</button>
</div>
</div>
</div>
);
}