-
Notifications
You must be signed in to change notification settings - Fork 7
Custom event tracking #286
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Tracking events | ||
|
|
||
| `Aikido::Zen.track_user_event` lets you record things happening in your app — like failed logins, signups, or password resets. Zen sends these to Aikido so patterns can be detected, like someone failing to log in 50 times in a minute. | ||
|
|
||
| ```ruby | ||
| # app/controllers/application_controller.rb | ||
| class ApplicationController < ActionController::Base | ||
| private | ||
|
|
||
| def authenticate_user! | ||
| # Your authentication logic here | ||
| # ... | ||
|
|
||
| unless current_user | ||
| Aikido::Zen.track_user_event("user.login_failed") | ||
| return | ||
| end | ||
|
|
||
| Aikido::Zen.set_user( | ||
| id: current_user.id, | ||
| name: current_user.name | ||
| ) | ||
|
|
||
| Aikido::Zen.track_user_event("user.login_succeeded") | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| Zen automatically picks up the IP address, user agent, and current user (if you called [`setUser`](./user.md)) from the request — you don't need to pass those yourself. | ||
|
|
||
| ## More examples | ||
|
|
||
| ```ruby | ||
| Aikido::Zen.track_user_event("user.signed_up") | ||
| Aikido::Zen.track_user_event("user.password_reset_requested") | ||
| Aikido::Zen.track_user_event("plan.invite_sent") | ||
| Aikido::Zen.track_user_event("payment.failed") | ||
| ``` | ||
|
|
||
| ## Naming events | ||
|
|
||
| Use lowercase with dots to group related events: | ||
|
|
||
| - `user.login_failed` | ||
| - `user.login_succeeded` | ||
| - `user.signed_up` | ||
| - `user.password_reset_requested` | ||
| - `payment.failed` | ||
| - `plan.invite_sent` | ||
|
|
||
| ## Things to know | ||
|
|
||
| `Aikido::Zen.track_user_event` only works inside an HTTP request. If you call it in a background job or a script, nothing gets sent and you'll see a warning in the console. | ||
|
|
||
| If you haven't called `Aikido::Zen.set_user` yet, the event still goes through — it just won't have a user ID attached. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,30 @@ def report(event) | |
| raise | ||
| end | ||
|
|
||
| def send_user_event(event) | ||
|
marksmith marked this conversation as resolved.
marksmith marked this conversation as resolved.
|
||
| event_type = "user_event" | ||
|
|
||
| if @rate_limiter.throttle?(event_type) | ||
|
Member
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. As these events are not send to Aikido Core, maybe not re-use the same rate limiter? (I think this is the case) |
||
| @config.logger.error("Not reporting #{event_type.upcase} event due to rate limiting") | ||
| return | ||
| end | ||
|
|
||
| @config.logger.debug("Reporting #{event_type.upcase} event") | ||
|
|
||
| req = Net::HTTP::Post.new("/api/runtime/events", default_headers) | ||
|
Member
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. These events can only be sent to 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. That's quite an important point for the other agents too. If the agent is unable to connect to
Member
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. Indeed, makes sense. Maybe log if user calls the track() API for the first time? cc. @hansott. |
||
| req.content_type = "application/json" | ||
| req.body = if event.respond_to?(:as_json) | ||
| @config.json_encoder.call(event.as_json) | ||
| else | ||
| @config.json_encoder.call(event) | ||
| end | ||
|
|
||
| request(req, base_url: @config.realtime_settings_updates_endpoint) | ||
| rescue Aikido::Zen::RateLimitedError | ||
| @rate_limiter.open! | ||
| raise | ||
| end | ||
|
|
||
| # Perform an HTTP request against one of our API endpoints, and process the | ||
| # response. | ||
| # | ||
|
|
@@ -133,6 +157,8 @@ def report(event) | |
| response = http.request(request) | ||
|
|
||
| case response | ||
| when Net::HTTPNoContent | ||
| # empty | ||
|
marksmith marked this conversation as resolved.
|
||
| when Net::HTTPSuccess | ||
| begin | ||
| body = decode(response.body, response["Content-Encoding"]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.