-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Google analytics to next/third-parties (#58418)
Co-authored-by: Jiachi Liu <[email protected]>
- Loading branch information
1 parent
c2ab5f7
commit ce92cea
Showing
8 changed files
with
167 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client' | ||
// TODO: Evaluate import 'client only' | ||
import React, { useEffect } from 'react' | ||
import Script from 'next/script' | ||
|
||
import type { GAParams } from '../types/google' | ||
|
||
declare global { | ||
interface Window { | ||
dataLayer?: Object[] | ||
} | ||
} | ||
|
||
let currDataLayerName: string | undefined = undefined | ||
|
||
export function GoogleAnalytics(props: GAParams) { | ||
const { gaId, dataLayerName = 'dataLayer' } = props | ||
|
||
if (currDataLayerName === undefined) { | ||
currDataLayerName = dataLayerName | ||
} | ||
|
||
useEffect(() => { | ||
// performance.mark is being used as a feature use signal. While it is traditionally used for performance | ||
// benchmarking it is low overhead and thus considered safe to use in production and it is a widely available | ||
// existing API. | ||
// The performance measurement will be handled by Chrome Aurora | ||
|
||
performance.mark('mark_feature_usage', { | ||
detail: { | ||
feature: 'next-third-parties-ga', | ||
}, | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<Script | ||
id="_next-ga-init" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
window['${dataLayerName}'] = window['${dataLayerName}'] || []; | ||
function gtag(){window['${dataLayerName}'].push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${gaId}');`, | ||
}} | ||
/> | ||
<Script | ||
id="_next-ga" | ||
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export const sendGAEvent = (...args: Object[]) => { | ||
if (currDataLayerName === undefined) { | ||
console.warn(`@next/third-parties: GA has not been initialized`) | ||
return | ||
} | ||
|
||
if (window[currDataLayerName]) { | ||
window[currDataLayerName].push(...args) | ||
} else { | ||
console.warn( | ||
`@next/third-parties: GA 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
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,3 +1,4 @@ | ||
export { default as GoogleMapsEmbed } from './google-maps-embed' | ||
export { default as YouTubeEmbed } from './youtube-embed' | ||
export { GoogleTagManager, sendGTMEvent } from './gtm' | ||
export { GoogleAnalytics, sendGAEvent } from './ga' |
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,23 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { GoogleAnalytics, sendGAEvent } from '@next/third-parties/google' | ||
|
||
const Page = () => { | ||
const onClick = () => { | ||
sendGAEvent({ event: 'buttonClicked', value: 'xyz' }) | ||
} | ||
|
||
return ( | ||
<div class="container"> | ||
<GoogleAnalytics gaId="GA-XYZ" /> | ||
<h1>GA</h1> | ||
<button id="ga-send" onClick={onClick}> | ||
Click | ||
</button> | ||
<GoogleAnalytics gaId="GA-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 { GoogleAnalytics, sendGAEvent } from '@next/third-parties/google' | ||
|
||
const Page = () => { | ||
const onClick = () => { | ||
sendGAEvent({ event: 'buttonClicked', value: 'xyz' }) | ||
} | ||
|
||
return ( | ||
<div class="container"> | ||
<GoogleAnalytics gaId="GA-XYZ" /> | ||
<h1>GA</h1> | ||
<button id="ga-send" onClick={onClick}> | ||
Click | ||
</button> | ||
<GoogleAnalytics gaId="GA-XYZ" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |