ignitionstack.pro v1.0 is out! Read the announcement →
Skip to Content

Analytics Setup

The boilerplate ships with dual instrumentation: Google Analytics 4 for page views/funnels and Mixpanel for product events. This page explains how to enable both.

For detailed event docs and tracking examples, see the Analytics section.

Architecture

src/app/ ├── components/ │ └── analytics/ │ ├── GoogleAnalytics.tsx # GA4 script │ └── MixpanelProvider.tsx # Mixpanel provider ├── lib/ │ ├── analytics/ │ │ ├── ga.ts # GA4 helper functions │ │ ├── mixpanel.ts # Mixpanel helper functions │ │ └── index.ts # Unified exports │ └── hooks/ │ └── useAnalytics.ts # Tracking hook └── app/ └── layout.tsx # Providers configured

Environment Variables

.env.local
# Google Analytics 4 NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX # Mixpanel NEXT_PUBLIC_MIXPANEL_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Environment (disables tracking in dev) NEXT_PUBLIC_APP_ENV=development

Google Analytics 4

Create a GA4 property

  1. Acesse analytics.google.com 
  2. Admin → Create Property
  3. Escolha “Web” como plataforma
  4. Copie o Measurement ID (G-XXXXXXXXXX)

Configure the data stream

  1. Admin → Data Streams → Add Stream
  2. Configure the site domain
  3. Habilite Enhanced Measurement

Add it to the project

The GoogleAnalytics component is already wired inside the layout:

src/app/layout.tsx
import { GoogleAnalytics } from '@/components/analytics/GoogleAnalytics' export default function RootLayout({ children }) { return ( <html> <body> {children} <GoogleAnalytics /> </body> </html> ) }

GoogleAnalytics component

src/components/analytics/GoogleAnalytics.tsx
'use client' import Script from 'next/script' export function GoogleAnalytics() { const gaId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID if (!gaId || process.env.NEXT_PUBLIC_APP_ENV === 'development') { return null } return ( <> <Script src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`} strategy="afterInteractive" /> <Script id="google-analytics" strategy="afterInteractive"> {` window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '${gaId}', { page_path: window.location.pathname, }); `} </Script> </> ) }

GA4 tracking helpers

src/lib/analytics/ga.ts
type GAEvent = { action: string category: string label?: string value?: number } export function trackEvent({ action, category, label, value }: GAEvent) { if (typeof window === 'undefined' || !window.gtag) return window.gtag('event', action, { event_category: category, event_label: label, value: value, }) } export function trackPageView(url: string) { if (typeof window === 'undefined' || !window.gtag) return window.gtag('config', process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID!, { page_path: url, }) }

Mixpanel

Create a Mixpanel project

  1. Acesse mixpanel.com 
  2. Create New Project
  3. Copy the project token

Add the provider

src/app/layout.tsx
import { MixpanelProvider } from '@/components/analytics/MixpanelProvider' export default function RootLayout({ children }) { return ( <html> <body> <MixpanelProvider> {children} </MixpanelProvider> </body> </html> ) }

Mixpanel provider

src/components/analytics/MixpanelProvider.tsx
'use client' import { useEffect } from 'react' import mixpanel from 'mixpanel-browser' export function MixpanelProvider({ children }: { children: React.ReactNode }) { useEffect(() => { const token = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN if (token && process.env.NEXT_PUBLIC_APP_ENV !== 'development') { mixpanel.init(token, { debug: false, track_pageview: true, persistence: 'localStorage', }) } }, []) return <>{children}</> }

Mixpanel tracking helpers

src/lib/analytics/mixpanel.ts
import mixpanel from 'mixpanel-browser' export function trackMixpanelEvent( event: string, properties?: Record<string, unknown> ) { if (process.env.NEXT_PUBLIC_APP_ENV === 'development') { console.log('[Mixpanel]', event, properties) return } mixpanel.track(event, properties) } export function identifyUser(userId: string, traits?: Record<string, unknown>) { if (process.env.NEXT_PUBLIC_APP_ENV === 'development') return mixpanel.identify(userId) if (traits) { mixpanel.people.set(traits) } } export function resetUser() { if (process.env.NEXT_PUBLIC_APP_ENV === 'development') return mixpanel.reset() }

Unified hook

Use the useAnalytics hook to simplify usage:

src/lib/hooks/useAnalytics.ts
'use client' import { useCallback } from 'react' import { trackEvent as trackGA } from '@/lib/analytics/ga' import { trackMixpanelEvent } from '@/lib/analytics/mixpanel' interface TrackEventParams { name: string category?: string properties?: Record<string, unknown> } export function useAnalytics() { const track = useCallback(({ name, category, properties }: TrackEventParams) => { // GA4 trackGA({ action: name, category: category || 'general', label: properties?.label as string, }) // Mixpanel trackMixpanelEvent(name, { category, ...properties, }) }, []) return { track } }

Usage inside components

src/components/BuyButton.tsx
'use client' import { useAnalytics } from '@/lib/hooks/useAnalytics' export function BuyButton({ productId, price }: Props) { const { track } = useAnalytics() function handleClick() { track({ name: 'purchase_click', category: 'ecommerce', properties: { productId, price, currency: 'BRL', }, }) } return <button onClick={handleClick}>Buy</button> }

E-commerce

EventWhenProperties
view_itemProduct pageitem_id, item_name, price
add_to_cartAdd to cartitem_id, quantity, price
begin_checkoutStart checkoutitems, value
purchasePurchase completedtransaction_id, value, items

Engagement

EventWhenProperties
sign_upSign upmethod
loginLoginmethod
page_viewPage viewpage_path, page_title
clickKey clickselement, destination

Debug Mode

GA4 Debug

Install the Google Analytics Debugger  extension and enable it.

Mixpanel Debug

// In development the events are logged in the console if (process.env.NEXT_PUBLIC_APP_ENV === 'development') { console.log('[Mixpanel]', event, properties) }

Verification

GA4

  1. Open Admin → DebugView in GA4
  2. Browse the site
  3. Events should show up in realtime

Mixpanel

  1. Open Live View in Mixpanel
  2. Perform actions on the site
  3. Events should appear within seconds

Troubleshooting

Events do not show up in GA4

Mixpanel does not initialize

Duplicate tracking

Resources