Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/components/google-analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<script src={gtagSource} async />
<script async data-ga-measurement-id={config.measurementId} src={gtagSource} />
<script>{inlineConfig}</script>
</>
)
Expand Down
11 changes: 11 additions & 0 deletions tests/stage-7-seo-feed-integrations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<GoogleAnalytics config={integrations.ga4} />)

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(
Expand Down