Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

PostHog support (#313)

Juan José Matafcf792b56041
diff --git a/.env.example b/.env.example
index 174ba9c700..1d372e3e9f 100644
--- a/.env.example
+++ b/.env.example
@@ -80,6 +80,10 @@ OIDC_REDIRECT_URI=
PRODUCT_NAME=
BRAND_NAME=
+# PostHog configuration
+POSTHOG_KEY=
+POSTHOG_HOST=
+
# Disable enforcing SSL connections
# DISABLE_SSL=true
diff --git a/Gemfile b/Gemfile
index 9d4ac1fe96..ede71beb3a 100644
--- a/Gemfile
+++ b/Gemfile
@@ -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 ]
diff --git a/Gemfile.lock b/Gemfile.lock
index d345ab6404..c7df119780 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -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)
diff --git a/app/views/layouts/shared/_head.html.erb b/app/views/layouts/shared/_head.html.erb
index bb797c66a1..8dd683d4be 100644
--- a/app/views/layouts/shared/_head.html.erb
+++ b/app/views/layouts/shared/_head.html.erb
@@ -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>
diff --git a/app/views/shared/_posthog.html.erb b/app/views/shared/_posthog.html.erb
new file mode 100644
index 0000000000..0b73624a36
--- /dev/null
+++ b/app/views/shared/_posthog.html.erb
@@ -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>
diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb
index b3076b38fe..834aa21186 100644
--- a/config/initializers/content_security_policy.rb
+++ b/config/initializers/content_security_policy.rb
@@ -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"
diff --git a/config/initializers/posthog.rb b/config/initializers/posthog.rb
new file mode 100644
index 0000000000..0d87f53b93
--- /dev/null
+++ b/config/initializers/posthog.rb
@@ -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