|
| 1 | +/* eslint-disable react/no-danger */ |
| 2 | + |
1 | 3 | import React from 'react';
|
| 4 | +import Router from 'next/router'; |
2 | 5 | import JssProvider from 'react-jss/lib/JssProvider';
|
3 | 6 | import Document, { Head, Main, NextScript } from 'next/document';
|
4 | 7 |
|
5 | 8 | import getContext from '../lib/context';
|
6 | 9 |
|
| 10 | +require('dotenv').config(); |
| 11 | + |
| 12 | +const gaTrackingId = 'process.env.GA_TRACKING_ID'; |
| 13 | + |
| 14 | +Router.onRouteChangeComplete = () => { |
| 15 | + if (window.gtag) { |
| 16 | + window.gtag('config', window.gaTrackingId, { |
| 17 | + page_location: window.location.href, |
| 18 | + page_path: window.location.pathname, |
| 19 | + page_title: window.document.title, |
| 20 | + }); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
7 | 24 | class MyDocument extends Document {
|
8 | 25 | render() {
|
9 | 26 | return (
|
@@ -61,6 +78,20 @@ class MyDocument extends Document {
|
61 | 78 | }
|
62 | 79 | `}
|
63 | 80 | </style>
|
| 81 | + <script async src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`} /> |
| 82 | + <script |
| 83 | + dangerouslySetInnerHTML={{ |
| 84 | + __html: ` |
| 85 | + window.gaTrackingId = '${gaTrackingId}'; |
| 86 | + window.dataLayer = window.dataLayer || []; |
| 87 | + function gtag(){ |
| 88 | + dataLayer.push(arguments); |
| 89 | + } |
| 90 | + gtag('js', new Date()); |
| 91 | + gtag('config', '${gaTrackingId}'); |
| 92 | + `, |
| 93 | + }} |
| 94 | + /> |
64 | 95 | </Head>
|
65 | 96 | <body
|
66 | 97 | style={{
|
|
0 commit comments