61 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2023-12-05 15:05:05 +08:00
import type { FC } from 'react'
import React from 'react'
2023-05-15 08:51:32 +08:00
import Script from 'next/script'
import { type UnsafeUnwrappedHeaders, headers } from 'next/headers'
2023-12-05 15:05:05 +08:00
import { IS_CE_EDITION } from '@/config'
2023-05-15 08:51:32 +08:00
export enum GaType {
admin = 'admin',
webapp = 'webapp',
}
const gaIdMaps = {
[GaType.admin]: 'G-DM9497FN4V',
[GaType.webapp]: 'G-2MFWXK7WYT',
}
2023-12-05 15:05:05 +08:00
export type IGAProps = {
2023-05-15 08:51:32 +08:00
gaType: GaType
}
const GA: FC<IGAProps> = ({
2023-12-05 15:05:05 +08:00
gaType,
2023-05-15 08:51:32 +08:00
}) => {
2023-12-05 15:05:05 +08:00
if (IS_CE_EDITION)
return null
const nonce = process.env.NODE_ENV === 'production' ? (headers() as unknown as UnsafeUnwrappedHeaders).get('x-nonce') : ''
2023-05-15 08:51:32 +08:00
return (
2023-12-05 15:05:05 +08:00
<>
<Script
strategy="beforeInteractive"
async
src={`https://www.googletagmanager.com/gtag/js?id=${gaIdMaps[gaType]}`}
nonce={nonce!}
></Script>
2023-12-05 15:05:05 +08:00
<Script
id="ga-init"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaIdMaps[gaType]}');
2023-05-15 08:51:32 +08:00
`,
2023-12-05 15:05:05 +08:00
}}
nonce={nonce!}
2023-12-05 15:05:05 +08:00
>
</Script>
2024-11-25 16:31:49 +08:00
{/* Cookie banner */}
<Script
id="cookieyes"
src='https://cdn-cookieyes.com/client_data/2a645945fcae53f8e025a2b1/script.js'
nonce={nonce!}
></Script>
2023-12-05 15:05:05 +08:00
</>
2023-05-15 08:51:32 +08:00
)
}
export default React.memo(GA)