feat: add PostHog analytics integration#1
Draft
posthog-eu[bot] wants to merge 1 commit into
Draft
Conversation
Integrate PostHog analytics via the setup wizard. Initialises PostHog through a reusable src/components/posthog.astro inline snippet included in BaseLayout, capturing pageviews and session replays on every page, plus custom events at key interaction points (newsletter signup, activity/story views, CTA clicks). Generated-By: PostHog Code Task-Id: 369d2cea-ed2a-4d9c-bc40-ed1132a3b4e7
Author
|
CI note: No required checks run on this PR. The repository's only GitHub Actions workflow ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Open Knowledge Belgium wanted product analytics on the new marketing site to understand how visitors engage with activities, stories, and the core conversion flows (newsletter signup, "Get involved", hero CTAs). This PR ships the PostHog integration produced by PostHog's setup wizard.
Summary
Adds PostHog analytics using the
posthog-jsSDK. PostHog is initialised through a reusable inline snippet component included in the base layout, so every page captures$pageviewevents and session replays automatically. Eight custom events are captured at key interaction points across the site.Files changed:
src/components/posthog.astro— new inline PostHog initialisation snippet (is:inline, readsPUBLIC_POSTHOG_PROJECT_TOKEN/PUBLIC_POSTHOG_HOST).src/layouts/BaseLayout.astro— includes<PostHog />in every page<head>.src/components/react/NewsletterCTA.tsx— capturesnewsletter_subscribed.src/components/react/Navbar.tsx— capturesget_involved_clicked.src/components/react/Hero.tsx— captureshero_cta_clicked.src/pages/activities/[slug].astro— capturesactivity_viewed,activity_website_clicked,activity_revive_clicked.src/pages/index.astro— capturesstart_activity_clicked.src/pages/stories/[slug].astro— capturesstory_viewed.package.json/pnpm-lock.yaml— addsposthog-js.pnpm-workspace.yaml— build-deps allowlist entry.Custom events captured:
newsletter_subscribedsrc/components/react/NewsletterCTA.tsxactivity_website_clickedsrc/pages/activities/[slug].astroactivity_revive_clickedsrc/pages/activities/[slug].astroget_involved_clickedsrc/components/react/Navbar.tsxhero_cta_clickedsrc/components/react/Hero.tsxstart_activity_clickedsrc/pages/index.astroactivity_viewedsrc/pages/activities/[slug].astrostory_viewedsrc/pages/stories/[slug].astroInsights and dashboards created
How to verify
.envfile, thenpnpm dev).$pageviewevents plus the custom events above arriving within a minute.Environment variables (action needed before PostHog works)
This site is deployed to GitHub Pages, built by the GitHub Actions workflow at
.github/workflows/deploy.yml(usingwithastro/action). Because it's a static site, the PostHog values are baked into the pages at build time — so they must be available to thepnpm buildstep that runs in GitHub Actions.Environment variables are named settings passed to the app when it builds, so the code can read them (like the PostHog token below) without hardcoding them. This repository intentionally keeps
.envfiles out of version control (they're in.gitignore), and the deploy workflow doesn't currently read any of them, so I could not set these up automatically. Until they're provided,posthog.init('')runs with an empty token and no data reaches PostHog.The two values needed:
PUBLIC_POSTHOG_PROJECT_TOKENphc_vQaRxbWFt9SCf9kPRNxf7ePVRCTTt4EYweyNY78nykcxPUBLIC_POSTHOG_HOSThttps://eu.i.posthog.comThese are public values — the token is a client-side PostHog project key that ends up visible in the site's JavaScript anyway, so it's safe to place in the build config.
To make them reach the production build, add an
env:block to the "Build with Astro" step in.github/workflows/deploy.yml:After that change is merged to
main, the next deploy will build the site with PostHog enabled — no further action needed. (I've left the workflow untouched in this PR so a maintainer can review and apply that change deliberately.)Created with PostHog Code