Thanks to Ritesh Kadmawala for building this!
GitHub: posthog/gatsby-plugin-posthog
Gatsby behaves like a single-page app which means to track $pageview
events special care is needed. This integration takes care of that.
Install
yarn add gatsby-plugin-posthog
or
npm install --save gatsby-plugin-posthog
How to use
// In your gatsby-config.jsmodule.exports = {plugins: [{resolve: `gatsby-plugin-posthog`,options: {// Specify the API key for your PostHog Project (required)apiKey: "<ph_project_api_key>",// Specify the app host if self-hosting (optional, default: https://app.posthog.com)apiHost: "<ph_client_api_host>",// Puts tracking script in the head instead of the body (optional, default: true)head: true,// Enable posthog analytics tracking during development (optional, default: false)isEnabledDevMode: true},},],}
This will automatically start tracking pageviews, clicks and more.
In your code you can access posthog via window.posthog
.
For more instructions, see browser JS library.