The personal finance app for everyone (by everyone)
Ruby
require "posthog"
Rails.configuration.x.posthog = ActiveSupport::OrderedOptions.new
# Explicit opt-in for local analytics testing; production remains enabled.
Rails.configuration.x.posthog.development_enabled = ActiveModel::Type::Boolean.new.cast(ENV.fetch("POSTHOG_DEVELOPMENT_ENABLED", "false"))
# One environment gate for browser analytics, feedback, and configuration health.
def (Rails.configuration.x.posthog).browser_enabled?
Rails.env.production? || (Rails.env.development? && development_enabled)
end
Rails.configuration.x.posthog.api_key = ENV["POSTHOG_KEY"].presence
# .presence (not .fetch) so an explicitly-blank POSTHOG_HOST="" still falls
# back to the default instead of reaching PostHog::Client as an empty string.
Rails.configuration.x.posthog.host = ENV["POSTHOG_HOST"].presence || "https://us.i.posthog.com"
Rails.configuration.x.posthog.feedback_enabled = ActiveModel::Type::Boolean.new.cast(ENV.fetch("POSTHOG_FEEDBACK_ENABLED", "true"))
# Public client configuration for shared feedback, including self-hosted Sankey.
# This write-only project token is safe to distribute; it is not an admin key.
# https://posthog.com/docs/api
Rails.configuration.x.posthog.self_hosted_feedback_project = {
api_key: "phc_D4stYbjnouz4J3H434HjYNQ655XG4vqL99HJH7cA4zMk",
host: "https://us.i.posthog.com"
}.freeze
# Register each feature's survey separately from the shared project destination.
# Managed app/demo deployments provide the survey belonging to their own project.
Rails.configuration.x.posthog.feedback_surveys = {
sankey: {
managed: ENV["POSTHOG_SANKEY_SURVEY_ID"].presence,
self_hosted: "01a0a162-73a2-0000-9402-ffab5bc45b4a"
}.freeze
}.freeze
# Both the server client below AND the browser snippet
# (app/views/shared/_posthog.html.erb, rendered from _head.html.erb) AND
# the CSP script-src/connect-src allowlist (content_security_policy.rb)
# send data to this host — posthog-ruby sends the API key in the request
# body and silently skips TLS when the scheme isn't https, and the browser
# integration would ship analytics/session data in cleartext the same way.
# A single `enabled` method gates all three integration points so a bad
# POSTHOG_HOST disables PostHog everywhere instead of only wherever a
# reviewer happened to look first. Defined as a method (not a value computed
# once at boot) so it re-evaluates against api_key/host, which tests stub.
Rails.configuration.x.posthog.define_singleton_method(:enabled) do
next false unless api_key.present?
scheme = begin
URI.parse(host).scheme
rescue URI::InvalidURIError
nil
end
scheme == "https"
end
if Rails.configuration.x.posthog.api_key.present? && !Rails.configuration.x.posthog.enabled
Rails.logger.error("[PostHog] POSTHOG_HOST (#{Rails.configuration.x.posthog.host}) is not HTTPS — refusing to enable PostHog (server client, browser snippet, and CSP allowlist) to avoid sending the API key/analytics data in cleartext.")
end
if Rails.configuration.x.posthog.enabled
# Initialize PostHog client
$posthog = PostHog::Client.new({
api_key: Rails.configuration.x.posthog.api_key,
host: Rails.configuration.x.posthog.host,
on_error: Proc.new { |status, msg| puts "PostHog error: #{status} - #{msg}" }
})
end
# Daily UI usage shares the bundled feedback destination, independently of any
# installation-owned analytics project. Capture also checks these gates.
feedback_config = Rails.configuration.x.posthog
if feedback_config.feedback_enabled && feedback_config.browser_enabled?
$posthog_feedback = PostHog::Client.new(
feedback_config.self_hosted_feedback_project.merge(
on_error: Proc.new { |status, _msg| Rails.logger.debug("PostHog feedback capture failed (status #{status})") }
)
)
end