From 8ae517cc8b47991fac523aea74606571851fac92 Mon Sep 17 00:00:00 2001 From: Amir Zak Date: Mon, 5 May 2025 18:52:16 +0300 Subject: [PATCH 1/2] Added support for posthog identity api --- integrations/posthog/src/index.ts | 35 +++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/integrations/posthog/src/index.ts b/integrations/posthog/src/index.ts index b61a2e9b9..a2f772db2 100644 --- a/integrations/posthog/src/index.ts +++ b/integrations/posthog/src/index.ts @@ -13,13 +13,14 @@ type PostHogRuntimeContext = RuntimeContext< { projectApiKey?: string; instanceAddress?: 'EU' | 'US'; + identifyUsers?: boolean; } > >; export const handleFetchEvent: FetchPublishScriptEventCallback = async ( event, - { environment }: PostHogRuntimeContext, + { environment, currentUser }: PostHogRuntimeContext, ) => { const instancesURLs = { EU: 'https://eu.posthog.com', @@ -27,6 +28,8 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async ( }; const projectApiKey = environment.siteInstallation?.configuration?.projectApiKey; const instanceAddress = environment.siteInstallation?.configuration?.instanceAddress; + const identifyUsers = environment.siteInstallation?.configuration?.identifyUsers ?? false; + if (!projectApiKey) { throw new Error( `The PostHog project API key is missing from the space configuration (ID: ${ @@ -42,17 +45,27 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async ( ); } - return new Response( - (script as string) - .replace('', projectApiKey) - .replace('', instancesURLs[instanceAddress]), - { - headers: { - 'Content-Type': 'application/javascript', - 'Cache-Control': 'max-age=604800', - }, + let scriptContent = script as string; + scriptContent = scriptContent.replace('', projectApiKey); + scriptContent = scriptContent.replace('', instancesURLs[instanceAddress]); + + // Add user identification after PostHog initialization if enabled + if (identifyUsers && currentUser) { + scriptContent += ` +posthog.identify("${currentUser.id}", { + name: "${currentUser.displayName || ''}", + email: "${currentUser.email || ''}", + avatar: "${currentUser.photoURL || ''}" +}); +`; + } + + return new Response(scriptContent, { + headers: { + 'Content-Type': 'application/javascript', + 'Cache-Control': 'max-age=604800', }, - ); + }); }; export default createIntegration({ From 293cc2ab3b5b5bdba9c56719c6711212fa353c25 Mon Sep 17 00:00:00 2001 From: Amir Zak Date: Mon, 5 May 2025 18:56:41 +0300 Subject: [PATCH 2/2] Added missing gitbook manifest --- integrations/posthog/gitbook-manifest.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/integrations/posthog/gitbook-manifest.yaml b/integrations/posthog/gitbook-manifest.yaml index 9524f4a88..dc1a5ee8a 100644 --- a/integrations/posthog/gitbook-manifest.yaml +++ b/integrations/posthog/gitbook-manifest.yaml @@ -44,6 +44,11 @@ configurations: enum: - EU - US + identifyUsers: + type: boolean + title: Identify Users + description: Enable user identification in PostHog to track individual user behavior + default: false required: - projectApiKey - instanceAddress