Add PostHog analytics integration - #17
Closed
posthog[bot] wants to merge 2 commits into
Closed
Conversation
Instruments anonymous, server-side usage events (profile_initialized, explainer_generation_started, explainer_generated) via posthog-node, and fixes a startup crash: the CLI threw an uncaught exception (even for --help) whenever POSTHOG_PROJECT_TOKEN/POSTHOG_HOST were unset, which is the default for virtually every install. Generated-By: PostHog Code Task-Id: 294b59aa-1890-42ff-8023-f6d58679a684
action.yml is the only production-analogous config this project has (an npm CLI/GitHub Action has no hosted deploy target) — it's the file every consumer's CI reads when they use shilpi1958/pr-explainer@v1. Sets POSTHOG_PROJECT_TOKEN/POSTHOG_HOST there so the instrumented events actually reach PostHog for that usage path. The project token is a write-only capture key (safe to expose publicly, same model as posthog-js browser keys). Generated-By: PostHog Code Task-Id: 294b59aa-1890-42ff-8023-f6d58679a684
Owner
|
Closing as superseded by #18, which already landed the PostHog funnel events ( This draft also conflicts with |
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.
Summary
This PR adds anonymous, server-side PostHog analytics to the CLI using the
posthog-nodeSDK (installed via npm), and fixes a crash the integration introduced.src/posthog.js— new PostHog client (singleton), acapture()helper (personless,$process_person_profile: false, a process-scoped random distinct ID), andshutdownPostHog().src/cli.js— instruments three lifecycle events and awaits SDK shutdown before exit (in a.finally()so it runs on success or failure).package.json/package-lock.json— addsposthog-node@4.18.0(pinned down from the initially-resolved5.47.3, which requires Node^20.20.0 || >=22.22.0while this package declares Node>=18)..env.example— documentsPOSTHOG_PROJECT_TOKENandPOSTHOG_HOST.action.yml— sets those two env vars for the packaged GitHub Action (see Environment variables below).Events instrumented
profile_initializedsrc/cli.jsexplainer_generation_startedsrc/cli.jsexplainer_generatedsrc/cli.jsFix: CLI crashed without PostHog env vars configured
src/posthog.jsthrew an uncaught exception at import time wheneverPOSTHOG_PROJECT_TOKEN/POSTHOG_HOSTwere unset outsideNODE_ENV=production. Since this package is installed globally via npm and run directly (noNODE_ENVset), this broke every invocation — includingpr-explainer --help, which the README lists as a documented sanity check. Changed thethrowto aconsole.errorwarning so the CLI still runs; behavior (loud until configured, silent no-op once set) is otherwise unchanged.Insights and dashboards created
How to verify
POSTHOG_PROJECT_TOKEN=<token> POSTHOG_HOST=https://us.i.posthog.com node src/cli.js init, or via the Action once merged.profile_initialized/explainer_generation_started/explainer_generatedevents arriving.Environment variables
This project has no hosted "production" deployment — it ships as an npm CLI (
npm install -g @shilpi1958/pr-explainer) and an optional packaged GitHub Action (action.yml).POSTHOG_PROJECT_TOKEN/POSTHOG_HOSTare now set directly inaction.yml'senv:block, so analytics work as soon as anyone usesshilpi1958/pr-explainer@v1— no action needed. (The PostHog project token is a write-only capture key, safe to commit — same model as aposthog-jsbrowser key.)npm install -g+ runningpr-explainerlocally): there's no file this reaches automatically. Analytics stay off for that path unless whoever runs it exportsPOSTHOG_PROJECT_TOKENandPOSTHOG_HOSTthemselves — this is inherent to a globally-installed CLI, not something a repo file can configure.Before merging (per the wizard's setup report)
Created with PostHog Code