|
| 1 | +import { analyticsPageView } from '@/utils/analytics'; |
| 2 | +import { env } from '@/utils/env'; |
| 3 | +import { usePathname } from 'next/navigation'; |
| 4 | +import Script from 'next/script'; |
| 5 | +import type { FC } from 'react'; |
| 6 | +import React, { useEffect } from 'react'; |
| 7 | + |
| 8 | +export const Analytics: FC = () => { |
| 9 | + const pathname = usePathname(); |
| 10 | + // 👇 send page views when users gets to the landing page |
| 11 | + useEffect(() => { |
| 12 | + if (!env.TRACKING_ID) return; |
| 13 | + |
| 14 | + gtag('config', env.TRACKING_ID, { |
| 15 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 16 | + send_page_view: false, //manually send page views to have full control |
| 17 | + }); |
| 18 | + }, []); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + if (!env.TRACKING_ID) return; |
| 22 | + analyticsPageView({ |
| 23 | + page_path: pathname ?? window.location.pathname, |
| 24 | + send_to: env.TRACKING_ID, |
| 25 | + }); |
| 26 | + }, [pathname]); |
| 27 | + |
| 28 | + if (!env.TRACKING_ID) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + return ( |
| 32 | + <> |
| 33 | + <Script |
| 34 | + rel="dns-prefetch" |
| 35 | + src={`https://www.googletagmanager.com/gtag/js?id=${env.TRACKING_ID}`} |
| 36 | + ></Script> |
| 37 | + <Script |
| 38 | + id="gtag-init" |
| 39 | + dangerouslySetInnerHTML={{ |
| 40 | + __html: ` |
| 41 | + window.dataLayer = window.dataLayer || []; |
| 42 | + function gtag(){dataLayer.push(arguments);} |
| 43 | + gtag('js', new Date()); |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | + gtag('consent', 'default', { |
| 48 | + 'analytics_storage': 'denied' |
| 49 | + }); |
| 50 | +
|
| 51 | + gtag('config', '${env.TRACKING_ID}', { |
| 52 | + 'debug_mode': false |
| 53 | + }); |
| 54 | + `, |
| 55 | + }} |
| 56 | + /> |
| 57 | + </> |
| 58 | + ); |
| 59 | +}; |
0 commit comments