import { useEffect , useContext } from 'react' ; import { GlobalContext } from '@/context/global' ; export const Analytics = ( props ) => { const { setting } = useContext ( GlobalContext ) ; useEffect ( ( ) => { const googleAnalyticsId = setting . googleAnalyticsId ; if ( ! googleAnalyticsId ) { return ; } window . dataLayer = window . dataLayer || [ ] ; function gtag ( ) { window . dataLayer . push ( arguments ) ; } gtag ( 'js' , new Date ( ) ) ; gtag ( 'config' , googleAnalyticsId ) ; const script = document . createElement ( 'script' ) ; script . src = `https://www.googletagmanager.com/gtag/js?id=${ googleAnalyticsId }` ; script . async = true ; if ( document . body ) { document . body . appendChild ( script ) ; } } , [ setting . googleAnalyticsId ] ) ; useEffect ( ( ) => { const baiduAnalyticsId = setting . baiduAnalyticsId ; if ( ! baiduAnalyticsId ) { return ; } const hm = document . createElement ( 'script' ) ; hm . src = `https://hm.baidu.com/hm.js?${ baiduAnalyticsId }` ; const s = document . getElementsByTagName ( 'script' ) [ 0 ] ; s . parentNode . insertBefore ( hm , s ) ; } , [ setting . baiduAnalyticsId ] ) ; return props . children || null ; } ;