| 
									
										
										
										
											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' | 
					
						
							| 
									
										
										
										
											2024-10-11 16:14:56 +08:00
										 |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-11 16:14:56 +08:00
										 |  |  |   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
										 |  |  |     <> | 
					
						
							| 
									
										
										
										
											2024-10-11 16:14:56 +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
										 |  |  |         }} | 
					
						
							| 
									
										
										
										
											2024-10-11 16:14:56 +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) |