Why we moved from hardcoded gtag snippets to Google Tag Manager in our Next.js app

posted 1 min read

For the first year of building XreplyAI, our tracking setup was the standard mess: gtag scripts in _app.tsx, conversion events scattered across components, and a tacit rule that any tracking change needed a full deploy cycle.

It worked, but it had a real cost: we consistently under-instrumented. Adding a conversion event for a new experiment didn't feel worth a PR when you're moving fast solo.

The switch to GTM

The migration was about 2 hours. The short version:

  1. Replace inline gtag calls with window.dataLayer.push() calls
  2. Move all tag configs (Google Ads, GA4, any new pixels) into a GTM container
  3. Publish the container — done

What actually changed

No deploys for tracking changes. New pixel, new conversion event, tweaked trigger — all done in GTM in minutes. This sounds minor until you ship 3 new conversion events in a week without opening your codebase once.

Preview mode is a real debugger. GTM shows every tag, trigger, and variable state as you click through your app in real time. We caught two misfiring conversion events we didn't know existed.

Explicit load order. Async gtag snippets fire whenever the browser gets around to them. GTM lets you sequence tags so dependencies are guaranteed to load before dependents run.

One place to audit everything. One dashboard instead of grepping through _app.tsx and a dozen component files.

Worth it at early stage?

Yes — especially if you're running any paid acquisition. The ops overhead of tracking living in your codebase compounds as you add channels. Moving it out early is much easier than untangling it later.

We're building XreplyAI — AI social media tool for solo founders. Happy to share the full GTM container structure in the comments if useful.

More Posts

Google Tag Manager GTM Setup Verification Guide

Ramandeep Singh - Aug 10, 2025

Bridging the Silence: Why Objective Data Outperforms Subjective Health Reports in Elderly Care

Huifer - Jan 27

Is Google Meet HIPAA Compliant? Healthcare Video Conferencing Guide

Huifer - Feb 14

Your App Feels Smart, So Why Do Users Still Leave?

kajolshah - Feb 2

The Privacy Gap: Why sending financial ledgers to OpenAI is broken

Pocket Portfolioverified - Feb 23
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!