Closed
Conversation
Introduce middleware to the Analytics class to allow separating out all the different things we like to do with analytics event logging.
Run spec with RECORD_ANALYTICS=1 to dump analytics events to a newline-delimited JSON file.
matthinz
commented
Dec 20, 2024
matthinz
commented
Dec 20, 2024
matthinz
commented
Dec 20, 2024
- Automatic normalization of _most_ of the happy path - Add AnalyticsRecordingHelper
- Get the desktop happy path tests actually passing
Co-authored-by: Zach Margolis <zachmargolis@users.noreply.github.com>
matthinz
commented
Dec 23, 2024
Comment on lines
+7
to
+37
| # Analytics middleware that sends the event to Ahoy | ||
| class AhoyMiddleware | ||
| attr_reader :ahoy | ||
|
|
||
| def initialize(ahoy: nil, request: nil) | ||
| @ahoy = ahoy || Ahoy::Tracker.new(request:) | ||
| end | ||
|
|
||
| def call(event) | ||
| event.tap do |event| | ||
| ahoy.track(event[:name], event[:properties]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Analytics middleware that augments NewRelic APM trace with additional metadata. | ||
| class NewRelicMiddleware | ||
| def call(event) | ||
| event.tap do | ||
| # Tag NewRelic APM trace with a handful of useful metadata | ||
| # https://www.rubydoc.info/github/newrelic/rpm/NewRelic/Agent#add_custom_attributes-instance_method | ||
| ::NewRelic::Agent.add_custom_attributes( | ||
| user_id: event.dig(:properties, :user_id), | ||
| user_ip: event.dig(:properties, :user_ip), | ||
| service_provider: event.dig(:properties, :service_provider), | ||
| event_name: event[:name], | ||
| git_sha: IdentityConfig::GIT_SHA, | ||
| ) | ||
| end | ||
| end | ||
| end |
Contributor
Author
There was a problem hiding this comment.
I think ideally these would live in app/services/analytics/ and ApplicationController would add them when it's creating its own Analytics instance
matthinz
commented
Dec 23, 2024
| # @param [Hash[]] expected_events Array of analytics event hashes. | ||
| # @param [String,nil] file_name File name events were loaded from (for error reporting) | ||
| # @param [Number] window_size Number of events we look at when trying to find a match. | ||
| def assert_logged_analytics_events_match( |
Contributor
Author
There was a problem hiding this comment.
I think this is more thorough than our current analytics feature spec, since it is requiring that every event seen match 1:1 with an event that was previously recorded.
The current spec defines a set of events, and then asserts that each one was logged. If the app starts firing a new analytics event, that won't cause the analytics spec to fail--it needs to be manually updated to know about the new event.
Contributor
Author
|
Gonna go in a different direction with this, I think. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivated by a bunch of broken analytics specs over on #11674, I wanted to play around with potentially recording analytics event emitted during our IDV end-to-end feature specs and then just asserting that, the next time we run the end-to-end specs, the analytics events look mostly the same.
To record analytics events for the end-to-end spec, you do:
Then, the next time you run the spec, the logged analytics events will be compared against the recording.
This PR doesn't actually remove the IDV analytics feature spec, but the idea is that, with a little care, you could someday.
Left to do: