Skip to content

Commit

Permalink
Add Google Analytics (deepset-ai#139)
Browse files Browse the repository at this point in the history
* new sitemap

* added google ananlytics
  • Loading branch information
PiffPaffM committed Sep 1, 2021
1 parent dfdf018 commit 3b342c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import remarkPrism from "remark-prism";
import autolink from "remark-autolink-headings";
import GitHubSlugger from "github-slugger";
import imgLinks from "@pondorasti/remark-img-links";
import semverCompare from "semver-compare";
import { getHaystackReleaseTagNames, getStargazersCount } from "./github";
import { MDXRemoteSerializeResult } from "next-mdx-remote";

Expand Down
24 changes: 24 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ import "../styles/global.css";
import "../styles/prism-theme.css";
import type { AppProps } from "next/app";

import { useEffect } from 'react'
import { useRouter } from 'next/router'

declare const window: any;

function MyApp({ Component, pageProps }: AppProps) {
const router = useRouter()

useEffect(() => {
const handleRouteChange = (url: any) => {
window.gtag('config', process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS, {
page_path: url,
});
}
//When the component is mounted, subscribe to router changes
//and log those page views
router.events.on('routeChangeComplete', handleRouteChange)

// If the component is unmounted, unsubscribe
// from the event with the `off` method
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [router.events])

return <Component {...pageProps} />;
}
export default MyApp;
19 changes: 18 additions & 1 deletion pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@ class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<Head>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
<body>
<Main />
<NextScript />
Expand Down

0 comments on commit 3b342c4

Please sign in to comment.