Google Analytics tracking on SSR pages
This commit is contained in:
parent
5588826691
commit
7bad7a2567
|
@ -11,3 +11,4 @@ SLACK_KEY=71315967491.XXXXXXXXXX
|
||||||
SLACK_SECRET=d2dc414f9953226bad0a356c794XXXXX
|
SLACK_SECRET=d2dc414f9953226bad0a356c794XXXXX
|
||||||
URL=http://localhost:3000
|
URL=http://localhost:3000
|
||||||
DEPLOYMENT=hosted
|
DEPLOYMENT=hosted
|
||||||
|
GOOGLE_ANALYTICS_ID=
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
// @flow
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
function getScript() {
|
||||||
|
if (!process.env.GOOGLE_ANALYTICS_ID) return undefined;
|
||||||
|
|
||||||
|
return {
|
||||||
|
__html: `
|
||||||
|
window.dataLayer = window.dataLayer || [];
|
||||||
|
function gtag(){dataLayer.push(arguments);}
|
||||||
|
gtag('js', new Date());
|
||||||
|
gtag('config', '${process.env.GOOGLE_ANALYTICS_ID}');
|
||||||
|
`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function Analytics() {
|
||||||
|
if (!process.env.GOOGLE_ANALYTICS_ID) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span>
|
||||||
|
<script
|
||||||
|
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GOOGLE_ANALYTICS_ID}`}
|
||||||
|
/>
|
||||||
|
<script dangerouslySetInnerHTML={getScript()} />
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Analytics;
|
|
@ -2,6 +2,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Helmet } from 'react-helmet';
|
import { Helmet } from 'react-helmet';
|
||||||
import Navigation from './Navigation';
|
import Navigation from './Navigation';
|
||||||
|
import Analytics from './Analytics';
|
||||||
import globalStyles from '../../../shared/styles/globals';
|
import globalStyles from '../../../shared/styles/globals';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
@ -18,6 +19,7 @@ export default function Layout({ children }: Props) {
|
||||||
<title>Atlas</title>
|
<title>Atlas</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
</Helmet>
|
</Helmet>
|
||||||
|
<Analytics />
|
||||||
|
|
||||||
{'{{HEAD}}'}
|
{'{{HEAD}}'}
|
||||||
{'{{CSS}}'}
|
{'{{CSS}}'}
|
||||||
|
|
Reference in New Issue