Skip to content

Commit aef0455

Browse files
author
Cezar Sampaio
committedMay 22, 2020
feat: add google analytics
1 parent a37cd46 commit aef0455

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 

‎gatsby-browser.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
import 'normalize.css';
22
import './src/global.css';
3+
4+
export const onRouteUpdate = () => {
5+
// For Google Analytics page views
6+
if (window.ga) {
7+
window.ga('send', 'pageview');
8+
}
9+
};

‎src/pages/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ export default function Home(props) {
6767
</SearchContainer>
6868

6969
<WhiteContainer>
70-
{categories.map((category) => (
70+
{categories.map((category, index) => (
7171
<CategoryCard
7272
title={category.name}
7373
url={`/${category.slug}/`}
7474
description={category.description.description}
75+
key={index}
7576
/>
7677
))}
7778
</WhiteContainer>

‎src/templates/layout.js

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import styled from '@emotion/styled';
3+
import { Helmet } from 'react-helmet';
34
import Header from '../components/header';
45
import Footer from '../components/footer';
56
import Wrap from '../components/wrap';
@@ -53,6 +54,25 @@ export default function Layout(props) {
5354
logoUrl={settings.logo.fixed.src}
5455
logoDescription={settings.logo.title}
5556
/>
57+
58+
<Helmet>
59+
{settings.googleAnalyticsId && (
60+
<script>
61+
{`
62+
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
63+
64+
ga('create', '${settings.googleAnalyticsId}', 'auto');
65+
ga('send', 'pageview');
66+
`}
67+
</script>
68+
)}
69+
{settings.googleAnalyticsId && (
70+
<script
71+
async
72+
src="https://www.google-analytics.com/analytics.js"
73+
></script>
74+
)}
75+
</Helmet>
5676
</>
5777
);
5878
}

0 commit comments

Comments
 (0)