index.tsx 1.4 KB

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