Skip to content

Commit 54ccad9

Browse files
committed
Analytics added
1 parent d2d54ce commit 54ccad9

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

library/helpers/gtag.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const GA_TRACKING_ID = process.env.G_ID; // GA_Tracking ID
2+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
3+
export const pageview = (url) => {
4+
window.gtag('config', GA_TRACKING_ID, {
5+
page_path: url,
6+
});
7+
};
8+
9+
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
10+
export const event = ({
11+
action, category, label, value,
12+
}) => {
13+
window.gtag('event', action, {
14+
event_category: category,
15+
event_label: label,
16+
value,
17+
});
18+
};

next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const nextConfig = {
1515
env: {
1616
API: process.env.API || 'https://api.hotel-prisma.ml/graphql',
1717
WS_API: process.env.WS_API || 'wss://api.hotel-prisma.ml/graphql',
18+
G_ID: process.env.G_ID || ' ',
1819
PUBLIC_STRIPE: process.env.PUBLIC_STRIPE,
1920
SECRET_STRIPE: process.env.SECRET_STRIPE,
2021
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY,

pages/_app.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import App from 'next/app';
22
import 'react-image-gallery/styles/css/image-gallery.css';
33
import 'react-toastify/dist/ReactToastify.min.css';
44
import 'react-dates/lib/css/_datepicker.css';
5+
import Router from 'next/router';
56
import { ThemeProvider } from 'styled-components';
67
// import { appWithTranslation } from '../i18n';
78
import AppLocale from 'translations/index';
89
import theme from 'themes/default.theme';
910
import GlobalStyles from 'assets/style/Global.style';
1011
import { withData } from 'library/helpers/restriction';
12+
import * as gtag from 'library/helpers/gtag';
1113
// Chỉ trong development
1214
import whyDidYouRender from '@welldone-software/why-did-you-render';
1315
import { LanguageProvider } from 'context/LanguageProvider';
@@ -53,6 +55,13 @@ class CustomApp extends App {
5355
this.setState({
5456
currentSelectedLanguage: language,
5557
});
58+
const handleRouteChange = (url) => {
59+
gtag.pageview(url)
60+
}
61+
Router.events.on('routeChangeComplete', handleRouteChange)
62+
return () => {
63+
Router.events.off('routeChangeComplete', handleRouteChange)
64+
}
5665
}
5766

5867

pages/_document.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Document, {
22
Html, Head, Main, NextScript,
33
} from 'next/document';
44
import { ServerStyleSheet } from 'styled-components';
5+
import { GA_TRACKING_ID } from 'library/helpers/gtag';
56

67
export default class MyDocument extends Document {
78
static async getInitialProps(ctx) {
@@ -36,7 +37,26 @@ export default class MyDocument extends Document {
3637
<meta name="theme-color" content="#008489" />
3738
<meta name="description" content="React next listing." />
3839
<meta name="keywords" content="React, React js, Next, Next js" />
39-
40+
{process.env.NODE_ENV === 'production' && (
41+
<>
42+
<script
43+
async
44+
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
45+
/>
46+
<script
47+
dangerouslySetInnerHTML={{
48+
__html: `
49+
window.dataLayer = window.dataLayer || [];
50+
function gtag(){dataLayer.push(arguments);}
51+
gtag('js', new Date());
52+
gtag('config', '${GA_TRACKING_ID}', {
53+
page_path: window.location.pathname,
54+
});
55+
`,
56+
}}
57+
/>
58+
</>
59+
)}
4060
<link
4161
rel="icon"
4262
// href="../static/images/favicon.png"
@@ -48,6 +68,7 @@ export default class MyDocument extends Document {
4868
rel="stylesheet"
4969
href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i,900,900i"
5070
/>
71+
5172
</Head>
5273
<body>
5374
<Main />

0 commit comments

Comments
 (0)