-
Notifications
You must be signed in to change notification settings - Fork 1
Analytics foundation: track_event hook + core instrumentation (COPLAN-20) #116
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module CoPlan | ||
| # Thin facade for the configurable analytics hook. Call sites use | ||
| # `CoPlan::Analytics.track("event_name", user:, **props)`; | ||
| # the host wires `CoPlan.configuration.track_event` to a destination | ||
| # (a MySQL events table, Snowflake, Datadog, etc.). Default is no-op. | ||
| # | ||
| # Errors in the host handler are swallowed and reported via | ||
| # `CoPlan.configuration.error_reporter` so a broken analytics sink | ||
| # never breaks the user request that triggered the event. | ||
| module Analytics | ||
| def self.track(event, user: nil, **properties) | ||
| handler = CoPlan.configuration.track_event | ||
| return unless handler | ||
|
|
||
| event_name = event.to_s | ||
| payload = { | ||
| event: event_name, | ||
| timestamp: Time.current.iso8601, | ||
| user_id: user&.id, | ||
| properties: properties | ||
| } | ||
|
|
||
| handler.call(event_name, payload) | ||
| rescue => e | ||
| reporter = CoPlan.configuration.error_reporter | ||
| reporter&.call(e, { coplan_analytics_event: event.to_s }) | ||
| nil | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe CoPlan::Analytics do | ||
| let(:received) { [] } | ||
| let(:handler) { ->(event, payload) { received << [event, payload] } } | ||
| let(:user) { create(:coplan_user) } | ||
|
|
||
| around do |example| | ||
| previous = CoPlan.configuration.track_event | ||
| CoPlan.configuration.track_event = handler | ||
| example.run | ||
| CoPlan.configuration.track_event = previous | ||
| end | ||
|
|
||
| it "is a no-op when no handler is configured" do | ||
| CoPlan.configuration.track_event = nil | ||
| expect { described_class.track("foo") }.not_to raise_error | ||
| expect(received).to be_empty | ||
| end | ||
|
|
||
| it "invokes the configured handler with event name and payload" do | ||
| freeze_time do | ||
| described_class.track("plan_created", user: user, plan_id: "abc", custom: "value") | ||
|
|
||
| expect(received.length).to eq(1) | ||
| event_name, payload = received.first | ||
| expect(event_name).to eq("plan_created") | ||
| expect(payload).to include( | ||
| event: "plan_created", | ||
| timestamp: Time.current.iso8601, | ||
| user_id: user.id, | ||
| properties: { plan_id: "abc", custom: "value" } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| it "accepts symbol event names and stringifies them" do | ||
| described_class.track(:plan_published, user: user) | ||
| expect(received.first.first).to eq("plan_published") | ||
| expect(received.first.last[:event]).to eq("plan_published") | ||
| end | ||
|
|
||
| it "sends nil user_id when no user given" do | ||
| described_class.track("page_view") | ||
| expect(received.first.last[:user_id]).to be_nil | ||
| end | ||
|
|
||
| it "swallows handler errors and reports them via error_reporter" do | ||
| reported = [] | ||
| previous_reporter = CoPlan.configuration.error_reporter | ||
| CoPlan.configuration.error_reporter = ->(exception, context) { reported << [exception, context] } | ||
| CoPlan.configuration.track_event = ->(_event, _payload) { raise "boom" } | ||
|
|
||
| expect { described_class.track("plan_created", user: user) }.not_to raise_error | ||
|
|
||
| expect(reported.length).to eq(1) | ||
| exception, context = reported.first | ||
| expect(exception.message).to eq("boom") | ||
| expect(context).to eq(coplan_analytics_event: "plan_created") | ||
| ensure | ||
| CoPlan.configuration.error_reporter = previous_reporter | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe CoPlan::Comment, "analytics" do | ||
| let(:user) { create(:coplan_user) } | ||
| let(:thread) { create(:comment_thread, created_by_user: user) } | ||
|
|
||
| it "tracks a comment_created event when a comment is created" do | ||
| events = capture_analytics_events do | ||
| thread.comments.create!( | ||
| author_type: "human", | ||
| author_id: user.id, | ||
| body_markdown: "first comment" | ||
| ) | ||
| end | ||
|
|
||
| comment_events = events.select { |name, _| name == "comment_created" } | ||
| expect(comment_events.length).to eq(1) | ||
|
|
||
| _, payload = comment_events.first | ||
| expect(payload[:user_id]).to eq(user.id) | ||
| expect(payload[:properties]).to include( | ||
| plan_id: thread.plan_id, | ||
| comment_thread_id: thread.id, | ||
| author_type: "human", | ||
| is_first_in_thread: true, | ||
| body_length: "first comment".length | ||
| ) | ||
| expect(payload[:properties][:comment_id]).to be_present | ||
| end | ||
|
|
||
| it "marks subsequent comments as not the first in thread" do | ||
| create(:comment, comment_thread: thread, body_markdown: "first") | ||
|
|
||
| events = capture_analytics_events do | ||
| thread.comments.create!( | ||
| author_type: "human", | ||
| author_id: user.id, | ||
| body_markdown: "second" | ||
| ) | ||
| end | ||
|
|
||
| _, payload = events.find { |name, _| name == "comment_created" } | ||
| expect(payload[:properties][:is_first_in_thread]).to be(false) | ||
| end | ||
|
|
||
| # `first_comment_in_thread?` (used by notify_plan_author) compares UUID | ||
| # strings with `id < ?`, which is not insertion-ordered. The analytics | ||
| # path uses a total-count check instead, so a reply whose UUID happens | ||
| # to sort before the existing first comment still records is_first=false. | ||
| it "is not fooled by a reply whose UUID sorts before earlier comments" do | ||
| high_id = "ffffffff-ffff-ffff-ffff-ffffffffffff" | ||
| low_id = "00000000-0000-0000-0000-000000000001" | ||
|
|
||
| create(:comment, comment_thread: thread, body_markdown: "first", id: high_id) | ||
|
|
||
| events = capture_analytics_events do | ||
| thread.comments.create!( | ||
| author_type: "human", | ||
| author_id: user.id, | ||
| body_markdown: "reply", | ||
| id: low_id | ||
| ) | ||
| end | ||
|
|
||
| reply_event = events.find { |name, payload| name == "comment_created" && payload[:properties][:comment_id] == low_id } | ||
| expect(reply_event).not_to be_nil | ||
| expect(reply_event.last[:properties][:is_first_in_thread]).to be(false) | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a plan is published through the documented API metadata endpoint rather than this web-only
update_statusaction, noplan_publishedevent is emitted; I checkedengine/app/controllers/coplan/api/v1/plans_controller.rband its status-change path only logs the event and triggers reviews at lines 95-101. Agents are explicitly instructed to update status via the API, so publishing a brainstorm plan that way will undercount the core publish KPI even though the status transition succeeds.Useful? React with 👍 / 👎.