Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

next-plausible

4lejandrito179.5kMIT3.12.4TypeScript support: included

Simple integration for https://nextjs.org and https://plausible.io analytics.

next, plausible, analytics

readme

Next-Plausible · npm version

Simple integration for https://nextjs.org and https://plausible.io analytics.

See it in action at https://next-plausible.vercel.app, and this commit for a real world example.

Important: If you're using a version of next lower than 11.1.0 please use next-plausible@2 to avoid type checking errors (see https://github.com/4lejandrito/next-plausible/issues/25).

Usage

Include the Analytics Script

To enable Plausible analytics in your Next.js app you'll need to expose the Plausible context, <PlausibleProvider />, at the top level of your application inside _app.js:

// pages/_app.js
import PlausibleProvider from 'next-plausible'

export default function MyApp({ Component, pageProps }) {
  return (
    <PlausibleProvider domain="example.com">
      <Component {...pageProps} />
    </PlausibleProvider>
  )
}

If you want to enable Plausible analytics only on a single page you can wrap the page in a PlausibleProvider component:

// pages/home.js
import PlausibleProvider from 'next-plausible'

export default Home() {
  return (
    <PlausibleProvider domain="example.com">
      <h1>My Site</h1>
      {/* ... */}
    </PlausibleProvider>
  )
}

If are using the app directory include PlausibleProvider inside the root layout:

// app/layout.js
import PlausibleProvider from 'next-plausible'

export default function RootLayout({ children }) {
  return (
    <html>
      <head>
        <PlausibleProvider domain="example.com" />
      </head>
      <body>{children}</body>
    </html>
  )
}

PlausibleProvider Props

Name Description
domain The domain of the site you want to monitor.
customDomain Set this if you use a custom domain to serve the analytics script. Defaults to https://plausible.io. See https://plausible.io/docs/custom-domain for more details.
trackOutboundLinks Set this to true if you want to enable outbound link click tracking.
trackFileDownloads Set this to true if you want to enable file download tracking.
taggedEvents Set this to true if you want to enable custom event tracking in HTML elements.
trackLocalhost Set this to true if you want to enable localhost tracking.
manualPageviews Set this to true if you want to disable automatic pageview events.
pageviewProps Set the custom properties for pageviews. The event- prefix will be added automatically. See an example.
revenue Set this to true if you want to enable ecommerce revenue tracking.
hash Set this to true if you want to use hash-based routing.
exclude Set this if you want to exclude a set of pages from being tracked. See https://plausible.io/docs/excluding-pages for more details.
selfHosted Set this to true if you are self hosting your Plausible instance. Otherwise you will get a 404 when requesting the script.
enabled Use this to explicitly decide whether or not to render script. If not passed the script will be rendered in production environments (checking NODE_ENV and VERCEL_ENV).
integrity Optionally define the subresource integrity attribute for extra security.
scriptProps Optionally override any of the props passed to the script element. See example.

Proxy the Analytics Script

To avoid being blocked by adblockers plausible recommends proxying the script. To do this you need to wrap your next.config.js with the withPlausibleProxy function:

const { withPlausibleProxy } = require('next-plausible')

module.exports = withPlausibleProxy()({
  // ...your next js config, if any
  // Important! it is mandatory to pass a config object, even if empty
})

This will set up the necessary rewrites as described here and configure PlausibleProvider to use the local URLs so you can keep using it like this:

  <PlausibleProvider domain="example.com">
    ...
  </PlausibleProvider>
}

Optionally you can overwrite the proxied script subdirectory and name, as well as the custom domain for the original script:

const { withPlausibleProxy } = require('next-plausible')

module.exports = withPlausibleProxy({
  subdirectory: 'yoursubdirectory',
  scriptName: 'scriptName',
  customDomain: 'http://example.com',
})({
  // ...your next js config, if any
  // Important! it is mandatory to pass a config object, even if empty
})

This will load the script from /yoursubdirectory/js/scriptName.js and fetch it from http://example.com/js/script.js.

Notes:

  • Proxying will only work if you serve your site using next start. Statically generated sites won't be able to rewrite the requests.
  • If you are self hosting plausible, you need to set customDomain to your instance otherwise no data will be sent.
  • Bear in mind that tracking requests will be made to the same domain, so cookies will be forwarded. See https://github.com/4lejandrito/next-plausible/issues/67. If this is an issue for you, from `next@13.0.0` you can use middleware to strip the cookies like this:

    import { NextResponse } from 'next/server'
    
    export function middleware(request) {
      const requestHeaders = new Headers(request.headers)
      requestHeaders.set('cookie', '')
      return NextResponse.next({
        request: {
          headers: requestHeaders,
        },
      })
    }
    
    export const config = {
      matcher: '/proxy/api/event',
    }

Send Custom Events

Plausible supports custom events as described at https://plausible.io/docs/custom-event-goals. This package provides the usePlausible hook to safely access the plausible function like this:

import { usePlausible } from 'next-plausible'

export default function PlausibleButton() {
  const plausible = usePlausible()

  return (
    <>
      <button onClick={() => plausible('customEventName')}>Send</button>

      <button
        id="foo"
        onClick={() =>
          plausible('customEventName', {
            props: {
              buttonId: 'foo',
            },
          })
        }
      >
        Send with props
      </button>
    </>
  )
}

If you use Typescript you can type check your custom events like this:

import { usePlausible } from 'next-plausible'

type MyEvents = {
  event1: { prop1: string }
  event2: { prop2: string }
  event3: never
}

const plausible = usePlausible<MyEvents>()

Only those events with the right props will be allowed to be sent using the plausible function.

Developing

  • npm run build will generate the production scripts under the dist folder.

changelog

Changelog

v3.12.3 (2024-11-01)

Full Changelog

v3.12.2 (2024-08-19)

Full Changelog

Closed issues:

  • v3.12.1 throws type error and breaks existing usePlausible hook #128

v3.12.1 (2024-08-05)

Full Changelog

Closed issues:

  • Heads up: Plausible site verification tool #123
  • Unique count with Next.js 14 and proxy #121
  • Next 14.1.4 analytics not working anymore #120
  • Refused to execute script from 'https://plausible.io/js/script.js' because its MIME type ('image/png') is not executable. #115
  • POST request is not fired when clicking on links #114
  • Changes as of Next.js 14.0.3 break the Plausible script #107

Merged pull requests:

v3.12.0 (2023-11-27)

Full Changelog

Closed issues:

  • Plausible script not included on website #80
  • Not catching any requests #76
  • withPlausibleProxy wrapper not forwarding country #75

Merged pull requests:

v3.11.3 (2023-10-27)

Full Changelog

Merged pull requests:

v3.11.2 (2023-10-17)

Full Changelog

Closed issues:

  • upgrading nextjs to 13.5.5 results in warnings on next build #104

v3.11.1 (2023-08-21)

Full Changelog

v3.11.0 (2023-08-21)

Full Changelog

Closed issues:

  • Add ability to track hash changes #99

v3.10.2 (2023-08-16)

Full Changelog

Closed issues:

  • Typescript error when having an Event without props and custom typed events #101

Merged pull requests:

  • fix: usage of events with type never #102 (cstrnt)

v3.10.1 (2023-07-18)

Full Changelog

Closed issues:

  • Script not added to Next.js App - Issue with custom script? #96

v3.10.0 (2023-07-15)

Full Changelog

v3.9.1 (2023-07-09)

Full Changelog

v3.9.0 (2023-07-09)

Full Changelog

Closed issues:

  • Support for new Revenue Feature #97

v3.8.0 (2023-06-22)

Full Changelog

Closed issues:

  • NextJs 13 warning with 'withPlausibleProxy' #93
  • Remove support for custom domains #92
  • How can I use this with next.config.mjs as generated by t3 #89
  • Removing PlausibleProvider on page navigation doesn't remove the script tag #88
  • Consider referencing head.js pattern for Next.js appDir installations #84
  • Cannot read properties of null (reading 'useCallback') #81

Merged pull requests:

  • Add custom properties for pageviews support #95 (rrozanka)

v3.7.2 (2023-02-08)

Full Changelog

Merged pull requests:

v3.7.1 (2023-01-22)

Full Changelog

Closed issues:

  • Proxy script caching is rather short #83
  • Breaking change in 3.7.0? #82
  • Self-hosting script with plausible-tracker? #78

v3.7.0 (2023-01-05)

Full Changelog

Closed issues:

  • support custom event css class name #77
  • withPlausibleProxy with next.js 13 app directory #74

v3.6.5 (2022-12-06)

Full Changelog

Closed issues:

  • Add usePlausibleEvent #73
  • Request Header Fields Too Large when using combined with auth0/next-auth #67
  • The root value has an unexpected property #65

v3.6.4 (2022-10-28)

Full Changelog

Merged pull requests:

v3.6.3 (2022-09-19)

Full Changelog

Merged pull requests:

v3.6.2 (2022-09-12)

Full Changelog

Closed issues:

  • Proxy doesn't work with Next basePath configuration option #63
  • Proxy is not compatible with Next basePath configuration option #62

v3.6.1 (2022-09-12)

Full Changelog

v3.6.0 (2022-09-12)

Full Changelog

Closed issues:

  • Can't load Plausible script when using Next basePath config #61
  • Vercel script/js 404 #60

v3.5.0 (2022-09-01)

Full Changelog

Closed issues:

  • Support file downloads tracking #56

Merged pull requests:

  • Add option to add "u" parameter to plausible function call #59 (lucasra1)
  • docs: add note for self hosted instances #58 (not-matthias)

v3.4.0 (2022-08-30)

Full Changelog

Closed issues:

  • Proxy rewrite for API POST request doesn't work on Netlify #54

Merged pull requests:

v3.3.1 (2022-08-26)

Full Changelog

Closed issues:

  • Documentation is incorrect for withPlausibleProxy subdirectory option #52
  • utm support missing #50
  • Redirects in _middleware.ts not tracked #48
  • can't see the script event in localhost with a customDomain #47

Merged pull requests:

v3.2.0 (2022-06-07)

Full Changelog

Closed issues:

  • Disable console logs #45
  • Proxying causes the "event" request to redirect when trailingSlash: true #43
  • Script usage warning when migrating to next@12.1 #36
  • Support proxy to plausible.io/api/event with http proxy? #30

v3.1.9 (2022-04-05)

Full Changelog

Merged pull requests:

v3.1.8 (2022-03-31)

Full Changelog

Closed issues:

  • Support for next@canary #42

v3.1.7 (2022-03-29)

Full Changelog

Closed issues:

  • Inject script using next/script #40

Merged pull requests:

v3.1.6 (2022-03-14)

Full Changelog

Closed issues:

  • Wrap usePlausible in useCallback #38

Merged pull requests:

v3.1.5 (2022-02-21)

Full Changelog

Closed issues:

  • Is tracking localhost real-time? #33
  • withPlausibleProxy causes next/image requests to CDNs like Sanity.io 400 #32
  • No requests made to self hosted instance #26

v3.1.4 (2021-11-08)

Full Changelog

Closed issues:

  • Proxy /api/event routes using withPlausibleProxy for custom domains #21

v3.1.3 (2021-11-06)

Full Changelog

v3.1.2 (2021-11-05)

Full Changelog

v3.1.1 (2021-11-04)

Full Changelog

v2.2.0 (2021-11-03)

Full Changelog

Closed issues:

  • Ability to track localhost traffic #27

v3.1.0 (2021-11-03)

Full Changelog

v3.0.1 (2021-10-27)

Full Changelog

Closed issues:

  • Types are incompatible with Next@11 #25

v3.0.0 (2021-10-21)

Full Changelog

v2.1.4 (2021-10-21)

Full Changelog

v2.1.3 (2021-09-22)

Full Changelog

Closed issues:

  • Events Tracked Immediately with useEffect Fail to Send #24

v2.1.2 (2021-09-17)

Full Changelog

Closed issues:

  • scriptname is index.js instead of plausible.js for selfHosted #20

v2.1.1 (2021-08-12)

Full Changelog

Closed issues:

  • Support custom domains for proxy #18
  • Is there a changelog? #17

v2.1.0 (2021-08-10)

Full Changelog

Merged pull requests:

  • feat: add support for custom domains in the proxy script #19 (paolotiu)

v2.0.0 (2021-07-11)

Full Changelog

v1.9.0 (2021-07-11)

Full Changelog

v1.8.0 (2021-07-11)

Full Changelog

Closed issues:

  • How to specify data-api on the Plausible script tag via next-plausible? #16
  • Double Pageviews #15
  • Support for proxy #14

v1.7.0 (2021-06-20)

Full Changelog

v1.6.2 (2021-06-18)

Full Changelog

Closed issues:

  • Don't overwrite the enabled variable #13
  • No way to use custom API host? #12

v1.6.1 (2021-04-08)

Full Changelog

Closed issues:

  • Confirming docs and env var usage #10

Merged pull requests:

v1.6.0 (2021-04-06)

Full Changelog

Closed issues:

  • Support outbound link click tracking #8
  • missing attribute when using the SRI #7

Merged pull requests:

v1.5.1 (2021-02-13)

Full Changelog

v1.5.0 (2021-02-12)

Full Changelog

Closed issues:

  • Configurable variable used to check for production #6
  • allow to add SRI to the script tag #5

v1.4.0 (2021-02-10)

Full Changelog

Closed issues:

  • Multiple tag #4

v1.3.1 (2021-01-26)

Full Changelog

Merged pull requests:

v1.2.0 (2021-01-01)

Full Changelog

Merged pull requests:

v1.1.1 (2020-11-11)

Full Changelog

v1.1.0 (2020-11-11)

Full Changelog

Merged pull requests:

  • Add options parameter to event function #1 (altaywtf)

v1.0.4 (2020-10-10)

Full Changelog

v1.0.3 (2020-10-10)

Full Changelog

* This Changelog was automatically generated by [githubchangelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)_