Skip to content

Commit

Permalink
Update Google Analytics example for App Router
Browse files Browse the repository at this point in the history
  • Loading branch information
qqww08 committed May 21, 2024
1 parent af49a5b commit 9fd0a4a
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 114 deletions.
4 changes: 3 additions & 1 deletion examples/with-google-analytics/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Example app with analytics

This example shows how to use [Next.js](https://github.com/vercel/next.js) along with [Google Analytics](https://developers.google.com/analytics/devguides/collection/gtagjs/). A custom [\_app](https://nextjs.org/docs/advanced-features/custom-app) is used to inject [tracking snippet](https://developers.google.com/analytics/devguides/collection/gtagjs/) and track [pageviews](https://developers.google.com/analytics/devguides/collection/gtagjs/pages) and [event](https://developers.google.com/analytics/devguides/collection/gtagjs/events).
This example shows how to use [Next.js](https://github.com/vercel/next.js) along with [Google Analytics 4](https://developers.google.com/analytics/devguides/collection/ga4).

If you are using the Pages Router, please refer to the [pages/ documentation.](https://nextjs.org/docs/pages/building-your-application/optimizing/third-party-libraries)

## Deploy your own

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Page from "../components/Page";
import Page from "../../components/Page";

export default function About() {
return (
Expand Down
27 changes: 27 additions & 0 deletions examples/with-google-analytics/app/contact/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use client";
import { useRef, type FormEvent } from "react";
import Page from "../../components/Page";
import { sendGTMEvent } from "@next/third-parties/google";

export default function About() {
const inputRef = useRef<HTMLTextAreaElement | null>(null);

const handleSubmit = (e: FormEvent) => {
e.preventDefault();
sendGTMEvent({ event: "message submit", value: inputRef.current?.value });
inputRef.current!.value = "";
};

return (
<Page>
<h1>This is the Contact page</h1>
<form onSubmit={handleSubmit}>
<label>
<span>Message:</span>
<textarea ref={inputRef} />
</label>
<button type="submit">submit</button>
</form>
</Page>
);
}
20 changes: 20 additions & 0 deletions examples/with-google-analytics/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GoogleTagManager } from "@next/third-parties/google";

export const metadata = {
title: "Google Analytics Next.js",
description:
"This example shows how to use Next.js along with Google Analytics.",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<GoogleTagManager gtmId={process.env.NEXT_PUBLIC_GA_ID} />
<body>{children}</body>
</html>
);
}
File renamed without changes.
10 changes: 0 additions & 10 deletions examples/with-google-analytics/components/Page.js

This file was deleted.

11 changes: 11 additions & 0 deletions examples/with-google-analytics/components/Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Header from "./Header";
import type { PropsWithChildren } from "react";

export default function Page({ children }: PropsWithChildren<{}>) {
return (
<div>
<Header />
{children}
</div>
);
}
17 changes: 0 additions & 17 deletions examples/with-google-analytics/lib/gtag.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/with-google-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
"start": "next start"
},
"dependencies": {
"@next/third-parties": "^14.2.3",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "20.12.12",
"@types/react": "^18.3.2",
"typescript": "^5.4.5"
}
}
46 changes: 0 additions & 46 deletions examples/with-google-analytics/pages/_app.js

This file was deleted.

39 changes: 0 additions & 39 deletions examples/with-google-analytics/pages/contact.js

This file was deleted.

0 comments on commit 9fd0a4a

Please sign in to comment.