#google analytics
1 messages · Page 1 of 1 (latest)
To add Google Analytics to your Next.js project, you can use the @vercel/analytics package. Here's how to add the Analytics component to your app using the pages directory:
- Install the
@vercel/analyticspackage by running the following command in the root directory of your Next.js project:
pnpm i @vercel/analytics
- Add the
Analyticscomponent to your main app file (pages/_app.tsx):
import type { AppProps } from 'next/app';
import { Analytics } from '@vercel/analytics/react';
function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
<Analytics />
</>
);
}
export default MyApp;
Source: Vercel Documentation (https://vercel.com/docs/frameworks/nextjs)
Please note that this method uses Vercel's Analytics, which is a wrapper around the tracking script and offers seamless integration with Next.js. If you specifically want to use Google Analytics, you'll need to follow a different approach, which is not covered in the provided extracts.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@uneven spoke kapa.ai is still learning and improving, please let us how it did by reacting below
@exotic matrix what do i do for the app directory?
Let me check the knowledge base for you. One moment! :books: