Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-plausible

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-plausible

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

  • 1.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
44K
increased by16.73%
Maintainers
1
Weekly downloads
 
Created
Source

Next-Plausible · npm version

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

See this commit for a real world example.

Usage

Include the analytics script

To include the Plausible analytics script in your NextJS page just use the PlausibleProvider component:

import PlausibleProvider from 'next-plausible'

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

If you want to include it globally for all your pages you can use the component in your custom _app.js file:

import PlausibleProvider from 'next-plausible'

export default function MyApp({ Component, pageProps }) {
  return (
    <PlausibleProvider domain="example.com">
      <Component {...pageProps} />
    </PlausibleProvider>
  )
}
PlausibleProvider props
NameDescription
domainThe domain of the site you want to monitor.
customDomainSet 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.
trackOutboundLinksSet this to true if you want to enable outbound link click tracking.
excludeSet this if you want to exclude a set of pages from being tracked. See https://plausible.io/docs/excluding-pages for more details.
selfHostedSet this to true if you are self hosting your Plausible instance. Otherwise you will get a 404 when requesting the script.
enabledUse this to explicitly decide whether or not to render script. If not passed the script will be rendered when process.env.NODE_ENV === 'production'.
integrityOptionally define the subresource integrity attribute for extra security.
apiOptionally define a custom endpoint for Plausible events. This can be useful for proxying from a statically generated site as described here.
srcOptionally override the path of the script. This can be useful if you serve a custom script from a statically generated site. If set, the customDomain property will be ignored.

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
})

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>
}

Note: This will only work if you serve your site using next start. Statically generated sites won't be able to rewrite the requests.

Optionally you can overwrite the proxied script subdirectory and name:

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

module.exports = withPlausibleProxy({
  subdirectory: 'yoursubdirectory',
  scriptName: 'scriptName',
})({
  // ...your next js config, if any
})

This will load the script from /js/yoursubdirectory/scriptName.js.

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

  • yarn build will generate the production scripts under the dist folder.

Keywords

FAQs

Package last updated on 11 Jul 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc