-
Notifications
You must be signed in to change notification settings - Fork 115
Add PostHog #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PostHog #906
Changes from all commits
ab1aaea
725acc6
64ea39b
bfd7787
33ac55e
31cf711
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,9 @@ gem "flamegraph" | |
|
|
||
| gem "skylight" | ||
|
|
||
| # Analytics | ||
| gem "posthog-ruby" | ||
|
|
||
| gem "geocoder" | ||
|
|
||
| # Airtable syncing | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ def update | |
| if @user.uses_slack_status? | ||
| @user.update_slack_status | ||
| end | ||
| PosthogService.capture(@user, "settings_updated", { fields: user_params.keys }) | ||
| redirect_to is_own_settings? ? my_settings_path : settings_user_path(@user), | ||
| notice: "Settings updated successfully" | ||
| else | ||
|
|
@@ -44,6 +45,7 @@ def rotate_api_key | |
|
|
||
| new_api_key = @user.api_keys.create!(name: "Hackatime key") | ||
|
|
||
| PosthogService.capture(@user, "api_key_rotated") | ||
| render json: { token: new_api_key.token }, status: :ok | ||
| end | ||
| rescue => e | ||
|
|
@@ -54,6 +56,12 @@ def rotate_api_key | |
| def wakatime_setup | ||
| api_key = current_user&.api_keys&.last | ||
| api_key ||= current_user.api_keys.create!(name: "Wakatime API Key") | ||
| @current_user_api_key = api_key&.token | ||
| PosthogService.capture(current_user, "setup_started", { step: 1 }) | ||
| end | ||
|
|
||
| def wakatime_setup_step_2 | ||
| PosthogService.capture(current_user, "setup_step_viewed", { step: 2 }) | ||
| setup_os = detect_setup_os(request.user_agent) | ||
|
|
||
| render inertia: "WakatimeSetup/Index", props: { | ||
|
Comment on lines
57
to
67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The methods Suggested FixRemove the duplicate method definitions for Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
@@ -71,6 +79,20 @@ def wakatime_setup_step_2 | |
| def wakatime_setup_step_3 | ||
| api_key = current_user&.api_keys&.last | ||
| api_key ||= current_user.api_keys.create!(name: "Wakatime API Key") | ||
|
|
||
| @current_user_api_key = api_key&.token | ||
| PosthogService.capture(current_user, "setup_step_viewed", { step: 3 }) | ||
| end | ||
|
Comment on lines
79
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixAdd an explicit Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
| def wakatime_setup_step_4 | ||
| @no_instruction_wording = [ | ||
| "There is no step 4, lol.", | ||
| "There is no step 4, psych!", | ||
| "Tricked ya! There is no step 4.", | ||
| "There is no step 4, gotcha!" | ||
| ].sample | ||
| PosthogService.capture(current_user, "setup_completed", { step: 4 }) | ||
|
|
||
| editor = params[:editor] | ||
|
|
||
| render inertia: "WakatimeSetup/Step3", props: { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| class PosthogService | ||
| class << self | ||
| def capture(user_or_id, event, properties = {}) | ||
| return unless $posthog | ||
|
|
||
| distinct_id = user_or_id.is_a?(User) ? user_or_id.id.to_s : user_or_id.to_s | ||
|
|
||
| $posthog.capture( | ||
| distinct_id: distinct_id, | ||
| event: event, | ||
| properties: properties | ||
| ) | ||
| rescue => e | ||
| Rails.logger.error "PostHog capture error: #{e.message}" | ||
| end | ||
|
|
||
| def identify(user, properties = {}) | ||
| return unless $posthog | ||
|
|
||
| $posthog.identify( | ||
| distinct_id: user.id.to_s, | ||
| properties: { | ||
| slack_uid: user.slack_uid, | ||
| username: user.username, | ||
| timezone: user.timezone, | ||
| country_code: user.country_code, | ||
| created_at: user.created_at&.iso8601, | ||
| admin_level: user.admin_level | ||
| }.merge(properties) | ||
| ) | ||
skyfallwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rescue => e | ||
| Rails.logger.error "PostHog identify error: #{e.message}" | ||
| end | ||
|
|
||
| def capture_once_per_day(user, event, properties = {}) | ||
| return unless $posthog | ||
|
|
||
| cache_key = "posthog_daily:#{user.id}:#{event}:#{Date.current}" | ||
| return if Rails.cache.exist?(cache_key) | ||
|
|
||
| capture(user, event, properties) | ||
| Rails.cache.write(cache_key, true, expires_at: Date.current.end_of_day + 1.hour) | ||
| rescue => e | ||
| Rails.logger.error "PostHog capture_once_per_day error: #{e.message}" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require "posthog" | ||
|
|
||
| if ENV["POSTHOG_API_KEY"].present? | ||
| $posthog = PostHog::Client.new({ | ||
| api_key: ENV["POSTHOG_API_KEY"], | ||
| host: ENV.fetch("POSTHOG_HOST", "https://us.i.posthog.com"), | ||
| on_error: proc { |status, msg| Rails.logger.error "PostHog error: #{status} - #{msg}" } | ||
| }) | ||
| else | ||
| $posthog = nil | ||
skyfallwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.