Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Ismaaa/next-use-posthog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ This package is now maintained by the PostHog core team. ⚠️

Repo here.

next-use-posthog

This is just a convenience wrapper around posthog-js for Next.js

Install

yarn add next-use-posthog

Usage

In pages/_app.js or pages/_app.tsx

JavaScript

import { usePostHog } from 'next-use-posthog'

const App = ({ Component, pageProps }) => {
  usePostHog('API_KEY', {
    api_host: 'https://app.posthog.com',
  })

  return <Component {...pageProps} />
}

export default App

TypeScript

import { usePostHog } from 'next-use-posthog'
import { AppProps } from 'next/app'
import { FC } from 'react'

const App: FC<AppProps> = ({ Component, pageProps }) => {
  usePostHog('API_KEY', {
    api_host: 'https://app.posthog.com',
  })

  return <Component {...pageProps} />
}

export default App

Disable in development

import { usePostHog } from 'next-use-posthog'
import { AppProps } from 'next/app'
import { FC } from 'react'

const App: FC<AppProps> = ({ Component, pageProps }) => {
  usePostHog('API_KEY', {
    api_host: 'https://app.posthog.com',
    loaded: (posthog) => {
      if (process.env.NODE_ENV === 'development') posthog.opt_out_capturing();
    },
  });

  return <Component {...pageProps} />
}

export default App

Dependencies

  • React
  • Next.js