@@ -13,20 +13,23 @@ type PostHogRuntimeContext = RuntimeContext<
13
13
{
14
14
projectApiKey ?: string ;
15
15
instanceAddress ?: 'EU' | 'US' ;
16
+ identifyUsers ?: boolean ;
16
17
}
17
18
>
18
19
> ;
19
20
20
21
export const handleFetchEvent : FetchPublishScriptEventCallback = async (
21
22
event ,
22
- { environment } : PostHogRuntimeContext ,
23
+ { environment, currentUser } : PostHogRuntimeContext ,
23
24
) => {
24
25
const instancesURLs = {
25
26
EU : 'https://eu.posthog.com' ,
26
27
US : 'https://app.posthog.com' ,
27
28
} ;
28
29
const projectApiKey = environment . siteInstallation ?. configuration ?. projectApiKey ;
29
30
const instanceAddress = environment . siteInstallation ?. configuration ?. instanceAddress ;
31
+ const identifyUsers = environment . siteInstallation ?. configuration ?. identifyUsers ?? false ;
32
+
30
33
if ( ! projectApiKey ) {
31
34
throw new Error (
32
35
`The PostHog project API key is missing from the space configuration (ID: ${
@@ -42,17 +45,27 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async (
42
45
) ;
43
46
}
44
47
45
- return new Response (
46
- ( script as string )
47
- . replace ( '<ph_project_api_key>' , projectApiKey )
48
- . replace ( '<ph_instance_address>' , instancesURLs [ instanceAddress ] ) ,
49
- {
50
- headers : {
51
- 'Content-Type' : 'application/javascript' ,
52
- 'Cache-Control' : 'max-age=604800' ,
53
- } ,
48
+ let scriptContent = script as string ;
49
+ scriptContent = scriptContent . replace ( '<ph_project_api_key>' , projectApiKey ) ;
50
+ scriptContent = scriptContent . replace ( '<ph_instance_address>' , instancesURLs [ instanceAddress ] ) ;
51
+
52
+ // Add user identification after PostHog initialization if enabled
53
+ if ( identifyUsers && currentUser ) {
54
+ scriptContent += `
55
+ posthog.identify("${ currentUser . id } ", {
56
+ name: "${ currentUser . displayName || '' } ",
57
+ email: "${ currentUser . email || '' } ",
58
+ avatar: "${ currentUser . photoURL || '' } "
59
+ });
60
+ ` ;
61
+ }
62
+
63
+ return new Response ( scriptContent , {
64
+ headers : {
65
+ 'Content-Type' : 'application/javascript' ,
66
+ 'Cache-Control' : 'max-age=604800' ,
54
67
} ,
55
- ) ;
68
+ } ) ;
56
69
} ;
57
70
58
71
export default createIntegration < PostHogRuntimeContext > ( {
0 commit comments