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

Update Google Analytics example for App Router #66021

Merged
merged 2 commits into from
May 21, 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
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 as string} />
<body>{children}</body>
</html>
);
}
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.

24 changes: 24 additions & 0 deletions examples/with-google-analytics/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}