forked from primefaces/primereact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics.js
44 lines (36 loc) · 1.1 KB
/
analytics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { useRouter } from 'next/router';
import Script from 'next/script';
import React, { useEffect } from 'react';
export const GTagManager = () => {
const router = useRouter();
useEffect(() => {
const handleRouteChange = (url) => {
pageview(url);
};
router.events.on('routeChangeComplete', handleRouteChange);
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);
useEffect(() => {
pageview(router.pathname);
}, [router.pathname]);
const pageview = (url) => {
window.gtag('config', 'G-FZJEC89ZVF', {
page_path: url
});
};
return (
<>
<Script src="https://www.googletagmanager.com/gtag/js?id=G-FZJEC89ZVF" />
<Script id="google-analytics">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-FZJEC89ZVF');
`}
</Script>
</>
);
};