I'm using Rails 4.1.5 and Ahoy. I've added an additional field to the Visit model called 'visitor_email' (with a migration). I can manually create a Visit object with a visitor_email.
In config/initializers/ahoy.rb, I'm subscribing to the track_visit data store. Here, I want to add the user's email to the new Visit model. I can see that the attribute being add to the data hash before I call super. But I can also that it is never written to the DB.
class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore
# customize here
def track_visit(data)
# new visit
# if the landing page has a param that indicates it came from the share_profile_mailer
# if request.params[:landing_page] != request.params[:landing_page].split('?visiting_user=')
# data[:visitor_email] = request.params[:landing_page].split('?visiting_user=')[1]
# end
data[:visitor_email] = "example@email.com"
super(data)
end
end
Watching the server, I see:
Processing by Ahoy::VisitsController#create as JSON
Parameters: {"visit_token"=>"d869c631-7f3a-4057-9bc2-eaa9160e9247", "visitor_token"=>"ebe8068e-fef0-4c25-93fd-54fe9e9aac75", "platform"=>"Web", "landing_page"=>"http://localhost:3000/organizations/35?visiting_user=colindmccann@gmail.com", "screen_width"=>1440, "screen_height"=>900, "visit"=>{"visit_token"=>"d869c631-7f3a-4057-9bc2-eaa9160e9247", "visitor_token"=>"ebe8068e-fef0-4c25-93fd-54fe9e9aac75", "landing_page"=>"http://localhost:3000/organizations/35?visiting_user=colindmccann@gmail.com", "screen_height"=>900, "screen_width"=>1440}}
[7, 16] in /Users/colin/Dev/Rails/sponsorcircle/config/initializers/ahoy.rb
7: # if the landing page has a param that indicates it came from the share_profile_mailer
8: # if request.params[:landing_page] != request.params[:landing_page].split('?visiting_user=')
9: # data[:visitor_email] = request.params[:landing_page].split('?visiting_user=')[1]
10: # end
11:
12: data[:visitor_email] = "example@email.com"
13: byebug
=> 14: super(data)
15: end
16: end
(byebug) data
{:started_at=>Mon, 27 May 2019 12:05:55 UTC +00:00, :visitor_email=>"example@email.com"}
(byebug) c
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 312 ORDER BY "users"."id" ASC LIMIT 1
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "visits" ("browser", "device_type", "ip", "landing_page", "os", "screen_height", "screen_width", "started_at", "user_agent", "user_id", "visit_token", "visitor_token") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["browser", "Chrome"], ["device_type", "Desktop"], ["ip", "127.0.0.1"], ["landing_page", "http://localhost:3000/organizations/35?visiting_user=colindmccann@gmail.com"], ["os", "Mac OS X"], ["screen_height", 900], ["screen_width", 1440], ["started_at", "2019-05-27 12:05:55.071724"], ["user_agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"], ["user_id", 312], ["visit_token", "d869c631-7f3a-4057-9bc2-eaa9160e9247"], ["visitor_token", "ebe8068e-fef0-4c25-93fd-54fe9e9aac75"]]
(0.2ms) COMMIT
Completed 200 OK in 7834ms (Views: 0.2ms | ActiveRecord: 4.1ms)
I initially thought this might have something to do with my param not being permitted by the Visit controller. However, I cannot set any Visit field in track_visit (I've tried eg setting browser and utm_medium)
Note: I have restarted the rails server
Am I misunderstanding something here? Or approaching this in the wrong way?