Skip to content

Commit

Permalink
chore: remove google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
strdr4605 committed Jun 6, 2023
1 parent f567919 commit 049a0ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 96 deletions.
33 changes: 0 additions & 33 deletions lib/gtag.ts

This file was deleted.

35 changes: 13 additions & 22 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
import type { AppProps } from "next/app";
import Head from "next/head";
import Router from "next/router";
import "prismjs/themes/prism.css";
import * as gtag from "../lib/gtag";
import "../styles/globals.css";
import Script from "next/script";

const isProduction = process.env.NODE_ENV === "production";
MyApp.getStaticProps = async () => {
const isProduction = process.env.NODE_ENV === "production";
return { props: { isProduction } };
};

// Remove workers created by gatsby-plugin-offline
// https://github.com/gatsbyjs/gatsby/issues/2880#issuecomment-349789919
if (typeof window !== "undefined" && "serviceWorker" in navigator) {
window.navigator.serviceWorker.getRegistrations().then((registrations) => {
registrations.forEach((r) => r.unregister());
});
}

if (isProduction) {
// Notice how we track pageview when route is changed
Router.events.on("routeChangeComplete", (url) => gtag.pageview(url));
}

function MyApp({ Component, pageProps }: AppProps) {
function MyApp(props: AppProps & { isProduction: boolean }) {
const { Component, pageProps, isProduction } = props;
return (
<>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>
<Component {...pageProps} />
<Script
async
src="https://analytics.umami.is/script.js"
data-website-id="ffd94eb4-d3a5-4505-bcd4-35c97edc57c8"
/>
{isProduction && (
<Script
async
src="https://analytics.umami.is/script.js"
data-website-id="ffd94eb4-d3a5-4505-bcd4-35c97edc57c8"
/>
)}
</>
);
}
Expand Down
42 changes: 1 addition & 41 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import Document, { Head, Html as HTML, Main, NextScript } from "next/document";
import { Fragment } from "react";
import { GA_TRACKING_ID } from "../lib/gtag";

export default class CustomDocument extends Document<{
isProduction: boolean;
}> {
static async getInitialProps(ctx: any) {
const initialProps = await Document.getInitialProps(ctx);

const isProduction = process.env.NODE_ENV === "production";

return {
...initialProps,
isProduction,
};
}

export default class CustomDocument extends Document {
render() {
const { isProduction } = this.props;

return (
<HTML lang="en">
<Head>
Expand All @@ -29,29 +12,6 @@ export default class CustomDocument extends Document<{
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:creator" content={process.env.twitterCreator} />
<meta name="theme-color" content="#FAF181" />
{/* We only want to add the scripts if in production */}
{isProduction && (
<Fragment>
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Fragment>
)}
</Head>
<body>
<Main />
Expand Down

0 comments on commit 049a0ca

Please sign in to comment.