Include repo domain and hash in posthog events

- so we can understand and prioritise forge support
This commit is contained in:
Mattias Granlund 2024-11-13 11:31:17 +01:00
parent 205f335ec9
commit 2291e38a82
4 changed files with 31 additions and 12 deletions

View File

@ -1,6 +1,7 @@
import { getVersion, getName } from '@tauri-apps/api/app';
import { posthog } from 'posthog-js';
import type { User } from '$lib/stores/user';
import type { RepoInfo } from '$lib/url/gitUrl';
import { PUBLIC_POSTHOG_API_KEY } from '$env/static/public';
export async function initPostHog() {
@ -34,6 +35,18 @@ export function resetPostHog() {
}
export function capture(eventName: string, properties: any = undefined) {
console.log('PostHog event', eventName, properties);
posthog.capture(eventName, properties);
}
/**
* Include repo information for all events for the remainder of the session,
* or until cleared.
*/
export function setPostHogRepo(repo: RepoInfo | undefined) {
if (repo) {
posthog.register_for_session({ repoDomain: repo.domain, repoHash: repo.hash });
} else {
posthog.unregister_for_session('repoDomain');
posthog.unregister_for_session('repoHash');
}
}

View File

@ -1,3 +1,4 @@
import { hashCode } from '@gitbutler/ui/utils/string';
import gitUrlParse from 'git-url-parse';
export type RepoInfo = {
@ -6,18 +7,21 @@ export type RepoInfo = {
owner: string;
organization?: string;
protocol?: string;
hash?: string;
};
export function parseRemoteUrl(url: string): RepoInfo | undefined {
try {
const { protocol, name, owner, organization, resource } = gitUrlParse(url);
const hash = hashCode(name + '|' + owner + '|' + name + '|' + organization);
return {
protocol,
domain: resource,
name,
owner,
organization
organization,
hash
};
} catch {
return undefined;

View File

@ -1,4 +1,5 @@
<script lang="ts">
import { setPostHogRepo } from '$lib/analytics/posthog';
import { Project, ProjectService } from '$lib/backend/projects';
import { TemplateService } from '$lib/backend/templateService';
import FileMenuAction from '$lib/barmenuActions/FileMenuAction.svelte';
@ -146,6 +147,10 @@
listServiceStore.set(ghListService);
forgeStore.set(forge);
prService.set(forge ? forge.prService() : undefined);
setPostHogRepo(repoInfo);
return () => {
setPostHogRepo(undefined);
};
});
// Once on load and every time the project id changes

View File

@ -13,18 +13,15 @@ export function camelCaseToTitleCase(input: string) {
return result;
}
export function hashCode(s: string) {
let hash = 0;
let chr;
let i;
export function hashCode(value: string) {
let hash = 5381; // Starting with a prime number
if (s.length === 0) return hash.toString();
for (i = 0; i < s.length; i++) {
chr = s.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
for (let i = 0; i < value.length; i++) {
hash = (hash * 33) ^ value.charCodeAt(i); // Bitwise XOR and multiplication
}
return hash.toString();
// Convert to an unsigned 32-bit integer, then to a hexadecimal string
return (hash >>> 0).toString(16);
}
export function isChar(char: string) {