Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Use the project token from your own PostHog project (Project settings -> Project API key).
# A token copied from documentation reports into someone else's project, whose alerts,
# insights and notifications you cannot read.
VITE_POSTHOG_PROJECT_TOKEN=phc_your_project_token
VITE_POSTHOG_HOST=https://your-posthog-host
PUBLIC_POSTHOG_PROJECT_TOKEN=phc_your_project_token
PUBLIC_POSTHOG_HOST=https://your-posthog-host
66 changes: 0 additions & 66 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,6 @@
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<script>
!(function (t, e) {
var o, n, p, r;
e.__SV ||
(window.posthog && window.posthog.__loaded) ||
((window.posthog = e),
(e._i = []),
(e.init = function (i, s, a) {
function g(t, e) {
var o = e.split(".");
(2 == o.length && ((t = t[o[0]]), (e = o[1])),
(t[e] = function () {
t.push([e].concat(Array.prototype.slice.call(arguments, 0)));
}));
}
p ||
(((p = t.createElement("script")).type = "text/javascript"),
(p.crossOrigin = "anonymous"),
(p.async = !0),
(p.src =
s.api_host.replace(".i.posthog.com", "-assets.i.posthog.com") + "/static/array.js"),
(p.onerror = function () {
p = null;
}),
(r = t.getElementsByTagName("script")[0]).parentNode.insertBefore(p, r));
var u = e;
for (
void 0 !== a ? (u = e[a] = []) : (a = "posthog"),
u.people = u.people || [],
Object.defineProperty(u, "toString", {
configurable: !0,
enumerable: !0,
writable: !0,
value: function (t) {
var e = "posthog";
return ("posthog" !== a && (e += "." + a), t || (e += " (stub)"), e);
},
}),
Object.defineProperty(u.people, "toString", {
configurable: !0,
enumerable: !0,
writable: !0,
value: function () {
return u.toString(1) + ".people (stub)";
},
}),
o =
"Eh Rh Ph Oh Dh qh init yd bd dd fd Sd wc md xd ad Ed Ad Rd Pd Od Fd capture getExtension gr pd gd calculateEventProperties Dd register register_once register_for_session unregister unregister_for_session zd hd jd getFeatureFlag getFeatureFlagPayload getFeatureFlagResult getAllFeatureFlags isFeatureEnabled reloadFeatureFlags updateFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSurveysLoaded onSessionId getSurveys getActiveMatchingSurveys onActiveMatchingSurveysChanged renderSurvey displaySurvey cancelPendingSurvey canRenderSurvey canRenderSurveyAsync Hd identify setPersonProperties unsetPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset Ud shutdown setIdentity clearIdentity get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException addExceptionStep captureLog startExceptionAutocapture stopExceptionAutocapture loadToolbar get_property getSessionProperty qd Td createPersonProfile setInternalOrTestUser Bd Uh zh opt_in_capturing opt_out_capturing Hh has_opted_in_capturing has_opted_out_capturing get_explicit_consent_status is_capturing clear_opt_in_out_capturing $d debug kc Ds getPageViewId captureTraceFeedback captureTraceMetric rd".split(
" ",
),
n = 0;
n < o.length;
n++
)
g(u, o[n]);
e._i.push([i, s, a]);
}),
(e.__SV = 1));
})(document, window.posthog || []);
posthog.init("phc_AS2JXQsqEwNNreH5kra5Eefd5TijExE5eiLfwdzXLhPg", {
api_host: "https://lightcms.lightcms.me", // your managed reverse proxy domain
ui_host: "https://us.posthog.com", // necessary because you're using a proxy, this way links will point back to PostHog properly
defaults: "2026-05-30",
person_profiles: "always",
});
</script>
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
46 changes: 37 additions & 9 deletions src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { HandleClientError } from "@sveltejs/kit";
import posthog from "posthog-js";
import { dev } from "$app/environment";
import { env } from "$env/dynamic/public";
import { captureLog } from "$lib/client/logging";
import { initBrowserTracing, startSpan } from "$lib/client/tracing";

Expand All @@ -17,16 +18,43 @@ function describeError(error: unknown): string {
return String(error);
}

posthog.init("phc_AS2JXQsqEwNNreH5kra5Eefd5TijExE5eiLfwdzXLhPg", {
api_host: "https://lightcms.lightcms.me",
defaults: "2026-05-30",
person_profiles: "always",
});
export function init() {
const key = env.PUBLIC_POSTHOG_PROJECT_TOKEN;
const host = env.PUBLIC_POSTHOG_HOST;

initBrowserTracing({
posthogKey: "phc_AS2JXQsqEwNNreH5kra5Eefd5TijExE5eiLfwdzXLhPg",
posthogHost: "https://lightcms.lightcms.me",
});
if (!key || !host) {
if (dev) {
const variable = !key ? "PUBLIC_POSTHOG_PROJECT_TOKEN" : "PUBLIC_POSTHOG_HOST";
throw new Error(
`${variable} variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once ${variable} is configured`,
);
}
return;
}

posthog.init(key, {
api_host: host,
defaults: "2026-05-30",
person_profiles: "always",
// Include browser identity and session headers on same-origin server requests.
tracing_headers: [window.location.hostname],
capture_exceptions: {
capture_unhandled_errors: true,
capture_unhandled_rejections: true,
capture_console_errors: false,
},
logs: {
serviceName: "lightcms-web",
environment: dev ? "development" : "production",
},
});
posthogInitialized = true;

initBrowserTracing({
posthogKey: key,
posthogHost: host,
});
}

export const handleError: HandleClientError = ({ error }) => {
if (!posthogInitialized) {
Expand Down
14 changes: 10 additions & 4 deletions src/lib/server/ai-observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ import { NodeSDK } from "@opentelemetry/sdk-node";
import { PostHogSpanProcessor } from "@posthog/ai/otel";
import { registerTelemetry } from "ai";

const key = process.env.VITE_POSTHOG_PROJECT_TOKEN;
const host = process.env.VITE_POSTHOG_HOST;
const key = process.env.PUBLIC_POSTHOG_PROJECT_TOKEN;
const host = process.env.PUBLIC_POSTHOG_HOST;

let posthogSpanProcessor: PostHogSpanProcessor | undefined;
let posthogLogProcessor: BatchLogRecordProcessor | undefined;
let posthogLogProvider: LoggerProvider | undefined;
let posthogLogger: ReturnType<LoggerProvider["getLogger"]> | undefined;

if (!key || !host) {
if (!key) {
if (process.env.NODE_ENV !== "production") {
console.warn(
"PostHog AI observability is not configured (VITE_POSTHOG_PROJECT_TOKEN / VITE_POSTHOG_HOST). Traces and logs will not be exported.",
"PUBLIC_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_PROJECT_TOKEN is configured",
);
}
} else if (!host) {
if (process.env.NODE_ENV !== "production") {
console.warn(
"PUBLIC_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_HOST is configured",
);
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/lib/server/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dev } from "$app/environment";
import { env } from "$env/dynamic/private";
import { env } from "$env/dynamic/public";
import { PostHog } from "posthog-node";

let posthogClient: PostHog | undefined;
Expand All @@ -26,13 +26,13 @@ const HTTP_METHODS = new Set(["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST
function getPostHogClient() {
if (posthogClient) return posthogClient;

const key = env.VITE_POSTHOG_PROJECT_TOKEN;
const host = env.VITE_POSTHOG_HOST;
const key = env.PUBLIC_POSTHOG_PROJECT_TOKEN;
const host = env.PUBLIC_POSTHOG_HOST;

if (!key) {
if (dev) {
console.warn(
"VITE_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once VITE_POSTHOG_PROJECT_TOKEN is configured",
"PUBLIC_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_PROJECT_TOKEN is configured",
);
}
return;
Expand All @@ -41,7 +41,7 @@ function getPostHogClient() {
if (!host) {
if (dev) {
console.warn(
"VITE_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once VITE_POSTHOG_HOST is configured",
"PUBLIC_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_HOST is configured",
);
}
return;
Expand Down
17 changes: 17 additions & 0 deletions src/lib/server/speech-ai-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface TranscribeAiSdkOptions {
language?: string;
prompt?: string;
temperature?: number;
observability?: {
distinctId?: string;
sessionId?: string;
traceName?: string;
};
}

export interface AiSdkTranscriptionResult {
Expand Down Expand Up @@ -119,8 +124,20 @@ export async function transcribeWithAiSdk(
providerOptions: {
openai: providerOptions,
},
runtimeContext: options?.observability
? {
distinctId: options.observability.distinctId,
sessionId: options.observability.sessionId,
traceName: options.observability.traceName,
}
: undefined,
telemetry: {
functionId: "cms_speech_transcription",
includeRuntimeContext: {
distinctId: true,
sessionId: true,
traceName: true,
},
recordInputs: true,
recordOutputs: true,
},
Expand Down
7 changes: 7 additions & 0 deletions src/lib/server/transcription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export async function transcribeAudio(
language: options?.language,
prompt: options?.prompt,
temperature: options?.temperature,
observability: options?.observability
? {
distinctId: options.observability.distinctId,
sessionId: options.observability.sessionId,
traceName: "cms_speech_transcription",
}
: undefined,
});

return {
Expand Down
Loading