66import reflex as rx
77
88# PostHog tracking configuration
9- POSTHOG_API_HOST : str = "https://us.i.posthog.com"
9+ POSTHOG_API_HOST : str = "https://pg.reflex.dev"
10+ POSTHOG_UI_HOST : str = "https://us.posthog.com"
1011
1112# PostHog initialization script template
1213POSTHOG_SCRIPT_TEMPLATE : str = """
13- !function(t,e){{
14- var o,n,p,r;
15- e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){{
16- function g(t,e){{
17- var o=e.split(".");
18- 2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){{
19- t.push([e].concat(Array.prototype.slice.call(arguments,0)))
20- }}
21- }}
22- (p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);
23- var u=e;
24- for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){{
25- var e="posthog";
26- return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e
27- }},u.people.toString=function(){{
28- return u.toString(1)+".people (stub)"
29- }},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys getNextSurveyStep onSessionId setPersonProperties".split(" "),n=0;n<o.length;n++)g(u,o[n]);
30- e._i.push([i,s,a])
31- }},e.__SV=1)
32- }}(document,window.posthog||[]);
33-
14+ !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=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",(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||[],u.toString=function(t){{var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e}},u.people.toString=function(){{return u.toString(1)+".people (stub)"}},o="init Dr qr Ci Br Zr Pr capture calculateEventProperties Yr register register_once register_for_session unregister unregister_for_session Xr getFeatureFlag getFeatureFlagPayload getFeatureFlagResult isFeatureEnabled reloadFeatureFlags updateFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSurveysLoaded onSessionId getSurveys getActiveMatchingSurveys renderSurvey displaySurvey cancelPendingSurvey canRenderSurvey canRenderSurveyAsync Jr identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset setIdentity clearIdentity get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException startExceptionAutocapture stopExceptionAutocapture loadToolbar get_property getSessionProperty Wr Vr createPersonProfile setInternalOrTestUser Gr Fr tn opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing get_explicit_consent_status is_capturing clear_opt_in_out_capturing $r debug ki Ur getPageViewId captureTraceFeedback captureTraceMetric Rr".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])}},e.__SV=1)}}(document,window.posthog||[]);
3415posthog.init('{project_id}', {{
3516 api_host: '{api_host}',
17+ ui_host: '{ui_host}',
3618 person_profiles: 'always',
3719 session_recording: {{
3820 recordCrossOriginIframes: true,
@@ -78,6 +60,7 @@ def _track_form_posthog(
7860 """
7961 filtered = {k : v for k , v in form_data .items () if k in allowed_keys }
8062 props_json = json .dumps (filtered )
63+
8164 return rx .call_script (
8265 f"""
8366 if (typeof posthog !== 'undefined') {{
@@ -134,12 +117,14 @@ def track_intro_form_posthog_submission(
134117def get_posthog_trackers (
135118 project_id : str ,
136119 api_host : str = POSTHOG_API_HOST ,
120+ ui_host : str = POSTHOG_UI_HOST ,
137121) -> rx .Component :
138122 """Generate PostHog tracking component for a Reflex application.
139123
140124 Args:
141125 project_id: PostHog project ID (defaults to app's project ID)
142- api_host: PostHog API host URL (defaults to US instance)
126+ api_host: PostHog API host URL (defaults to reverse proxy)
127+ ui_host: PostHog UI host URL for proper link generation
143128
144129 Returns:
145130 rx.Component: Script component needed for PostHog tracking
@@ -148,5 +133,6 @@ def get_posthog_trackers(
148133 POSTHOG_SCRIPT_TEMPLATE .format (
149134 project_id = project_id ,
150135 api_host = api_host ,
136+ ui_host = ui_host ,
151137 )
152138 )
0 commit comments