Skip to content

Chiffre analytics integration for Next.js

License

Notifications You must be signed in to change notification settings

chiffre-io/nextjs-chiffre

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nextjs-chiffre

NPM MIT License Continuous Integration Coverage Status Dependabot Status

Chiffre analytics integration for Next.js

Installation

$ yarn add nextjs-chiffre
or
$ npm install nextjs-chiffre

Usage

In your _document.(jsx|tsx), add the <ChiffreAnalytics> component at the end of <body>:

import Document, { Html, Main, NextScript } from 'next/document'
import { ChiffreAnalytics } from 'next-chiffre-analytics'

export default class MyDocument extends Document {
  static async getInitialProps(ctx: any) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <Html lang="en">
        <body>
          <Main />
          <NextScript />
          {/* ------------------------------------*/}
          <ChiffreAnalytics
            projectID="yourProjectID"
            publicKey="pk.yourProjectPublicKey"
          />
          {/* ----------------------------------- */}
        </body>
      </Html>
    )
  }
}

If you wish to use environment variables to pass the project ID and public key, you must prefix them with NEXT_PUBLIC_ to make them available to the front-end code:

<ChiffreAnalytics
  projectID={process.env.NEXT_PUBLIC_CHIFFRE_PROJECT_ID}
  publicKey={process.env.NEXT_PUBLIC_CHIFFRE_PUBLIC_KEY}
/>

Configuration

The only two arguments required are:

  • projectID: Your project ID can be found in the project url. Eg: https://chiffre.io/projects/yourProjectID.
  • publicKey: Your public key can be found in the project settings, and looks like pk.{43 base64 characters}

Optional props

You can disable the <noscript> behaviour, that logs anonymous visits for users with JavaScript disabled:

<ChiffreAnalytics projectID="..." publicKey="..." disableNoJSAnonymousVisits />

You can declare the allowed domains where analytics can be sent from, using an array of strings or regexes:

<ChiffreAnalytics
  projectID="..."
  publicKey="..."
  allowedDomains={[
    'my-project.io', // Production domain
    /^my-project-.+\.vercel\.app$/ // Preview deployments on Vercel
  ]}
/>

You can set paths to be ignored by page tracking (eg: private pages):

<ChiffreAnalytics
  projectID="..."
  publicKey="..."
  ignorePaths={['/admin/', '/private/']}
/>

If the path of the URL starts with one of the values listed, no events will be sent.

License

MIT - Made with ❤️ by François Best.

About

Chiffre analytics integration for Next.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published