Google Tag Manager
Push events to Google Tag Manager's dataLayer from the server. Combine server-side data collection with client-side tag management.
Nextlytics is a server-side analytics library for Next.js. No client JavaScript, no cookies, GDPR compliant. Learn more →
Configuration
import { Nextlytics } from "@nextlytics/core/server";
import { googleTagManagerBackend } from "@nextlytics/core/backends/gtm";
export const { middleware, analytics } = Nextlytics({
backends: [
googleTagManagerBackend({
// Required. Your GTM Container ID.
// Find it: GTM -> Admin -> Container Settings
containerId: "GTM-XXXXXXX",
}),
],
});How It Works
The GTM backend uses a hybrid approach:
- On initial page load, it injects the GTM snippet and pushes initial data to
dataLayer - On client-side navigations, it pushes virtual pageview events to
dataLayer - Custom events are pushed to
dataLayerfor your GTM tags to process
This gives you the best of both worlds: server-side data collection with client-side tag management.
Event Mapping
Page Views
Page views push to dataLayer with the page_view event:
dataLayer.push({
event: "page_view",
eventId: "nl_abc123",
page_path: "/products",
page_title: "Products",
page_location: "example.com/products",
});page_location is built from serverContext.host + serverContext.path.
Custom Events
Custom events are converted to snake_case and pushed to dataLayer:
await api.sendEvent("formSubmission", {
props: { formId: "newsletter" },
});Becomes:
dataLayer.push({
event: "form_submission",
eventId: "nl_xyz789",
formId: "newsletter",
});User Identification
When a user is identified, their data is pushed to dataLayer:
export const { middleware, analytics } = Nextlytics({
callbacks: {
getUser: async (ctx) => {
const session = await getSession(ctx);
if (!session?.user) return undefined;
return {
userId: session.user.id,
traits: { plan: "pro" },
};
},
},
backends: [googleTagManagerBackend({ containerId: "GTM-XXXXXXX" })],
});Results in:
dataLayer.push({
userId: "user_123",
userTraits: { plan: "pro" },
});GTM Tag Setup
In Google Tag Manager, create tags that fire on your dataLayer events:
GA4 Event Tag
- Create a new GA4 Event tag
- Set trigger to "Custom Event" with event name
page_view - Map dataLayer variables to GA4 parameters
Facebook Pixel Tag
- Create a Facebook Pixel tag
- Set trigger to fire on
purchaseevents - Use dataLayer variables for conversion value
DataLayer Variables
Create these GTM variables to access Nextlytics data:
| Variable Name | Type | DataLayer Variable Name |
|---|---|---|
| NL Event ID | Data Layer Variable | eventId |
| NL User ID | Data Layer Variable | userId |
| NL Page Path | Data Layer Variable | page_path |
Debugging
Use GTM's Preview mode to inspect dataLayer pushes:
- Open GTM and click "Preview"
- Enter your site URL
- Navigate your site and watch events appear in the debugger
Limitations
- Requires client JavaScript: GTM must run in the browser
- No server-side tags: All tag firing happens client-side
- No event updates: Once pushed to dataLayer, events cannot be modified
Ready to add server-side analytics?
Get started with Nextlytics in 3 simple steps.