Download next.js/packages/third-parties/src/google/ga.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/packages/third-parties/src/google/ga.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/packages/third-parties/src/google/ga.tsx
-
curl -L -o ga.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/packages/third-parties/src/google/ga.tsx
1.92 kB
| '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, debugMode, dataLayerName = 'dataLayer', nonce } = 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}' ${debugMode ? ",{ 'debug_mode': true }" : ''});`, | |
| }} | |
| nonce={nonce} | |
| /> | |
| ) | |
| } | |
| export function sendGAEvent(..._args: Object[]) { | |
| if (currDataLayerName === undefined) { | |
| console.warn(`@next/third-parties: GA has not been initialized`) | |
| return | |
| } | |
| if (window[currDataLayerName]) { | |
| window[currDataLayerName].push(arguments) | |
| } else { | |
| console.warn( | |
| `@next/third-parties: GA dataLayer ${currDataLayerName} does not exist` | |
| ) | |
| } | |
| } | |