Skip to content

Commit a56a61e

Browse files
authored
Merge pull request #10325 from gitbutlerapp/analytics-event-sampling
Add sampling for high-volume analytics events and rate-limit flag
2 parents 76baea6 + 784101a commit a56a61e

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

apps/desktop/src/lib/analytics/posthog.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ export class PostHogWrapper {
1919
) {}
2020

2121
capture(eventName: string, properties?: Properties) {
22+
if (shouldIgnoreEvent(eventName, properties)) return;
2223
const context = this.eventContext.getAll();
2324
const newProperties = { ...context, ...properties };
24-
this._instance?.capture(eventName, newProperties);
25+
const skipClientRateLimiting =
26+
eventName === 'tauri_command' && properties?.command !== undefined;
27+
this._instance?.capture(eventName, newProperties, {
28+
skip_client_rate_limiting: skipClientRateLimiting
29+
});
2530
}
2631

2732
captureOnboarding(event: OnboardingEvent, error?: unknown) {
@@ -99,6 +104,35 @@ export class PostHogWrapper {
99104
}
100105
}
101106

107+
type EventDescription = {
108+
name: string;
109+
command: string;
110+
};
111+
112+
const HIGH_VOLUME_EVENTS: EventDescription[] = [
113+
{ name: 'tauri_command', command: 'stack_details' }
114+
];
115+
116+
const MID_VOLUME_EVENTS: EventDescription[] = [
117+
{ name: 'tauri_command', command: 'get_base_branch_data' },
118+
{ name: 'tauri_command', command: 'fetch_from_remotes' }
119+
];
120+
121+
function shouldIgnoreEvent(eventName: string, properties: Properties | undefined): boolean {
122+
if (HIGH_VOLUME_EVENTS.some((e) => e.name === eventName && e.command === properties?.command)) {
123+
if (Math.random() < 0.95) {
124+
return true;
125+
}
126+
}
127+
128+
if (MID_VOLUME_EVENTS.some((e) => e.name === eventName && e.command === properties?.command)) {
129+
if (Math.random() < 0.5) {
130+
return true;
131+
}
132+
}
133+
return false;
134+
}
135+
102136
export enum OnboardingEvent {
103137
ConfirmedAnalytics = 'onboarding_confirmed_analytics',
104138
AddLocalProject = 'onboarding_add_local_project',

apps/desktop/src/lib/state/customHooks.svelte.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
6565
error_message: parsedError?.message,
6666
error_code: parsedError?.code
6767
});
68-
69-
/** TODO: How long do we need to send these duplicates? */
70-
const legacyName = args.failure ? `${actionName} Failed` : `${actionName} Successful`;
71-
posthog?.capture(legacyName, {
72-
actionName,
73-
command,
74-
durationMs,
75-
failure: args.failure,
76-
error: args.error
77-
});
7868
}
7969

8070
async function fetch<T extends TranformerFn>(

0 commit comments

Comments
 (0)