More UAE brands are moving from Shopify themes or WooCommerce to custom storefronts built with Next.js. They're fast and flexible, and you own every pixel. But there's a catch most teams find out after launch: the tracking that worked on your old site quietly breaks. Ads stop learning, GA4 undercounts, and nobody notices for weeks.
Here's why it happens and how to set up marketing and tracking properly.
Why a Next.js store is different
On a traditional site, every click loads a new page. Every tag (Google Analytics, Google Ads, Meta Pixel, TikTok) fires again on each page load.
A Next.js store is a single-page application after the first load. When a shopper goes from the homepage to a product page to the cart, the browser doesn't reload. Next.js swaps the content and updates the URL using the browser's History API.
Google Tag Manager's standard "Page View" trigger fires once, on the first load. Everything after that is invisible to any tag that relies on it.
| What breaks | What you'll see |
|---|---|
| Meta Pixel PageView and ViewContent | Only fires on the landing page; retargeting audiences shrink |
| Google Ads remarketing | Product-level audiences don't build |
| Custom GTM tags on "Page View" | Fire once per session instead of once per page |
| Conversion tags on a thank-you page | Never fire if the shopper reaches it by client-side navigation |
Step 1: Load GTM the Next.js way
Use the official @next/third-parties package rather than pasting the GTM snippet into a layout by hand:
// app/layout.tsx
import { GoogleTagManager } from '@next/third-parties/google'
export default function RootLayout({ children }) {
return (
<html lang="en">
<GoogleTagManager gtmId="GTM-XXXXXXX" />
<body>{children}</body>
</html>
)
}
Step 2: Tell GTM when the "page" changes
You have two options. Pick one, not both, or you'll double-count.
Option A: GTM's History Change trigger. In GTM, create a trigger of type History Change. Add it alongside "Page View" (or Initialization) on every tag that should fire per page: Meta PageView, TikTok, remarketing tags. GA4 also has its own setting, Page changes based on browser history events (under Enhanced measurement), which records these as page views automatically.
Option B: push your own event from Next.js (more control). Add a small client component that pushes a virtual page view to the dataLayer on every route change:
// app/route-tracker.tsx
'use client'
import { usePathname, useSearchParams } from 'next/navigation'
import { useEffect } from 'react'
export function RouteTracker() {
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
window.dataLayer = window.dataLayer || []
window.dataLayer.push({
event: 'virtual_page_view',
page_path: pathname + (searchParams.toString() ? `?${searchParams}` : ''),
page_title: document.title,
})
}, [pathname, searchParams])
return null
}
In GTM, create a Custom Event trigger for virtual_page_view, use it on your page-level tags, and turn off GA4's history-based page views so GA4 doesn't count each page twice. Wrap RouteTracker in a <Suspense> boundary in your layout, since it reads search params.
We prefer Option B for stores: you decide exactly when a page counts, and it fires after the new page title is set.
Step 3: Send e-commerce events with real data
GA4, Google Ads and Meta all need the same core events, pushed from your React components at the right moment:
| Event | When to push it |
|---|---|
view_item |
Product page renders with product data |
view_item_list |
Category or search results render |
add_to_cart |
The cart API confirms the item was added (not on button click) |
begin_checkout |
Checkout page loads |
add_shipping_info / add_payment_info |
Each checkout step completes |
purchase |
The order is confirmed by your server |
window.dataLayer.push({ ecommerce: null }) // clear the previous ecommerce object
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: order.id,
value: order.total,
currency: 'AED',
items: order.items.map(i => ({
item_id: i.sku, item_name: i.name, price: i.price, quantity: i.qty,
})),
},
})
Two rules that save a lot of pain:
- Always send
transaction_id. It lets GA4 and Google Ads ignore duplicates when a shopper refreshes the thank-you page. - Fire
purchaseonly after payment is confirmed. With Tabby, Tamara or card redirects, the shopper sometimes never returns to your site. Use server-side tracking (below) as the source of truth.
Step 4: About "workers": what helps and what doesn't
You'll hear two kinds of "workers" when people talk about Next.js tracking. They do very different things.
Web workers (Partytown and strategy="worker")
Next.js can load third-party scripts in a web worker using Partytown (<Script strategy="worker">). The idea is good: move heavy scripts like GTM off the main thread so your store stays fast and scores well on Core Web Vitals.
In practice, we don't recommend it for GTM on a store:
- Next.js marks the worker strategy as experimental, and it doesn't support the App Router.
- Scripts in a worker can't directly access the page. Calls like
dataLayer.pushmust be "forwarded" to the worker, and events pushed during client-side navigation have been known to go missing. - GTM Preview mode, some cookie handling and many tags (chat widgets, heatmaps) behave unpredictably.
Use strategy="afterInteractive" (what @next/third-parties does) or lazyOnload for non-essential scripts instead. You get most of the speed benefit without the tracking risk.
Server-side workers (server-side tagging)
This is the "worker" that really pays off. Instead of every tag running in the shopper's browser, your site sends one stream of events to your own tagging server: a server-side GTM container on Google Cloud Run, a hosted service, or an edge function such as a Cloudflare Worker. From there, the events go to GA4, Google Ads, Meta Conversions API and TikTok Events API.
Why it matters for a UAE store:
- Fewer tags in the browser, so the store is faster.
- More complete data. Ad blockers and browser privacy features block fewer first-party requests.
- Purchases tracked from the server. Your order webhook can send
purchaseeven when the shopper never comes back from the payment page. - Control over data. You decide which fields leave your system, which helps with consent and data-protection obligations.
Pair it with Google Consent Mode v2 and a proper consent banner, and with Enhanced Conversions for Google Ads and the Conversions API for Meta.
Step 5: Marketing a Next.js store (what Shopify used to do for you)
On Shopify, apps handle a lot of marketing plumbing. On a custom store, your team has to build it:
- Product feeds. Generate a feed for Google Merchant Center (Shopping and free listings) and a Meta/TikTok catalog from your product database. Without it, you can't run Shopping or dynamic product ads.
- SEO basics in code. Use
generateMetadatafor unique titles and descriptions per product,app/sitemap.tsfor a sitemap that updates itself, andProductstructured data (price, availability, reviews) for rich results. - Arabic and English. Use proper
/enand/arroutes withhreflangtags, RTL layout, and translated product data, not just a translated interface. - Server rendering for product pages. Make sure product and category pages are rendered on the server (or statically generated), so Google sees content without running JavaScript.
- Email and WhatsApp flows. Abandoned cart, back-in-stock and post-purchase messages need their own integrations and events.
- Speed as a sales tool. A fast Next.js store improves conversion rate and lowers your cost per sale on ads, so measure it.
Testing checklist before launch
- [ ] GTM Preview shows a page view on every navigation, not just the first
- [ ] No double page views in GA4 DebugView
- [ ]
add_to_cart,begin_checkoutandpurchaseshow the correct value and AED currency - [ ] Refreshing the thank-you page doesn't create a second purchase
- [ ] Meta Events Manager shows browser and server events deduplicated
- [ ] Merchant Center feed approved with no item errors
- [ ] Consent banner blocks marketing tags until the shopper accepts
Building or fixing a Next.js store?
We build custom e-commerce in Next.js and Shopify, and set up tracking that ad platforms can actually learn from. See our e-commerce optimisation services.
Sources: Next.js docs: Third Party Libraries · Partytown issue: dataLayer events on route change