|
| 1 | +"""PostHog analytics tracking integration for Reflex applications.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import reflex as rx |
| 6 | + |
| 7 | +# PostHog tracking configuration |
| 8 | +POSTHOG_API_HOST: str = "https://us.i.posthog.com" |
| 9 | + |
| 10 | +# PostHog initialization script template |
| 11 | +POSTHOG_SCRIPT_TEMPLATE: str = """ |
| 12 | +!function(t,e){{ |
| 13 | + var o,n,p,r; |
| 14 | + e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){{ |
| 15 | + function g(t,e){{ |
| 16 | + var o=e.split("."); |
| 17 | + 2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){{ |
| 18 | + t.push([e].concat(Array.prototype.slice.call(arguments,0))) |
| 19 | + }} |
| 20 | + }} |
| 21 | + (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); |
| 22 | + var u=e; |
| 23 | + for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){{ |
| 24 | + var e="posthog"; |
| 25 | + return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e |
| 26 | + }},u.people.toString=function(){{ |
| 27 | + return u.toString(1)+".people (stub)" |
| 28 | + }},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]); |
| 29 | + e._i.push([i,s,a]) |
| 30 | + }},e.__SV=1) |
| 31 | +}}(document,window.posthog||[]); |
| 32 | +
|
| 33 | +posthog.init('{project_id}', {{ |
| 34 | + api_host: '{api_host}', |
| 35 | + person_profiles: 'always', |
| 36 | + session_recording: {{ |
| 37 | + recordCrossOriginIframes: true, |
| 38 | + }} |
| 39 | +}}); |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +def identify_posthog_user(user_id: str) -> rx.event.EventSpec: |
| 44 | + """Identify a user in PostHog analytics. |
| 45 | +
|
| 46 | + Args: |
| 47 | + user_id: Unique identifier for the user |
| 48 | +
|
| 49 | + Returns: |
| 50 | + rx.event.EventSpec: Event specification for identifying the user in PostHog |
| 51 | + """ |
| 52 | + user_id = json.dumps(user_id) |
| 53 | + |
| 54 | + return rx.call_script( |
| 55 | + f""" |
| 56 | + if (typeof posthog !== 'undefined') {{ |
| 57 | + posthog.identify({user_id}); |
| 58 | + }} |
| 59 | + """ |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def get_posthog_trackers( |
| 64 | + project_id: str, |
| 65 | + api_host: str = POSTHOG_API_HOST, |
| 66 | +) -> rx.Component: |
| 67 | + """Generate PostHog tracking component for a Reflex application. |
| 68 | +
|
| 69 | + Args: |
| 70 | + project_id: PostHog project ID (defaults to app's project ID) |
| 71 | + api_host: PostHog API host URL (defaults to US instance) |
| 72 | +
|
| 73 | + Returns: |
| 74 | + rx.Component: Script component needed for PostHog tracking |
| 75 | + """ |
| 76 | + return rx.script( |
| 77 | + POSTHOG_SCRIPT_TEMPLATE.format( |
| 78 | + project_id=project_id, |
| 79 | + api_host=api_host, |
| 80 | + ) |
| 81 | + ) |
0 commit comments