
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Simple integration for https://nextjs.org and https://umami.is analytics.
To enable Umami analytics in your Next.js app you'll need to expose the Umami context.
If you're using the app router include UmamiProvider
inside the root layout:
// app/layout.js
import UmamiProvider from 'next-umami'
export default function RootLayout({ children }) {
return (
<html>
<head>
<UmamiProvider websiteId="a3d85e62-dc8b-4d4b-bd1f-e8a71b55d3cf" />
</head>
<body>{children}</body>
</html>
)
}
If you're using the pages router include the UmamiProvider
inside _app.js
:
// pages/_app.js
import UmamiProvider from 'next-umami'
export default function MyApp({ Component, pageProps }) {
return (
<UmamiProvider websiteId="a3d85e62-dc8b-4d4b-bd1f-e8a71b55d3cf">
<Component {...pageProps} />
</UmamiProvider>
)
}
If you want to enable Umami analytics only on a single page you can wrap the page in a UmamiProvider
component:
// pages/home.js
import UmamiProvider from 'next-umami'
export default function Home() {
return (
<UmamiProvider websiteId="a3d85e62-dc8b-4d4b-bd1f-e8a71b55d3cf">
<h1>My Site</h1>
{/* ... */}
</UmamiProvider>
)
}
UmamiProvider
PropsName | Type | Description |
---|---|---|
websiteId | string | Website ID found in Umami dashboard. https://umami.is/docs/collect-data. |
src? | string | By default it's set to https://cloud.umami.is/script.js. You can override this in case you're self-hosting. |
hostUrl? | string | By default, Umami will send data to wherever the script is located. You can override this to send data to another location. See in docs. |
autoTrack? | boolean | By default, Umami tracks all pageviews and events for you automatically. You can disable this behavior and track events yourself using the tracker functions. See in docs. |
domains? | string[] | If you want the tracker to only run on specific domains, you can add them to your tracker script. This is a comma delimited list of domain names. Helps if you are working in a staging/development environment. See in docs. |
onLoad? | (e: any) => void | Execute code after Umami has loaded. |
onReady? | () => void | null | Execute code after Umami's load event when it first loads and then after every subsequent component re-mount. |
onError? | (e: any) => void | Handle errors if Umami fails to load. |
To improve performance and avoid ad blockers that might interfere with your analytics, you can proxy Umami's tracking script and API through your own domain. Update your next.config.mjs
:
/** @type {import('next').NextConfig} */
import { withUmamiProxy } from 'next-umami';
const nextConfig = withUmamiProxy({
clientApiPath: '/events',
clientScriptPath: '/js/script.js',
})({
// Your existing Next.js configuration
reactStrictMode: true,
});
export default nextConfig;
The withUmamiProxy
function accepts these options:
Name | Type | Description |
---|---|---|
clientScriptPath? | string | Path where Umami script will be served. Defaults to /script.js |
serverScriptDestination? | string | Original Umami script URL. Defaults to https://cloud.umami.is/script.js |
clientApiPath? | string | Path where tracking data will be sent. Defaults to / |
serverApiDestination? | string | Original Umami API endpoint. Defaults to https://api-gateway.umami.dev/api/send |
The useUmami
hook exposes two functions that you can call on your website if you want more control over your tracking.
By default, everything is automatically collected, but you can disable this using autoTrack={false}
in UmamiProvider
and send the data yourself.
Default properties are automatically sent. If you wish to override any property, use the umami.pageView
function like this:
import { useUmami } from 'next-umami'
export default function Page() {
const umami = useUmami()
// Default Pageview
umami.pageView()
// OR
// Custom Pageview
umami.pageView({
url: '/custom-pageview',
})
return <h1>My Page</h1>
}
Pageview
PropsName | Type | Description |
---|---|---|
hostname? | string | Hostname of server |
language? | string | Browser language |
referrer? | string | Page referrer |
screen? | string | Screen dimensions (e.g. 1920x1080) |
title? | string | Page title |
url? | string | Page URL |
Access the umami.event
function like this:
import { useUmami } from 'next-umami'
export default function UmamiButtons() {
const umami = useUmami()
return (
<>
{/* Basic Event */}
<button onClick={() => umami.event('Basic Event')}>Submit</button>
{/* Custom Event */}
<button
onClick={() =>
umami.event('Custom Event', {
userAgent: window.navigator.userAgent,
})
}
>
Submit with custom data
</button>
</>
)
}
Event
PropsName | Type | Description |
---|---|---|
name | string | Name of the event |
data? | Record<string, string | number> | Custom data for the event |
FAQs
Next.js Umami Analytics Integration
The npm package next-umami receives a total of 83 weekly downloads. As such, next-umami popularity was classified as not popular.
We found that next-umami demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.