Template
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F22Ms5Cxam5UHyxaCrtZzu
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import type { ConsentConfig, ShopAnalytics } from "@shopify/hydrogen";
|
|
import { usePathname, useSearchParams } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
|
|
import { AnalyticsEvent, configureAnalytics, getAnalytics } from "@/lib/analytics";
|
|
|
|
/**
|
|
* Root analytics tracker (`hydrogen-analytics` / `references/react.md` Next.js
|
|
* App Router shape). Keys the `PAGE_VIEWED` effect by `pathname + "?" + search`
|
|
* so client navigations fire a fresh page view (F9: no polling). Wrapped in
|
|
* `<Suspense>` in `Providers` so the `useSearchParams()` CSR bailout is scoped
|
|
* to the tracker, not the whole layout.
|
|
*/
|
|
export function AnalyticsTracker({
|
|
shop,
|
|
consent,
|
|
}: {
|
|
shop: ShopAnalytics;
|
|
consent: ConsentConfig;
|
|
}) {
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const pageKey = `${pathname}?${searchParams?.toString() ?? ""}`;
|
|
|
|
useEffect(() => {
|
|
configureAnalytics(shop, consent);
|
|
const analytics = getAnalytics();
|
|
if (!analytics) return;
|
|
analytics.publish(AnalyticsEvent.PAGE_VIEWED);
|
|
}, [pageKey, shop, consent]);
|
|
|
|
return null;
|
|
}
|