Skip to content

Commit cd22544

Browse files
junzero741claude
andcommitted
feat(frontend): replace manual GTM script with @next/third-parties
- Add @next/third-parties package for official Next.js GTM integration - Replace inline script + noscript iframe with GoogleTagManager component - Add NEXT_PUBLIC_GTM_ID ARG to Dockerfile so it's inlined at build time Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f646b37 commit cd22544

4 files changed

Lines changed: 23 additions & 20 deletions

File tree

apps/frontend/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ COPY packages/shared/ ./packages/shared/
2020
# NEXT_PUBLIC_ vars are inlined at build time
2121
ARG NEXT_PUBLIC_API_URL
2222
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
23+
ARG NEXT_PUBLIC_GTM_ID
24+
ENV NEXT_PUBLIC_GTM_ID=$NEXT_PUBLIC_GTM_ID
2325

2426
# Build frontend (includes shared via dependsOn)
2527
RUN pnpm nx build frontend

apps/frontend/src/app/layout.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './global.css';
22
import { Inter } from 'next/font/google';
3-
import Script from 'next/script';
3+
import { GoogleTagManager } from '@next/third-parties/google';
44
import type { Metadata } from 'next';
55

66
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID;
@@ -48,26 +48,8 @@ export default function RootLayout({
4848
}) {
4949
return (
5050
<html lang="ko" className={inter.variable}>
51-
{GTM_ID && (
52-
<Script id="gtm" strategy="afterInteractive">
53-
{`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
54-
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
55-
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
56-
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
57-
})(window,document,'script','dataLayer','${GTM_ID}');`}
58-
</Script>
59-
)}
6051
<body className="flex min-h-screen flex-col font-sans">
61-
{GTM_ID && (
62-
<noscript>
63-
<iframe
64-
src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`}
65-
height="0"
66-
width="0"
67-
style={{ display: 'none', visibility: 'hidden' }}
68-
/>
69-
</noscript>
70-
)}
52+
{GTM_ID && <GoogleTagManager gtmId={GTM_ID} />}
7153
<header className="border-b border-border">
7254
<div className="mx-auto flex max-w-3xl items-center gap-2.5 px-4 py-4">
7355
<svg

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
},
5555
"dependencies": {
5656
"@aws-sdk/client-s3": "^3.999.0",
57+
"@next/third-parties": "^16.1.6",
5758
"@prisma/adapter-pg": "^7.4.1",
5859
"@prisma/client": "^7.4.1",
5960
"@tailwindcss/typography": "^0.5.19",

pnpm-lock.yaml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)