mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
Co-authored-by: CodingOnStar <hanxujiang@dify.ai>
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
import type { UnsafeUnwrappedHeaders } from 'next/headers'
|
|
import type { FC } from 'react'
|
|
import { headers } from 'next/headers'
|
|
import Script from 'next/script'
|
|
import * as React from 'react'
|
|
import { IS_CE_EDITION } from '@/config'
|
|
|
|
export enum GaType {
|
|
admin = 'admin',
|
|
webapp = 'webapp',
|
|
}
|
|
|
|
const gaIdMaps = {
|
|
[GaType.admin]: 'G-DM9497FN4V',
|
|
[GaType.webapp]: 'G-2MFWXK7WYT',
|
|
}
|
|
|
|
export type IGAProps = {
|
|
gaType: GaType
|
|
}
|
|
|
|
const extractNonceFromCSP = (cspHeader: string | null): string | undefined => {
|
|
if (!cspHeader)
|
|
return undefined
|
|
const nonceMatch = cspHeader.match(/'nonce-([^']+)'/)
|
|
return nonceMatch ? nonceMatch[1] : undefined
|
|
}
|
|
|
|
const GA: FC<IGAProps> = ({
|
|
gaType,
|
|
}) => {
|
|
if (IS_CE_EDITION)
|
|
return null
|
|
|
|
const cspHeader = process.env.NODE_ENV === 'production'
|
|
? (headers() as unknown as UnsafeUnwrappedHeaders).get('content-security-policy')
|
|
: null
|
|
const nonce = extractNonceFromCSP(cspHeader)
|
|
|
|
return (
|
|
<>
|
|
{/* Initialize dataLayer first */}
|
|
<Script
|
|
id="ga-init"
|
|
strategy="afterInteractive"
|
|
dangerouslySetInnerHTML={{
|
|
__html: `
|
|
window.dataLayer = window.dataLayer || [];
|
|
window.gtag = function gtag(){window.dataLayer.push(arguments);};
|
|
window.gtag('js', new Date());
|
|
window.gtag('config', '${gaIdMaps[gaType]}');
|
|
`,
|
|
}}
|
|
nonce={nonce}
|
|
/>
|
|
{/* Load GA script */}
|
|
<Script
|
|
strategy="afterInteractive"
|
|
src={`https://www.googletagmanager.com/gtag/js?id=${gaIdMaps[gaType]}`}
|
|
nonce={nonce}
|
|
/>
|
|
{/* Cookie banner */}
|
|
<Script
|
|
id="cookieyes"
|
|
strategy="lazyOnload"
|
|
src="https://cdn-cookieyes.com/client_data/2a645945fcae53f8e025a2b1/script.js"
|
|
nonce={nonce}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
export default React.memo(GA)
|