diff --git a/src/components/google-analytics.tsx b/src/components/google-analytics.tsx index d6db5f6..86e68b0 100644 --- a/src/components/google-analytics.tsx +++ b/src/components/google-analytics.tsx @@ -10,16 +10,11 @@ export function GoogleAnalytics({ config }: GoogleAnalyticsProps) { } const gtagSource = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(config.measurementId)}` - const inlineConfig = ` -window.dataLayer = window.dataLayer || []; -function gtag(){dataLayer.push(arguments);} -gtag('js', new Date()); -gtag('config', '${config.measurementId}'); -` + const inlineConfig = `window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());var el=document.querySelector('script[data-ga-measurement-id]');if(el){var id=el.getAttribute('data-ga-measurement-id');if(id)gtag('config',id);}` return ( <> - ) diff --git a/tests/stage-7-seo-feed-integrations.test.tsx b/tests/stage-7-seo-feed-integrations.test.tsx index f10b2ad..5c7b3a6 100644 --- a/tests/stage-7-seo-feed-integrations.test.tsx +++ b/tests/stage-7-seo-feed-integrations.test.tsx @@ -68,6 +68,17 @@ describe("stage 7 seo feed and integration gates", () => { expect(integrations.kakao.enabled).toBe(true) }) + it("Given enabled GA config When rendering GoogleAnalytics Then scripts are properly configured with data attribute", () => { + const integrations = getPublicIntegrations({ + NEXT_PUBLIC_GA_MEASUREMENT_ID: "G-ABC123DEF4", + }) + const markup = renderToStaticMarkup() + + expect(markup).toContain('data-ga-measurement-id="G-ABC123DEF4"') + expect(markup).toContain("https://www.googletagmanager.com/gtag/js?id=G-ABC123DEF4") + expect(markup).toContain("gtag('config',id)") + }) + it("Given missing public integration env When rendering gates Then no broken scripts or iframes render", () => { const integrations = getPublicIntegrations({}) const markup = renderToStaticMarkup(