Skip to content

Commit

Permalink
Fix: (third-parties) sendGTMEvent not queueing events before GTM init (
Browse files Browse the repository at this point in the history
…#68683) (#72111)

Backports: #68683

Co-authored-by: Develliot <[email protected]>
  • Loading branch information
ijjk and Develliot authored Nov 13, 2024
1 parent 91cbf9d commit 515055a
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions packages/third-parties/src/google/gtm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import Script from 'next/script'

import type { GTMParams } from '../types/google'

let currDataLayerName: string | undefined = undefined
let currDataLayerName = 'dataLayer'

export function GoogleTagManager(props: GTMParams) {
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer } = props

if (currDataLayerName === undefined) {
currDataLayerName = dataLayerName
}
currDataLayerName = dataLayerName

const gtmLayer = dataLayerName !== 'dataLayer' ? `&l=${dataLayerName}` : ''
const gtmAuth = auth ? `&gtm_auth=${auth}` : ''
Expand Down Expand Up @@ -53,17 +51,10 @@ export function GoogleTagManager(props: GTMParams) {
)
}

export const sendGTMEvent = (data: Object) => {
if (currDataLayerName === undefined) {
console.warn(`@next/third-parties: GTM has not been initialized`)
return
}

if (window[currDataLayerName]) {
window[currDataLayerName].push(data)
} else {
console.warn(
`@next/third-parties: GTM dataLayer ${currDataLayerName} does not exist`
)
}
export const sendGTMEvent = (data: Object, dataLayerName?: string) => {
// special case if we are sending events before GTM init and we have custom dataLayerName
const dataLayer = dataLayerName || currDataLayerName
// define dataLayer so we can still queue up events before GTM init
window[dataLayer] = window[dataLayer] || []
window[dataLayer].push(data)
}

0 comments on commit 515055a

Please sign in to comment.