index.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import Script from 'next/script'
  4. import { headers } from 'next/headers'
  5. import { IS_CE_EDITION } from '@/config'
  6. export enum GaType {
  7. admin = 'admin',
  8. webapp = 'webapp',
  9. }
  10. const gaIdMaps = {
  11. [GaType.admin]: 'G-DM9497FN4V',
  12. [GaType.webapp]: 'G-2MFWXK7WYT',
  13. }
  14. export type IGAProps = {
  15. gaType: GaType
  16. }
  17. const GA: FC<IGAProps> = ({
  18. gaType,
  19. }) => {
  20. if (IS_CE_EDITION)
  21. return null
  22. const nonce = process.env.NODE_ENV === 'production' ? headers().get('x-nonce') : ''
  23. return (
  24. <>
  25. <Script
  26. strategy="beforeInteractive"
  27. async
  28. src={`https://www.googletagmanager.com/gtag/js?id=${gaIdMaps[gaType]}`}
  29. nonce={nonce!}
  30. ></Script>
  31. <Script
  32. id="ga-init"
  33. dangerouslySetInnerHTML={{
  34. __html: `
  35. window.dataLayer = window.dataLayer || [];
  36. function gtag(){dataLayer.push(arguments);}
  37. gtag('js', new Date());
  38. gtag('config', '${gaIdMaps[gaType]}');
  39. `,
  40. }}
  41. nonce={nonce!}
  42. >
  43. </Script>
  44. {/* Cookie banner */}
  45. <Script
  46. id="cookieyes"
  47. src='https://cdn-cookieyes.com/client_data/2a645945fcae53f8e025a2b1/script.js'
  48. nonce={nonce!}
  49. ></Script>
  50. </>
  51. )
  52. }
  53. export default React.memo(GA)