Skip to content

Commit

Permalink
Add integration with Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
madebyais committed Jul 24, 2021
1 parent 7f6a225 commit 3782e5d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ For base css style, you can go to [TailwindCSS](https://tailwindcss.com/) docume
#### Header
- 2 plug-n-play header components

#### Others
- Google Analytics

### Getting Started

Before getting started, you can clone this project or run following command:
Expand Down
5 changes: 4 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
FACEBOOK_CLIENT_SECRET: process.env.FACEBOOK_CLIENT_SECRET,
FACEBOOK_REDIRECT_URI: process.env.FACEBOOK_REDIRECT_URI,
FACEBOOK_OAUTH_SCOPE: process.env.FACEBOOK_OAUTH_SCOPE,
FACEBOOK_OAUTH_FIELDS: process.env.FACEBOOK_OAUTH_FIELDS
FACEBOOK_OAUTH_FIELDS: process.env.FACEBOOK_OAUTH_FIELDS,

GOOGLE_ANALYTICS_ENABLE: process.env.GOOGLE_ANALYTICS_ENABLE || false,
GOOGLE_ANALYTICS_TRACKING_ID: process.env.GOOGLE_ANALYTICS_TRACKING_ID
}
}
14 changes: 14 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import '../styles/style.css'
import type { AppProps } from 'next/app'
import { NextRouter, useRouter } from 'next/dist/client/router'
import { useEffect } from 'react'
import * as GoogleAnalytics from 'util/google-analytics'

function App({ Component, pageProps }: AppProps) {
const router: NextRouter = useRouter()

useEffect(() => {
const routerEventsChange = () => router.events.on('routeChangeComplete', GoogleAnalytics.pageview)

routerEventsChange()
return () => {
routerEventsChange()
}
}, [router.events])

return <Component {...pageProps} />
}
export default App
17 changes: 17 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ class _Document extends Document {
<Head>
<link rel='icon' href='/favicon.ico' />
<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css' integrity='sha512-SfTiTlX6kk+qitfevl/7LibUOeJWlt9rbyDn92a1DqWOw9vWG2MFoays0sgObmWazO5BQPiFucnnEAjpAB+/Sw==' crossOrigin='anonymous' />

<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GOOGLE_ANALYTICS_TRACKING_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GOOGLE_ANALYTICS_TRACKING_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
<body>
<Main />
Expand Down
13 changes: 13 additions & 0 deletions util/google-analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const pageview = (url: string) => {
if (process.env.GOOGLE_ANALYTICS_ENABLE) {
// @ts-ignore
window.gtag('config', process.env.GOOGLE_ANALYTICS_TRACKING_ID, {
page_path: url
})
}
}

export const event = ({ action, params }: any) => {
// @ts-ignore
window.gtag('event', action, params)
}

0 comments on commit 3782e5d

Please sign in to comment.