How to track GitHub stars in PostHog

Mar 27, 2024

GitHub stars are a popular way to track the success of open-sourced projects. Beyond seeing how many stars a repo has, GitHub does little to help you track or analyze these stars.

To help you make better use of stars, this tutorial shows you how to capture them using a combination of PostHog, GitHub webhooks, and Val Town, a platform for writing, running, and scheduling JavaScript functions in your browser.

Creating a webhook handler in Val Town

Our webhook handler will accept a POST request and capture a PostHog event with the data from the request. Val Town is great for doing this because you can reuse the code I already wrote here (just click Fork in the three-dot menu beside Save):

Now create a phProjectAPIKey environment variable in your Val Town settings. Set this value to your PostHog project API key, which you can find in your project settings. Finally, click the three-dot menu next to theSave button then copy the HTTP endpoint link.

Val created

Setting up your webhook in GitHub

With the HTTP endpoint link from Val Town, go the settings page of the repository you want to track. Once there:

  1. Click Webhooks in the sidebar and then Add webhook.
  2. Paste your HTTP endpoint link into the Payload URL and choose application/json as the content type.
  3. Change events that trigger the webhook to selected events, then unselect Pushes and select Stars.
  4. Click Add webhook.

Webhook

Note: You can also choose other GitHub events like deployments, forks, issues, pushes, pull requests, releases, and more.

Now, go to your repo and star it. You should see an event with the relevant information captured in PostHog.

Event in PostHog

Analyzing GitHub stars in PostHog

To visualize star data in PostHog, create an insight by going to the product analytics tab and clicking New insight. Select GitHub Star as the event for your series and visualize it using a time series line chart or total value number.

GitHub Stars insight in PostHog

You can also set up an action and then use the the Slack webhook to send a message every time you receive a new star.

Further reading

Comments