ankane / ahoy

Simple, powerful, first-party analytics for Rails
MIT License
4.23k stars 377 forks source link

Add support for additional values from the controller #521

Closed jhubert closed 1 year ago

jhubert commented 1 year ago

As we look at tracking events, our system needs to records more than just user and request details. We need to know the team the user is on and the company account that they're using.

This change allows you to specify those values in the controller like so:

class ApplicationController < ActionController::Base
  ...
  def ahoy_visit_values
    { team_id: current_team_id, account: Apartment::Tenant.current }
  end

  def ahoy_event_values
    { team_id: current_team_id, account: Apartment::Tenant.current }
  end
end

The additional values need to map to attributes on the event and visit objects.

To implement this, I first looked at using the methods in the tracker.rb file. It felt like changing the data that was passed to track_visit or track_event would make sense. But, then I realized that you're adding user through the data store via the controller and decided that was a more reliable way to do it so I moved it there.

I couldn't figure out the best way to test this given that way that you have the tests setup, but I'm more than happy to put more effort in to add tests and get this ready to merge if you think it's worth adding.

ankane commented 1 year ago

Hey @jhubert, thanks for the PR, but this should already be possible: https://github.com/ankane/ahoy/#track-additional-data

jhubert commented 1 year ago

@ankane 🤦 I'm not sure how I missed that. I read through the docs and was surprised it wasn't supported. Thanks!