@statcounter/nextjs
v0.1.9
Published
Official Statcounter integration for Next.js
Maintainers
Readme
@statcounter/nextjs
The official Statcounter integration for Next.js.
Contents
Features
- Next.js App Router Ready : Optimized for Next.js 13, 14, and 15+ using the
app/directory standards. - Automatic Page Tracking : Automatically records page views on client-side route changes.
- Lightweight : Zero-dependency wrapper with minimal impact on bundle size.
- Invisible : Hardcoded to be hidden, ensuring your UI remains clean.
- Easy Setup : Simple install process.
Installation
Stop your server then install statcounter with this :
npm install @statcounter/nextjs
# or
yarn add @statcounter/nextjsUsage
1. Update your Layout
Open your root layout file (usually app/layout.tsx or app/layout.js).
Import the component at the top of the file:
import { StatCounter } from "@statcounter/nextjs";2. Add the Component to Layout
Place the <StatCounter /> component inside your layout just before the closing </body> tag.
<StatCounter project_id={1234567} security_code="abcdef12" />Delete the default values for the project_id and security_code and get your details from the Statcounter website by clicking your project name, the gear icon in the lower left corner, settings, then scroll down to bottom.
Example placement in your layout file.
// app/layout.tsx
import { StatCounter } from "@statcounter/nextjs";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
{/* Place Statcounter before closing body tag */}
<StatCounter project_id={1234567} security_code="abcdef12" />
</body>
</html>
);
}3. Rebuild the site and restart the app
4. Verify install
After you rebuild the site and restart the server, go to your site and open the browser console > Network tab and reload the page. You should see this activity in the Network tab.
counter.js
https://c.statcounter.com/t.php?project_id=111111[...]Load a few different pages to make sure the Statcounter fires on each page change.
If your project uses Session Replay you will also see
statcounter.io
recorder.jsNote: If you do not see any Statcounter related network activity, ensure your browser Adblocker is disabled as they often block analytics scripts.
Cookie Consent
You can toggle on Cookie Consent mode with this
sc_manageConsent={true}And enter the Consent Program name here
sc_CMP="cookieyes"Supported values are :
vanillaconsentcookieyescookiebotreact-cookie-consent
<Statcounter project_id={1234567} security_code="abcdef12" sc_manageConsent={true} sc_CMP="vanillaconsent" />For React Cookie Consent you also provide the cookie name
sc_cmpCookieName="myConsentCookie"<Statcounter project_id={1234567} security_code="abcdef12" sc_manageConsent={true} sc_CMP="react-cookie-consent" sc_cmpCookieName="myConsentCookie"/>Note : vanillaconsent is for Orest Bida CookieConsent
Vercel Hosted
If your site is deployed on Vercel and you don’t SSH into a server install Statcounter via your local dev environment and Git.
Install
@statcounter/nextjslocally using the steps 1 and 2 above.Commit and push the changes to your GitHub repository.
Vercel will automatically detect the new change and redeploy your site including the statcounter tracking.
Advanced
If you prefer the project id and security code can be put in an environment variable file instead of the layout file. Some might prefer this if their site code is public on a Github repo. That way if someone clones your code they don't also pull down your statcounter tracking code.
- Add your statcounter details to the .env.local file which lives in the app root folder, in the same location as the file package.json. If this file does not exist you can create it.
NEXT_PUBLIC_STATCOUNTER_PROJECT_ID=1234567
NEXT_PUBLIC_STATCOUNTER_SECURITY_CODE="abcabcabc"- In your layout file use code like this in the
<StatCounter />component, placing it just before the closing</body>tag.
<StatCounter
project_id={Number(process.env.NEXT_PUBLIC_STATCOUNTER_PROJECT_ID || 0)}
security_code={process.env.NEXT_PUBLIC_STATCOUNTER_SECURITY_CODE || ""}
/>- Build the site and restart the app for the changes to take effect
Support
If you have any questions please email us at or use our contact form here https://statcounter.com/support/contact/
Props
| Prop | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| project_id | number | Yes | Your Statcounter Project ID number. |
| security_code | string | Yes | Your Statcounter Security Code string. |
| sc_manageConsent | boolean | No | Enable / disable Cookie Consent mode. |
| sc_CMP | string | No | The name of your CMP. |
| sc_cmpCookieName | string | No | If your CMP requires a cookie name, use this. |
