1.1.0 • Published 6 months ago

next-cloudflare-web-analytics v1.1.0

Weekly downloads
-
License
GPL-3.0+
Repository
github
Last release
6 months ago

Next Cloudflare Web Analytics · npm

Simple Integration for Next.js (https://nextjs.org) and Cloudflare Web Analytics (https://www.cloudflare.com/web-analytics). Cloudflare Web Analytics is a simple and lightweight privacy-first analytics and performance metrics service.

Cloudflare Web Analytics Overview

Cloudflare Web Analytics provides free, privacy-first analytics for your website... All you need to enable Cloudflare Web Analytics is a Cloudflare account and a JavaScript snippet on your page to start getting information on page views and visitors.

Usage

Installation

npm install next-cloudflare-web-analytics --save

or

yarn add next-cloudflare-web-analytics

App Router Implementation

If you are using the Next.js App Router, to enable Cloudflare Web Analytics you will need to add the <CloudflareWebAnalyticsProvider /> into the root layout.js:

// app/layout.js
import CloudflareWebAnalyticsProvider from 'next-cloudflare-web-analytics'

export default function RootLayout({children}) {
  return (
    <html lang="en">
      <CloudflareWebAnalyticsProvider token={'XXXXXXXXX'}/>
      <body>{children}</body>
    </html>
  )
}

Pages Router Implementation

If you are using the Next.js Next.js Pages Router, to enable Cloudflare Web Analytics you'll need to add the <CloudflareWebAnalyticsProvider />at the top level of your application inside a custom _app.js:

// pages/_app.js
import CloudflareWebAnalyticsProvider from 'next-cloudflare-web-analytics'

export default function MyApp({Component, pageProps}) {
  return (
    <CloudflareWebAnalyticsProvider token={'XXXXXXXXX'}>
      <Component {...pageProps} />
    </CloudflareWebAnalyticsProvider>
  )
}

Props for CloudflareWebAnalyticsProvider

NameTypeDescription
tokenstringThe token provided by Cloudflare Web Analytics of the site you want to monitor. Required
spaIsDisabledbooleanSet this to true to disable Cloudflare Web Analytics Single Page Application measurements.
enabledbooleanUse this to explicitly decide whether or not to render script. If not passed the script will be rendered in production environments only based on either NODE_ENV or NEXT_PUBLIC_VERCEL_ENV.

Developing

  • npm run build will build the project to the dist folder
  • npm run watch will watch and build the project to the dist folder