mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2025-01-03 15:06:01 +03:00
optimize posthog load
This commit is contained in:
parent
cffb008a1b
commit
35a3a0194d
@ -1,20 +0,0 @@
|
||||
import posthog from "posthog-js";
|
||||
import { PUBLIC_POSTHOG_API_KEY } from "$env/static/public";
|
||||
|
||||
async function setupPostHog() {
|
||||
console.log("Initializing PostHog");
|
||||
posthog.init(PUBLIC_POSTHOG_API_KEY, {
|
||||
api_host: "https://eu.posthog.com",
|
||||
capture_performance: false,
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize the database *once*
|
||||
const phSetup = setupPostHog();
|
||||
|
||||
export async function handle({ request, render }) {
|
||||
// Wait for the posthog to initialize the first time
|
||||
// or instantly get the result of when it was initialized
|
||||
const ph = await phSetup;
|
||||
return await render(request);
|
||||
}
|
31
src/lib/posthog.ts
Normal file
31
src/lib/posthog.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import posthog from "posthog-js";
|
||||
import { PUBLIC_POSTHOG_API_KEY } from "$env/static/public";
|
||||
import type { User } from "$lib/api";
|
||||
import { log } from "$lib";
|
||||
|
||||
export default () => {
|
||||
const instance = posthog.init(PUBLIC_POSTHOG_API_KEY, {
|
||||
api_host: "https://eu.posthog.com",
|
||||
capture_performance: false,
|
||||
});
|
||||
log.info("posthog initialized");
|
||||
return {
|
||||
identify: (user: User | undefined) => {
|
||||
if (user) {
|
||||
log.info("posthog identify", {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
});
|
||||
instance?.identify(`user_${user.id}`, {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
});
|
||||
} else {
|
||||
log.info("posthog reset");
|
||||
instance?.capture("log-out");
|
||||
instance?.reset();
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
@ -2,33 +2,18 @@
|
||||
import "../app.postcss";
|
||||
|
||||
import type { LayoutData } from "./$types";
|
||||
import { log } from "$lib";
|
||||
import { onMount } from "svelte";
|
||||
import { BackForwardButtons } from "$lib/components";
|
||||
import { setContext } from "svelte";
|
||||
import { writable } from "svelte/store";
|
||||
import Breadcrumbs from "$lib/components/Breadcrumbs.svelte";
|
||||
import posthog from "posthog-js";
|
||||
|
||||
export let data: LayoutData;
|
||||
const { user, posthog } = data;
|
||||
|
||||
setContext("project", writable(null));
|
||||
setContext("session", writable(null));
|
||||
|
||||
onMount(log.setup);
|
||||
|
||||
export let data: LayoutData;
|
||||
const { user } = data;
|
||||
|
||||
user.subscribe((user) => {
|
||||
if (user) {
|
||||
posthog.identify("user_" + user.id.toString(), {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
});
|
||||
} else {
|
||||
posthog.capture("log-out");
|
||||
posthog.reset();
|
||||
}
|
||||
});
|
||||
user.subscribe(posthog.identify);
|
||||
</script>
|
||||
|
||||
<header
|
||||
|
@ -3,6 +3,8 @@ import type { LayoutLoad } from "./$types";
|
||||
import { building } from "$app/environment";
|
||||
import type { Project } from "$lib/projects";
|
||||
import Api from "$lib/api";
|
||||
import Posthog from "$lib/posthog";
|
||||
import { log } from "$lib";
|
||||
|
||||
export const ssr = false;
|
||||
export const prerender = true;
|
||||
@ -11,25 +13,26 @@ export const csr = true;
|
||||
export const load: LayoutLoad = async ({ fetch }) => {
|
||||
const projects = building
|
||||
? {
|
||||
...readable<Project[]>([]),
|
||||
add: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
get: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
}
|
||||
...readable<Project[]>([]),
|
||||
add: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
get: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
}
|
||||
: await (await import("$lib/projects")).default();
|
||||
const user = building
|
||||
? {
|
||||
...readable<undefined>(undefined),
|
||||
set: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
delete: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
}
|
||||
...readable<undefined>(undefined),
|
||||
set: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
delete: () => {
|
||||
throw new Error("not implemented");
|
||||
},
|
||||
}
|
||||
: await (await import("$lib/users")).default();
|
||||
return { projects, user, api: Api({ fetch }) };
|
||||
await log.setup();
|
||||
return { projects, user, api: Api({ fetch }), posthog: Posthog() };
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user