Skip to content

Commit

Permalink
Update Google Analytics example for App Router (#66021)
Browse files Browse the repository at this point in the history
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
Hello 

This PR updates the `with-google-analytics` example to use:
- App Router
- TypeScript
- @next/third-parties

Co-authored-by: Sam Ko <sam@vercel.com>
  • Loading branch information
qqww08 and samcx committed May 21, 2024
1 parent 5cba51e commit 16c4e47
Show file tree
Hide file tree
Showing 13 changed files with 92 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 as string} />
<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.

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"]
}

0 comments on commit 16c4e47

Please sign in to comment.