Skip to content

Commit

Permalink
Support disabling analytics through env var
Browse files Browse the repository at this point in the history
  • Loading branch information
benjreinhart committed Aug 20, 2024
1 parent c17ae94 commit 271575f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ rm -rf ~/.srcbook

In order to improve Srcbook, we collect some behavioral analytics. We don't collect anything personal or identifiable, our goals are simply to improve the application. The code is open source so you don't have to trust us, you can verify! You can find more information in our [privacy policy](./PRIVACY-POLICY.md).

If you want to disable tracking, you can do so in the settings page of the application.
If you want to disable tracking, you have two options:

1. Run Srcbook with `SRCBOOK_DISABLE_ANALYTICS=true` set in the environment
2. Disable inside the application's settings page

## Development

Expand Down
3 changes: 0 additions & 3 deletions packages/api/dev-server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { WebSocketServer as WsWebSocketServer } from 'ws';
import app from './server/http.mjs';
import webSocketServer from './server/ws.mjs';

import { posthog } from './posthog-client.mjs';

export { SRCBOOK_DIR } from './constants.mjs';

const server = http.createServer(app);
Expand All @@ -19,7 +17,6 @@ server.listen(port, () => {
});

process.on('SIGINT', async function () {
await posthog.shutdown();
server.close();
process.exit();
});
65 changes: 25 additions & 40 deletions packages/api/posthog-client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,48 @@ import { PostHog } from 'posthog-node';
import { getConfig } from './config.mjs';
import { IS_PRODUCTION } from './constants.mjs';

type QueuedEvent = {
const POSTHOG_API_KEY = 'phc_bQjmPYXmbl76j8gW289Qj9XILuu1STRnIfgCSKlxdgu';
const POSTHOG_HOST = 'https://us.i.posthog.com';

type EventType = {
event: string;
properties?: Record<string, any>;
};

class PostHogClient {
private installId: string;
private client: PostHog | null = null;
private isEnabled: boolean = false;
private eventQueue: QueuedEvent[] = [];
private configAnalyticsEnabled: boolean;
private client: PostHog;

constructor(config: { enabledAnalytics: boolean; installId: string }) {
this.isEnabled = config.enabledAnalytics;
this.configAnalyticsEnabled = config.enabledAnalytics;
this.installId = config.installId;

if (this.isEnabled) {
this.client = new PostHog(
// We're sending over API key to GitHub and clients, but it's the only way.
'phc_bQjmPYXmbl76j8gW289Qj9XILuu1STRnIfgCSKlxdgu',
{ host: 'https://us.i.posthog.com' },
);
}

this.flushQueue();
this.client = new PostHog(POSTHOG_API_KEY, { host: POSTHOG_HOST });
}

private flushQueue(): void {
if (!this.isEnabled || !this.client) {
this.eventQueue = []; // Clear the queue if analytics are disabled
return;
}
private get envAnalyticsEnabled(): boolean {
const disabled = process.env.SRCBOOK_DISABLE_ANALYTICS || '';
return disabled.toLowerCase() !== 'true';
}

while (this.eventQueue.length > 0) {
const event = this.eventQueue.shift();
if (event) {
this.client.capture({ ...event, distinctId: this.installId });
}
}
private get isEnabled(): boolean {
return this.configAnalyticsEnabled && this.envAnalyticsEnabled && IS_PRODUCTION;
}

public capture(event: QueuedEvent): void {
if (this.isEnabled && IS_PRODUCTION) {
if (this.client) {
this.client.capture({ ...event, distinctId: this.installId });
}
} else {
this.eventQueue.push(event);
}
// If the config for analytics is changed at run time
// in settings, use this accessor to update the value
public setConfigAnalyticsEnabled(value: boolean) {
console.log('Setting analytics updated:', value);
this.configAnalyticsEnabled = value;
}

public async shutdown(): Promise<void> {
this.flushQueue();
if (this.client) {
await this.client.shutdown();
public capture(event: EventType): void {
if (!this.isEnabled) {
return;
}

this.client.capture({ ...event, distinctId: this.installId });
}
}

const config = await getConfig();
export const posthog = new PostHogClient(config);
export const posthog = new PostHogClient(await getConfig());
5 changes: 5 additions & 0 deletions packages/api/server/http.mts
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,15 @@ router.get('/settings', cors(), async (_req, res) => {
router.post('/settings', cors(), async (req, res) => {
try {
const updated = await updateConfig(req.body);

// Ensure analytics always reflects the user's settings
posthog.setConfigAnalyticsEnabled(Boolean(updated[0] && updated[0].enabledAnalytics));

posthog.capture({
event: 'user updated settings',
properties: { setting_changed: Object.keys(req.body) },
});

return res.json({ result: updated });
} catch (e) {
const error = e as unknown as Error;
Expand Down
1 change: 0 additions & 1 deletion srcbook/src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ server.listen(port, () => {
});

process.on('SIGINT', async () => {
await posthog.shutdown();
server.close();
process.exit();
});

0 comments on commit 271575f

Please sign in to comment.