1.0.0 • Published 6 months ago

next-umami v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

next-umami

Simple integration for https://nextjs.org and https://umami.is analytics.

Usage

Include the Analytics Script

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 Props

NameTypeDescription
websiteIdstringWebsite ID found in Umami dashboard. https://umami.is/docs/collect-data.
src?stringBy default it's set to https://cloud.umami.is/script.js. You can override this in case you're self-hosting.
hostUrl?stringBy 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?booleanBy 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) => voidExecute code after Umami has loaded.
onReady?() => void \| nullExecute code after Umami's load event when it first loads and then after every subsequent component re-mount.
onError?(e: any) => voidHandle errors if Umami fails to load.

Proxying Umami Requests

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:

NameTypeDescription
clientScriptPath?stringPath where Umami script will be served. Defaults to /script.js
serverScriptDestination?stringOriginal Umami script URL. Defaults to https://cloud.umami.is/script.js
clientApiPath?stringPath where tracking data will be sent. Defaults to /
serverApiDestination?stringOriginal Umami API endpoint. Defaults to https://api-gateway.umami.dev/api/send

Send Custom Events

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.

Pageview

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 Props

NameTypeDescription
hostname?stringHostname of server
language?stringBrowser language
referrer?stringPage referrer
screen?stringScreen dimensions (e.g. 1920x1080)
title?stringPage title
url?stringPage URL

Events

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 Props

NameTypeDescription
namestringName of the event
data?Record<string, string \| number>Custom data for the event
  • Numbers have a max precision of 4.
  • Strings have a max length of 500.
  • Arrays are converted to a String, with the same max length of 500.
  • Objects have a max of 50 properties. Arrays are considered 1 property.
1.0.0

6 months ago

0.0.5

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago