-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding useGoogleTagManager hook to @next/third-parties (#56106)
This PR adds the `useGoogleTagManage` hook to `@next/third-parties` repo.
- Loading branch information
1 parent
df1d4a1
commit 5e474a3
Showing
9 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use client' | ||
// TODO: Evaluate import 'client only' | ||
import React from 'react' | ||
import Script from 'next/script' | ||
|
||
declare global { | ||
interface Window { | ||
dataLayer?: Object[] | ||
[key: string]: any | ||
} | ||
} | ||
|
||
type GTMParams = { | ||
gtmId: string | ||
dataLayer: string[] | ||
dataLayerName: string | ||
auth: string | ||
preview: string | ||
} | ||
|
||
let currDataLayerName: string | undefined = undefined | ||
|
||
export function GoogleTagManager(props: GTMParams) { | ||
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer } = props | ||
|
||
if (currDataLayerName === undefined) { | ||
currDataLayerName = dataLayerName | ||
} | ||
|
||
const gtmLayer = dataLayerName !== 'dataLayer' ? `$l=${dataLayerName}` : '' | ||
const gtmAuth = auth ? `>m_auth=${auth}` : '' | ||
const gtmPreview = preview ? `>m_preview=${preview}>m_cookies_win=x` : '' | ||
|
||
return ( | ||
<> | ||
<Script | ||
id="_next-gtm-init" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
(function(w,l){ | ||
w[l]=w[l]||[]; | ||
w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'}); | ||
${dataLayer ? `w[l].push(${JSON.stringify(dataLayer)})` : ''} | ||
})(window,'${dataLayerName}');`, | ||
}} | ||
/> | ||
<Script | ||
id="_next-gtm" | ||
src={`https://www.googletagmanager.com/gtm.js?id=${gtmId}${gtmLayer}${gtmAuth}${gtmPreview}`} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
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` | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as GoogleMapsEmbed } from './GoogleMapsEmbed' | ||
export { default as YouTubeEmbed } from './YouTubeEmbed' | ||
export { default as GoogleMapsEmbed } from './google-maps-embed' | ||
export { default as YouTubeEmbed } from './youtube-embed' | ||
export { GoogleTagManager, sendGTMEvent } from './gtm' |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { GoogleTagManager, sendGTMEvent } from '@next/third-parties/google' | ||
|
||
const Page = () => { | ||
const onClick = () => { | ||
sendGTMEvent({ event: 'buttonClicked', value: 'xyz' }) | ||
} | ||
|
||
return ( | ||
<div class="container"> | ||
<GoogleTagManager gtmId="GTM-XYZ" /> | ||
<h1>GTM</h1> | ||
<button id="gtm-send" onClick={onClick}> | ||
Click | ||
</button> | ||
<GoogleTagManager gtmId="GTM-XYZ" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import { GoogleTagManager, sendGTMEvent } from '@next/third-parties/google' | ||
|
||
const Page = () => { | ||
const onClick = () => { | ||
sendGTMEvent({ event: 'buttonClicked', value: 'xyz' }) | ||
} | ||
|
||
return ( | ||
<div class="container"> | ||
<GoogleTagManager gtmId="GTM-XYZ" /> | ||
<h1>GTM</h1> | ||
<button id="gtm-send" onClick={onClick}> | ||
Click | ||
</button> | ||
<GoogleTagManager gtmId="GTM-XYZ" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |