Giter VIP home page Giter VIP logo

next-plausible's Introduction

Next-Plausible · npm version

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

See 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 4lejandrito#25).

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

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.
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 when process.env.NODE_ENV === 'production'.
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
})

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

This will load the script from /js/yoursubdirectory/scriptName.js and fetch it from http://example.com/js/script.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.

next-plausible's People

Contributors

4lejandrito avatar altaywtf avatar dominikwilkowski avatar igassmann avatar paolotiu avatar sgithuku avatar thepaulmcbride avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.