Template
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F22Ms5Cxam5UHyxaCrtZzu
28 lines
765 B
TypeScript
28 lines
765 B
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
import { AnalyticsEvent, getAnalytics } from "@/lib/analytics";
|
|
import { useCart } from "@/lib/cart";
|
|
|
|
/**
|
|
* Publishes `CART_VIEWED` when the `/cart` page is viewed
|
|
* (`hydrogen-analytics` / `references/react.md`). The cart payload is
|
|
* `AnalyticsCart | null`: when a compatible cart is available, include `id` +
|
|
* `updatedAt` + connection-shaped `lines`; otherwise pass `cart: null`.
|
|
*/
|
|
export function CartViewedTracker() {
|
|
const cart = useCart((state) => state.data);
|
|
|
|
useEffect(() => {
|
|
const analytics = getAnalytics();
|
|
if (!analytics) return;
|
|
analytics.publish(AnalyticsEvent.CART_VIEWED, {
|
|
cart: cart.id ? cart : null,
|
|
prevCart: null,
|
|
});
|
|
}, [cart]);
|
|
|
|
return null;
|
|
}
|