DEV Community

Ashish kumawat Global Performance Marketer
Ashish kumawat Global Performance Marketer

Posted on • Originally published at ashishkumawat.com on

GA4 Server-Side Tracking: How to Set Up Google Analytics 4 Measurement Protocol in Next.js

In the modern performance marketing landscape, cookie consent, ad-blockers, and browser-side tracking protections (like Safari's Intelligent Tracking Prevention) severely degrade the accuracy of standard Google Analytics. Traditional GA4 configurations running inside Google Tag Manager (GTM) in the user's browser frequently fail to capture up to 30% of actual customer leads. For digital agencies and performance consultants, this data gap makes it impossible to optimize campaigns or calculate true ROAS. The solution is GA4 Server-Side Tracking. By sending event signals directly from our backend server to Google's analytics servers using the GA4 Measurement Protocol , we achieve 100% data reliability. In this technical walkthrough, we document how Ashish Kumawat—an Advanced Analytics Expert—successfully deployed GA4 server-side tracking on a custom Next.js website.

Step 1: Creating the Measurement Protocol API Secret in GA4

Before we can send backend events to Google Analytics, we must authorize our server. This is done by creating a secure API secret key. In the Google Analytics 4 Admin settings under Data Streams, select your web stream and navigate to Measurement Protocol API Secrets. Here, we created a new secret named NextJS Server API. This secret, along with your GA4 Measurement ID, allows our server-side code to make secure, authenticated POST requests straight to Google's measurement collectors, bypassing client-side constraints legally:

Google Analytics 4 Measurement Protocol API Secret settings

Step 2: Coding the Next.js API Route for GA4 Server Events

Once the API Secret was generated, we updated our Next.js backend utility (lib/serverTracking.js). The code parses the client's request cookies to extract the Google Analytics client identifier (_ga). This client ID is critical because it tells Google Analytics that the server-side event belongs to the same user browser session. We then construct the GA4 Measurement Protocol payload. When a user submits our lead form, the Next.js server executes a POST request to https://www.google-analytics.com/mp/collect in the background, passing the client ID and the custom event name generate_lead along with custom event parameters like lead_goal and form_name.

Step 3: Verification in GA4 Realtime Overview

To test the integration, we submitted a live test form on our website. In Google Analytics, we opened the **Realtime Overview** dashboard and navigated to the **Event count by Event name** card. Within seconds of submission, the custom event generate_lead appeared live in the dashboard with a count of 2. Clicking into the event parameters and navigating to lead_goal , we verified that the dynamically selected service value (e.g. Full Growth Strategy) was transmitted perfectly:

GA4 Realtime dashboard showing generate_lead event

GA4 lead_goal parameter value showing Full Growth Strategy

Step 4: Elevating the Event to a Key Event (Conversion)

Finally, to use this event for optimizing Google Ads campaigns, it must be marked as a conversion. In the GA4 Admin panel under **Data display > Events**, we located the generate_lead event and marked it as a **Key Event** by highlighting its star icon. Marking the server event as a key event ensures it appears in conversions reports and can be imported directly into Google Ads, enabling bidding algorithms to optimize for actual backend leads rather than raw browser clicks:

GA4 Admin panel marking generate_lead as a Key Event

Unlock the Power of Server-Side Data Analytics

Server-side tracking is no longer optional for high-performing brands. By setting up GA4 Measurement Protocol, you bypass ad-blockers, preserve client site performance, and feed clean, accurate conversion data to your ad campaigns, lowering your CPA. If you target premium international markets like the USA, UK, Canada, Australia, and Dubai (UAE), this technical setup is your ultimate competitive asset. As an Advanced Analytics and Tracking Expert, I design custom integrations that protect your ad budgets and scale your revenue. Book your free digital growth audit today and let's optimize your marketing stack.

Top comments (0)