Skip to content

Components: Analytics

Google Tag Manager and GA4 ecommerce events, gated by Consent Mode v2, and the Core Web Vitals reporter.

Generated from apps/theme/src/components/analytics/: 6 files, 6 components. Import a component through the @/components/… path shown, so a fork can override it in src/custom/.

@/components/analytics/deferred-google-tag-manager

apps/theme/src/components/analytics/deferred-google-tag-manager.tsx client component

<DeferredGoogleTagManager>

Mounts <GoogleTagManager> (gtm.js + the GA4 tag) only AFTER the page is idle or the visitor first interacts — keeping that ~245 ms of third-party script evaluation out of the LCP/hydration window (it otherwise loads afterInteractive, right in the post-hydration main-thread contention that starves the mobile LCP paint).

This does NOT touch Consent Mode: the inline consent-default script in <GoogleAnalytics> runs at HTML parse time (before this gate ever mounts), so dataLayer/gtag and the denied defaults are set first. Events pushed to window.dataLayer before gtm.js loads (view_item, add_to_cart, web-vitals) are queued and flushed when gtm.js evaluates — see @next/third-parties' sendGTMEvent ("queue up events before GTM init"). So deferring the loader loses no events and preserves consent ordering.

PropTypeDescription
gtmContainerIdstring

@/components/analytics/google-analytics

apps/theme/src/components/analytics/google-analytics.tsx server component

<GoogleAnalytics>

Loads Google Tag Manager with Google Consent Mode v2.

Order matters: the inline consent script is emitted in the initial HTML and runs at parse time — before GTM loads. GTM itself is mounted by <DeferredGoogleTagManager>, which delays gtm.js until idle / first interaction to keep it off the LCP window (see that file). It defines dataLayer / gtag and sets the consent default to denied, so no analytics/ads cookies are written until the visitor accepts. It then reads the persisted cookie-consent choice and, for a returning visitor who already accepted, immediately upgrades the relevant signals to granted (so consent survives reloads). Subsequent changes flow through updateConsentMode() from the cookie banner.

Renders nothing when no container ID is configured (analytics disabled). CSP permits the inline script via script-src 'unsafe-inline'.

PropTypeDescription
gtmContainerIdstring

@/components/analytics/search-tracker

apps/theme/src/components/analytics/search-tracker.tsx client component

<SearchTracker>

Fires a GA4 search event for the search results page. Keyed on the term, so paging, sorting or filtering the same query doesn't count as a new search. Renders nothing.

PropTypeDescription
searchTermstring

@/components/analytics/view-item-list-tracker

apps/theme/src/components/analytics/view-item-list-tracker.tsx client component

<ViewItemListTracker>

Fires a GA4 view_item_list event when a product list renders. Re-fires only when the list or its contents change (pagination, sorting, filtering, switching tabs), never on an unrelated re-render. Renders nothing.

PropTypeDescription
listAnalyticsList
itemsAnalyticsItem[]
currency?string

@/components/analytics/view-item-tracker

apps/theme/src/components/analytics/view-item-tracker.tsx client component

<ViewItemTracker>

Fires a GA4 view_item event once per product view. Render output is null — it's a side-effect-only client component, mounted on the (server-rendered) product page next to <RecentlyViewedTracker>.

PropTypeDescription
skustring
name?string
price?number
currency?string
category?string

@/components/analytics/web-vitals

apps/theme/src/components/analytics/web-vitals.tsx client component

<WebVitals>

Real-user Core Web Vitals reporter. Subscribes (via next/web-vitals, which wraps the web-vitals library) to LCP / CLS / INP / FCP / TTFB — plus Next's own hydration/render custom metrics — and sends each sample to GA4 through the GTM dataLayer (trackWebVital), next to the ecommerce events.

Field data, the counterpart to the simulated PageSpeed lab score: what actual visitors experience on their real devices/networks. Cloudflare Web Analytics measures the same vitals at the edge with no code here. Renders nothing; only mounted when GTM is configured (layout).

No props.