Module: Persona::Trackable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/persona/trackable.rb
Defined Under Namespace
Classes: PersonaBuilder
Instance Method Summary collapse
-
#bulk_track!(actions, metadata: {}) ⇒ Object
Bulk track multiple actions at once (no cap enforcement between each).
-
#reset_persona! ⇒ Object
Reset all events for this record.
-
#track!(action, metadata: {}, skip_cap: false) ⇒ Object
Track an action on this record.
Instance Method Details
#bulk_track!(actions, metadata: {}) ⇒ Object
Bulk track multiple actions at once (no cap enforcement between each)
user.bulk_track!([:login, :view_dashboard, :export_report])
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/persona/trackable.rb', line 63 def bulk_track!(actions, metadata: {}) now = Time.current rows = actions.map do |action| { trackable_type: self.class.name, trackable_id: id, action: action.to_s, metadata: , created_at: now, updated_at: now } end PersonaEvent.insert_all!(rows) Persona::Pruner.enforce_cap_for(self) end |
#reset_persona! ⇒ Object
Reset all events for this record
80 81 82 |
# File 'lib/persona/trackable.rb', line 80 def reset_persona! persona_events.delete_all end |
#track!(action, metadata: {}, skip_cap: false) ⇒ Object
Track an action on this record.
user.track!(:login)
user.track!(:purchase, metadata: { plan: "pro" })
user.track!(:purchase, metadata: { plan: "pro" }, skip_cap: true)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/persona/trackable.rb', line 35 def track!(action, metadata: {}, skip_cap: false) action = action.to_s unless self.class._persona_open_tracking if self.class._persona_tracked_actions.any? && !self.class._persona_tracked_actions.include?(action.to_sym) raise Persona::UntrackedActionError, "'#{action}' is not declared on #{self.class.name}. " \ "Add `track :#{action}` in your persona block, or call `open_tracking!`." end end if Persona.configuration.async && defined?(Sidekiq) Persona::AsyncTracker.track(self.class.name, id, action, ) else PersonaEvent.create!( trackable: self, action: action, metadata: ) Persona::Pruner.enforce_cap_for(self) unless skip_cap end end |