PostHog support (#313)

This commit is contained in:
Juan José Mata
2025-11-11 16:06:08 +01:00
committed by GitHub
parent c47a790ad9
commit fcf792b560
7 changed files with 36 additions and 0 deletions

View File

@@ -80,6 +80,10 @@ OIDC_REDIRECT_URI=
PRODUCT_NAME=
BRAND_NAME=
# PostHog configuration
POSTHOG_KEY=
POSTHOG_HOST=
# Disable enforcing SSL connections
# DISABLE_SSL=true

View File

@@ -40,6 +40,7 @@ gem "rack-mini-profiler"
gem "sentry-ruby"
gem "sentry-rails"
gem "sentry-sidekiq"
gem "posthog-ruby"
gem "logtail-rails"
gem "skylight", groups: [ :production ]

View File

@@ -425,6 +425,8 @@ GEM
platform_agent (1.0.1)
activesupport (>= 5.2.0)
useragent (~> 0.16.3)
posthog-ruby (3.3.3)
concurrent-ruby (~> 1)
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
@@ -736,6 +738,7 @@ DEPENDENCIES
pagy
pg (~> 1.5)
plaid
posthog-ruby
propshaft
puma (>= 5.0)
rack-attack (~> 6.6)

View File

@@ -27,5 +27,9 @@
<link rel="apple-touch-icon" href="/logo-pwa.png">
<link rel="apple-touch-icon" sizes="512x512" href="/logo-pwa.png">
<% if Rails.env.production? && (posthog_config = Rails.configuration.x.posthog).try(:api_key).present? %>
<%= render "shared/posthog", posthog_api_key: posthog_config.api_key, posthog_host: posthog_config.host %>
<% end %>
<%= yield :head %>
</head>

View File

@@ -0,0 +1,9 @@
<!-- PostHog -->
<script nonce="<%= content_security_policy_nonce %>">
!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 Rr Mr fi Or Ar ci Tr Cr capture Mi calculateEventProperties Lr register register_once register_for_session unregister unregister_for_session Hr getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSurveysLoaded onSessionId getSurveys getActiveMatchingSurveys renderSurvey displaySurvey canRenderSurvey canRenderSurveyAsync identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty Ur jr createPersonProfile zr kr Br 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 Dr debug M Nr getPageViewId captureTraceFeedback captureTraceMetric $r".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('<%= posthog_api_key %>', {
api_host: '<%= posthog_host %>',
defaults: '2025-05-24',
person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well
})
</script>

View File

@@ -11,6 +11,7 @@
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.script_src :self, :https, 'https://us.i.posthog.com'
# policy.style_src :self, :https
# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"

View File

@@ -0,0 +1,14 @@
require "posthog"
Rails.configuration.x.posthog = ActiveSupport::OrderedOptions.new
Rails.configuration.x.posthog.api_key = ENV["POSTHOG_KEY"].presence
Rails.configuration.x.posthog.host = ENV.fetch("POSTHOG_HOST", "https://us.i.posthog.com")
if (api_key = Rails.configuration.x.posthog.api_key).present?
# Initialize PostHog client
$posthog = PostHog::Client.new({
api_key: api_key,
host: Rails.configuration.x.posthog.host,
on_error: Proc.new { |status, msg| puts "PostHog error: #{status} - #{msg}" }
})
end