DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Originally published at thinkthroo.com

PostHog configuration in Codebuff codebase.

In this article, we review PostHog configuration in Codebuff codebase. We will look at:

  1. What is PostHog?

  2. analytics.ts in Codebuff.

I study patterns used in an open source project found on Github Trending. For this week, I reviewed Codebuff codebase and wrote this article.

What is PostHog?

PostHog is a single platform for people who build things. They help product engineers build successful products. Literally every piece of SaaS that a product engineer needs. This includes tools for building products, talking to customers, and making sense of all your customer data.

PostHog provides as apps by stage.

Startup/side project

Growth

Scale

Explore all their apps in Product OS.

analytics.ts in Codebuff

I found trackEvent function being called in recreateShell as shown below:

export const recreateShell = async (cwd: string) => { persistentProcess = await createPersistentProcess(cwd) trackEvent(AnalyticsEvent.SHELL_RECREATED, { persistentProcess }) } 
Enter fullscreen mode Exit fullscreen mode

This trackEvent function is imported as shown below at the top of the file:

import { trackEvent } from '../utils/analytics' 
Enter fullscreen mode Exit fullscreen mode

trackEvent definition

This trackEvent function is defined as shown below in utils/analytics.ts:

export function trackEvent( event: AnalyticsEvent, properties?: Record<string, any>, ) { const distinctId = currentUserId if (!distinctId) { return } if (!client) { if (process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod') { throw new Error('Analytics client not initialized') } return } if (process.env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') { if (DEBUG_DEV_EVENTS) { console.log('Analytics event sent', { event, properties, }) } return } client.capture({ distinctId, event, properties, }) } 
Enter fullscreen mode Exit fullscreen mode

The below block prevents sending events in any env other than prod by simply logging it:

if (process.env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') { if (DEBUG_DEV_EVENTS) { console.log('Analytics event sent', { event, properties, }) } return } 
Enter fullscreen mode Exit fullscreen mode

client is a global variable in initialized as shown below:

let client: PostHog | undefined ... export function initAnalytics() { ... client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, { host: process.env.NEXT_PUBLIC_POSTHOG_HOST_URL, enableExceptionAutocapture: process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod', }) ... 
Enter fullscreen mode Exit fullscreen mode

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Want to learn from open-source? Solve challenges inspired by open-source projects.

References:

  1. https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/index.ts#L48

  2. https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/terminal/run-command.ts#L279

  3. https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/utils/analytics.ts#L60

  4. https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/index.ts#L53

  5. https://github.com/CodebuffAI/codebuff/blob/main/npm-app/src/utils/analytics.ts#L38

Top comments (0)