next-use-posthog
Version:
A Next.js hook for PostHog analytics
83 lines (55 loc) • 1.65 kB
Markdown
We now offer a PostHog provider and hooks which makes setup easier and more reliable.
We recommend using [the approach outlined in our documentation](https://posthog.com/docs/integrate/third-party/next-js)
<hr/>
In v2.0.0 we moved PostHog to be a peer dependency so you can control which version of PostHog you use without needing changes to this integration.
`yarn add next-use-posthog`
In `pages/_app.js` or `pages/_app.tsx`
```typescript
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;
```
```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",
loaded: (posthog) => {
if (process.env.NODE_ENV === "development") posthog.opt_out_capturing();
},
});
return <Component {...pageProps} />;
};
export default App;
```
- React
- Next.js
- posthog-js