Skip to content

Commit

Permalink
feat: add GA tracking to app
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Won committed Sep 13, 2020
1 parent c6f410a commit e441f74
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: '12'
- name: Create DotENV config
uses: SpicyPizza/create-envfile@v1
with:
envkey_NEXT_PUBLIC_ANALYTICS_ID: ${{ secrets.INPUT_ENVKEY_GA_ID }}
file_name: .env
- name: Install Dependencies
run: |
yarn
Expand Down
18 changes: 18 additions & 0 deletions src/lib/gtag.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_ANALYTICS_ID;
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export { pageView, event };

const pageView = (url) => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url,
});
};

// https://developers.google.com/analytics/devguides/collection/gtagjs/events
const event = ({ action, category, label, value }) => {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value,
});
};
5 changes: 4 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ThemeProvider, CSSReset } from '@chakra-ui/core';
import '../styles/global.css';
import customTheme from '../styles/theme';

import Router from 'next/router';
import * as gtag from 'src/lib/gtag'; //https://hoangtrinhj.com/using-google-analytics-with-next-js
interface IProps {
[key: string]: any;
}
// Notice how we track pageview when route is changed
Router.events.on('routeChangeComplete', (url) => gtag.pageView(url));

export default function App({ Component, pageProps }: IProps) {
return (
Expand Down
20 changes: 20 additions & 0 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { GA_TRACKING_ID } from 'src/lib/gtag';

class MyDocument extends Document {
// static async getInitialProps(ctx) {
Expand All @@ -18,6 +19,25 @@ class MyDocument extends Document {
href="https://fonts.googleapis.com/css2?family=Alegreya+Sans&display=swap"
rel="stylesheet"
></link>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}', {
page_path: window.location.pathname,
});
</script>
`,
}}
/>
</Head>

<body>
Expand Down
3 changes: 3 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Window {
gtag: any;
}

0 comments on commit e441f74

Please sign in to comment.