From a19e1c0a96d8ae7407958f2e279d8940872055ed Mon Sep 17 00:00:00 2001 From: DaeHyun Date: Tue, 21 May 2024 15:41:12 +0900 Subject: [PATCH] Update Google Analytics example for App Router --- examples/with-google-analytics/README.md | 4 +- .../{pages/about.js => app/about/page.tsx} | 2 +- .../app/contact/page.tsx | 27 +++++++++++ examples/with-google-analytics/app/layout.tsx | 20 ++++++++ .../{pages/index.js => app/page.tsx} | 0 .../components/{Header.js => Header.tsx} | 0 .../with-google-analytics/components/Page.js | 10 ---- .../with-google-analytics/components/Page.tsx | 11 +++++ examples/with-google-analytics/lib/gtag.js | 17 ------- examples/with-google-analytics/package.json | 6 +++ examples/with-google-analytics/pages/_app.js | 46 ------------------- .../with-google-analytics/pages/contact.js | 39 ---------------- examples/with-google-analytics/tsconfig.json | 24 ++++++++++ 13 files changed, 92 insertions(+), 114 deletions(-) rename examples/with-google-analytics/{pages/about.js => app/about/page.tsx} (73%) create mode 100644 examples/with-google-analytics/app/contact/page.tsx create mode 100644 examples/with-google-analytics/app/layout.tsx rename examples/with-google-analytics/{pages/index.js => app/page.tsx} (100%) rename examples/with-google-analytics/components/{Header.js => Header.tsx} (100%) delete mode 100644 examples/with-google-analytics/components/Page.js create mode 100644 examples/with-google-analytics/components/Page.tsx delete mode 100644 examples/with-google-analytics/lib/gtag.js delete mode 100644 examples/with-google-analytics/pages/_app.js delete mode 100644 examples/with-google-analytics/pages/contact.js create mode 100644 examples/with-google-analytics/tsconfig.json diff --git a/examples/with-google-analytics/README.md b/examples/with-google-analytics/README.md index beadddd13fa92..f5e507a140384 100644 --- a/examples/with-google-analytics/README.md +++ b/examples/with-google-analytics/README.md @@ -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 diff --git a/examples/with-google-analytics/pages/about.js b/examples/with-google-analytics/app/about/page.tsx similarity index 73% rename from examples/with-google-analytics/pages/about.js rename to examples/with-google-analytics/app/about/page.tsx index 425faf7c40891..4faa020811f94 100644 --- a/examples/with-google-analytics/pages/about.js +++ b/examples/with-google-analytics/app/about/page.tsx @@ -1,4 +1,4 @@ -import Page from "../components/Page"; +import Page from "../../components/Page"; export default function About() { return ( diff --git a/examples/with-google-analytics/app/contact/page.tsx b/examples/with-google-analytics/app/contact/page.tsx new file mode 100644 index 0000000000000..f3dcc3ad8e299 --- /dev/null +++ b/examples/with-google-analytics/app/contact/page.tsx @@ -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(null); + + const handleSubmit = (e: FormEvent) => { + e.preventDefault(); + sendGTMEvent({ event: "message submit", value: inputRef.current?.value }); + inputRef.current!.value = ""; + }; + + return ( + +

This is the Contact page

+
+