React and Next.js
Add Okidoki to a React or Next.js app with the component or the script tag.
In a React or Next.js app you can use the Okidoki component, or put the script tag in the root of your app. Both load the same script, on every page. If an AI tool builds your app, it is easier to paste the install sentence and let it decide.
#The React component
Install the package:
npm install okidoki-analyticsPlace the component once, in your root layout or App component:
import { Okidoki } from 'okidoki-analytics';
export default function App() {
return (
<>
<Okidoki site="ok_your_site_key" />
{/* the rest of your app */}
</>
);
}The component renders nothing. It adds the script to the page head once, even if it is rendered more than once.
| Prop | Required | What it does |
|---|---|---|
site | Yes | Your site key, like ok_7f3kq2m9xa |
exclude | No | Paths to leave out, with everything below them, like ['/account', '/admin'] |
src | No | Where the script is loaded from. Defaults to https://okidoki.sh/js/okidoki.js |
#Next.js with the App Router
Put the script tag in app/layout.tsx, so it is on every page:
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script defer data-site="ok_your_site_key" src="https://okidoki.sh/js/okidoki.js" />
</head>
<body>{children}</body>
</html>
);
}The React component works in Next.js too, also in a server layout like app/layout.tsx: it is a client component and renders nothing.
#Vite, Create React App and plain React
Put the script tag in index.html, inside <head>. That file is loaded for every page of the app:
<script defer data-site="ok_your_site_key" src="https://okidoki.sh/js/okidoki.js"></script>#Page changes without reloading
React apps change pages without loading a new document. Okidoki notices every page change by itself, through the browser's history, so each page counts as its own page view. You do not need a router hook or anything else.
#Custom events from React
Count a button by adding one attribute, or call window.okidoki from your code:
<button data-okidoki-event="Signup">Sign up</button>window.okidoki?.('Signup');See Custom events.
#Testing on your own computer
To leave pages out, like a signed-in area, pass exclude={['/account']} to the component or data-exclude="/account" to the script tag. Every page below each path is left out too.
The script skips localhost and 127.0.0.1, so your development server never shows up in your numbers. To test the script locally anyway, add data-local to the tag. Visits only count when the address you test on belongs to your site, so this is mostly useful for debugging. See the Tracker reference.