Gatsby integration

Last updated:

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 plugin takes care of that.


yarn add gatsby-plugin-posthog


npm install --save gatsby-plugin-posthog

How to use

// In your gatsby-config.js
module.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:
apiHost: "<ph_instance_address>",
// 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 instructions on the JS library itself, see JS library.

Ask a question