What it does
Track how people find your site, what they do on it, and whether they buy. Google Analytics 4 (GA4) handles traffic and behavior tracking. Meta Pixel handles Facebook/Instagram ad attribution. Meta Conversions API (CAPI)sends events server-side so your tracking survives ad blockers. Together, they give you a complete picture of what's working and what isn't.
Think of it as three layers. GA4is the behavior layer — it tells you what users do on your site (which pages they visit, where they drop off, how long they stay). Meta Pixel and CAPIform the attribution layer — they tell you which marketing efforts actually drive revenue. UTM tracking is the glue that connects your content to conversions, letting you trace every sale back to the specific post or ad that brought the customer in.
Server-side tracking through CAPI is critical because ad blockers now block 30–40% of client-side events. If you only track in the browser, Meta's algorithm is blind to a huge chunk of your conversions — which means worse ad targeting, wasted spend, and inaccurate reporting.
Why we use it
You can't improve what you don't measure. Analytics tells you which content drives traffic, which pages convert, and where people drop off. Attribution tracking shows which ad spend and social posts actually generate revenue — so you can double down on what works and cut what doesn't. Without server-side tracking through CAPI, you're blind to 30–40% of conversions that ad blockers hide from client-side scripts.
Without attribution, you're guessing which content works. You might think your Instagram posts drive sales when it's actually Google organic. You might pour money into Facebook ads when your newsletter converts better. Analytics removes the guessing — you see the exact path from first touch to purchase, and you make decisions based on data instead of intuition.
Setup checklist
Create a Google Analytics 4 property
Go to analytics.google.com. Create an account if you don't have one, then create a new property. Choose Web as your platform and enter your domain. GA4 generates a Measurement ID that looks like G-XXXXXXXXXX— copy this. You'll need it to connect your app.
During setup, GA4 asks about your business objectives. Pick the ones closest to your goals — this customizes the default reports. Don't overthink it; you can change these later. The important thing is getting the Measurement ID.
Add GA4 to your Next.js app
Create a GoogleTag component that loads the gtag.js script. Use next/script with strategy="afterInteractive"so it doesn't block your page from rendering. Pass your Measurement ID from an environment variable (NEXT_PUBLIC_GA_ID) — never hardcode it. Add the component to your root layout so it loads on every page.
The component should return nullif the environment variable isn't set. This way GA4 loads in production but doesn't fire during local development, keeping your analytics data clean.
Set up GA4 custom events
GA4 tracks page views automatically— you don't need to add anything for that. What you do need to add are custom events for key actions in your funnel. These are the moments that matter: a visitor clicks your CTA, starts checkout, completes a purchase.
Fire events with gtag('event', 'cta_click', { tier: 'ship' }) for button clicks. gtag('event', 'purchase', { value: 79, currency: 'USD' }) for completed sales. Then in GA4 Admin, go to Events > Mark as conversion for the events that represent real business outcomes. This tells GA4 which events are your goals, not just noise.
Create a Meta Business Suite account
Go to business.facebook.comand create a Business Account if you don't have one. This is required before you can create a Meta Pixel. You'll need a personal Facebook account to create the business account, but they stay separate — your personal profile isn't visible to customers.
Meta Business Suite is the central hub for managing your Pixel, ad accounts, Instagram business profile, and conversion data. Even if you're not running paid ads yet, set this up now so your Pixel starts collecting audience data from day one. When you do run ads later, Meta's algorithm will already have data to optimize against.
Create a Meta Pixel
In Meta Business Suite, go to Events Manager > Connect Data Sources > Web > Meta Pixel. Name it after your product and click create. You'll get a Pixel ID— a long number that identifies your tracking pixel. Save this as NEXT_PUBLIC_META_PIXEL_ID in your environment variables.
Add Meta Pixel to your app
Create a MetaPixel component that loads the fbq script. Add it to your root layout alongside the GA4 component. The base code initializes the Pixel with your ID and fires a PageView event automatically on every page load.
For specific conversion events, call fbq('track', 'Purchase', { value: 79, currency: 'USD' }) when a purchase completes, fbq('track', 'InitiateCheckout') when someone clicks your buy button, and fbq('track', 'ViewContent') on product pages. These are Meta's standard events— using them (instead of custom names) unlocks Meta's built-in optimization for each event type.
Set up Meta Conversions API (CAPI)
CAPI sends events server-side, bypassing ad blockers entirely. This is critical — client-side tracking alone misses 30–40% of events because of browser privacy tools and ad blockers. Without CAPI, Meta's algorithm doesn't see a huge chunk of your conversions, which means worse ad targeting and wasted spend.
In Events Manager, go to Settings > Generate Access Token. Save this token as META_CAPI_TOKENin your server-side environment variables (not NEXT_PUBLIC_ — this token must stay server-side). Create a server function that sends events to https://graph.facebook.com/v19.0/{pixel_id}/events with your access token. This function runs in your API routes or server actions, not in the browser.
Implement key conversion events with deduplication
Track these events on both the client-side Pixel and server-side CAPI: ViewContent (product pages), InitiateCheckout (click buy button), Purchase (after payment succeeds). The key to making this work is deduplication.
Generate a unique event_idfor each event (a UUID works). Send that same event_id from both the Pixel call and the CAPI call. Meta uses this ID to recognize they're the same event and counts it only once. Without deduplication, every conversion gets double-counted, which destroys your reporting accuracy and wastes ad budget on false signals.
For CAPI events, include user datafor better matching: hashed email address, IP address, and user agent. The more data you provide, the higher Meta's Event Match Quality score, and the better your ad optimization will perform.
Set up UTM tracking
Add UTM parameters to every marketing link you share: ?utm_source=instagram&utm_medium=social&utm_campaign=launch. This tells GA4 exactly where each visitor came from. Without UTMs, social traffic shows up as "direct" or "referral" with no useful detail.
On your site, capture UTM parameters on arrival and store them in localStorage with a 60-day expiration window. When a visitor eventually purchases (maybe days or weeks later), pass those stored UTMs to your Stripe checkout metadata and analytics events. This gives you full attribution— you can trace every sale back to the specific post, ad, or link that brought the customer to your site.
Configure GA4 conversions and audiences
In GA4 Admin, go to Events and mark your key events as conversions: purchase, signup, cta_click. This surfaces them in conversion reports and lets you track conversion rates across different traffic sources, landing pages, and campaigns.
Set up Audiencesfor retargeting. Create an audience for "visited pricing page but didn't purchase" — these are your warmest leads. Create another for "purchased in the last 30 days" — these are candidates for upsells or referral asks. If you connect GA4 to Google Ads later, these audiences become targeting options for your ad campaigns.
Test everything
Use GA4 DebugView(Realtime > DebugView in your GA4 property) to verify events fire correctly. Install the Meta Pixel Helper Chrome extension to verify Pixel events appear on each page. In Events Manager, use Test Events to verify your CAPI events are arriving server-side.
Test the full funnel: land on a page (PageView fires in GA4 and Meta), click a CTA (cta_click fires in GA4, ViewContent fires in Meta), proceed to checkout (InitiateCheckout fires in Meta), complete a test purchase (purchase fires in GA4, Purchase fires in both Meta Pixel and CAPI with matching event_ids). If any event is missing or doubled, fix it before you spend money on ads.
Set up dashboards and review cadence
In GA4, create a custom reportwith the metrics that matter: traffic sources, top landing pages, conversion rates by source, and revenue. Don't get lost in vanity metrics — the numbers that matter are: where did visitors come from, what did they do, and did they buy.
In Meta Events Manager, check your attribution data to see which ads and organic posts drive actual purchases, not just clicks. Review both dashboards weekly. The goal isn't to obsess over daily fluctuations — it's to spot trends over time. Which traffic sources are growing? Which pages have high drop-off? Where should you invest more time and money?
Pro tips
Use GA4's Explorations(not just standard reports) for funnel analysis — you can build custom funnels that show exactly where users drop off between landing on your site and completing a purchase.
Set up Google Search Console alongside GA4 to see which keywords bring traffic to your site. GA4 tells you what people do after they arrive. Search Console tells you what they searched to find you in the first place.
Create a Meta Custom Audienceof "visited pricing page but didn't buy" for retargeting ads. These are your warmest prospects — they showed purchase intent but didn't convert. A well-targeted retargeting ad to this audience will outperform any cold campaign.
Track scroll depthon long pages (blog posts, landing pages) to see if people actually read your content. If 80% of visitors never scroll past the first fold, your headline is working but your body copy isn't — or your page is too long.
GA4 component
// components/analytics/GoogleTag.tsx "use client"; import Script from "next/script"; const GA_ID = process.env.NEXT_PUBLIC_GA_ID; export function GoogleTag() { if (!GA_ID) return null; return ( <> <Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} strategy="afterInteractive" /> <Script id="gtag-init" strategy="afterInteractive"> {`window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','${GA_ID}');`} </Script> </> ); }
AI prompt to get started
I'm setting up analytics for my Next.js app. Help me implement: 1) Google Analytics 4 with custom events for CTA clicks and purchases, 2) Meta Pixel with standard events (ViewContent, InitiateCheckout, Purchase), 3) Server-side Meta Conversions API with proper event deduplication using matching event_ids, 4) UTM parameter capture and 60-day attribution storage in localStorage. Show me the components and server-side functions I need.
Mistakes to avoid
- ✕Adding analytics before having something to track — set it up when you have real pages and a payment flow, not at project kickoff
- ✕Not testing events before going live — use GA4 DebugView and Meta Pixel Helper to verify every event fires correctly
- ✕Forgetting server-side tracking (CAPI) — client-side tracking misses 30–40% of events due to ad blockers, which cripples your ad optimization
- ✕Not using UTM parameters — without UTMs, you can’t attribute conversions to specific campaigns, posts, or links
- ✕Tracking too many events — focus on the funnel: page view → CTA click → checkout → purchase. Everything else is noise until you have volume
- ✕Not deduplicating client + server events — send the same event_id from both Pixel and CAPI so Meta doesn’t double-count your conversions