55 lines
1.1 KiB
TypeScript
Raw 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 { 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().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>
</>
2023-05-15 08:51:32 +08:00
)
}
export default React.memo(GA)