Skip to content

Commit d96983b

Browse files
committed
refactor: add Google Tag Manager
1 parent 3ee6de4 commit d96983b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

public/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,22 @@
3030
work correctly both with client-side routing and a non-root public URL.
3131
Learn how to configure a non-root public URL by running `npm run build`.
3232
-->
33+
<!-- Google Tag Manager -->
34+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
35+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
36+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
37+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
38+
})(window,document,'script','dataLayer','GTM-KX4JH47');</script>
39+
<!-- End Google Tag Manager -->
3340
</head>
3441
<body>
3542
<noscript>
43+
<iframe
44+
src="https://www.googletagmanager.com/ns.html?id=GTM-KX4JH47"
45+
height="0"
46+
width="0"
47+
style="display: none; visibility: hidden"
48+
></iframe>
3649
You need to enable JavaScript to run this app.
3750
</noscript>
3851
<div id="root"></div>

src/layout/DefaultLayout.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
2+
import { useLocation } from 'react-router-dom'
3+
24
import { AppContent, AppSidebar, AppFooter, AppHeader } from '../components/index'
35

46
const DefaultLayout = () => {
7+
const location = useLocation()
8+
9+
useEffect(() => {
10+
// Google Tag Manager
11+
window.dataLayer = window.dataLayer || []
12+
window.dataLayer.push({
13+
event: 'pageview',
14+
page_location: window.location.href,
15+
})
16+
}, [location])
17+
518
return (
619
<div>
720
<AppSidebar />

0 commit comments

Comments
 (0)