Skip to content

Commit

Permalink
fix(analytics): Use gtag
Browse files Browse the repository at this point in the history
  • Loading branch information
ReedD committed Aug 26, 2018
1 parent 372516d commit 724c87f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ const App: React.SFC<ISiteData> = ({ author, siteName, googleAnalytics }) => {
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700"
type="text/css"
/>
{addAnalytics && (
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${googleAnalytics}`}
/>
)}
{addAnalytics && (
<script>
{`
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '${googleAnalytics}', 'auto');
ga('send', 'pageview');
`.replace(/\s+/g, ' ')}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${googleAnalytics}');
`}
</script>
)}
</Head>
Expand All @@ -55,10 +59,12 @@ const App: React.SFC<ISiteData> = ({ author, siteName, googleAnalytics }) => {
<Route
path="/"
render={({ location }) => {
const ga = typeof window !== 'undefined' && (window as any).ga;
if (ga) {
ga('set', 'page', location.pathname + location.search);
ga('send', 'pageview');
const gtag =
typeof window !== 'undefined' && (window as any).gtag;
if (addAnalytics && gtag) {
gtag('config', googleAnalytics, {
page_path: location.pathname + location.search,
});
}
return null;
}}
Expand Down

0 comments on commit 724c87f

Please sign in to comment.