Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "chore: replace gtag implementation w/ next.js" #51

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { GoogleAnalytics } from "@next/third-parties/google";

import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import GoogleAnalytics from "@/components/GoogleAnalytics";
import { Toaster } from "@/components/ui/toaster";

const inter = Inter({ subsets: ["latin"], fallback: ["sans-serif"] });
Expand Down Expand Up @@ -42,7 +41,7 @@ export default function RootLayout({
<body className={inter.className}>
<div className="flex flex-col place-content-center bg-background text-text">
<Header />
<GoogleAnalytics gaId="G-PMNW29ZB3Y" />
<GoogleAnalytics />
{children}
<Footer />
<Toaster />
Expand Down
31 changes: 31 additions & 0 deletions components/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import Script from "next/script";
import * as gtag from "@/lib/gtag";

const GoogleAnalytics = () => {
return (
<>
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.NEXT_PUBLIC_GA_TRACKING_ID}`}
/>
<Script
id="gtag-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtag.NEXT_PUBLIC_GA_TRACKING_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</>
);
};

export default GoogleAnalytics;
10 changes: 10 additions & 0 deletions lib/gtag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://stackoverflow.com/questions/77063977/implementing-google-analytics-in-nextjs-13-app-directory

export const NEXT_PUBLIC_GA_TRACKING_ID: string | undefined =
process.env.NEXT_PUBLIC_GA_TRACKING_ID;

export const pageview = (url: string) => {
(window as any).gtag("config", NEXT_PUBLIC_GA_TRACKING_ID, {
page_path: url,
});
};
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@next/third-parties": "^14.1.4",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
Expand Down
Loading