Track ahoy visits on the server side
In commit96ae69fe9, we stopped using cookies to track Ahoy visits and started using a combination of the IP and the browser agent instead. However, since we're still using the legacy logic from Ahoy 1.x to track visits (which we had to add in commitb5220effd), this way of tracking visits doesn't work and counts every page visited by a user as an independent visit. Maybe we could migrate existing data, which uses the `visitor_id` column so it uses the new `visit_token` and `visitor_token` columns, but there's no mention in the Ahoy documentation regarding how to do so. While deciding what to do about this, we found something interesting. For two years, we've been seeing random failures in the `system/admin/tenants_spec.rb` tests, with messages like: ``` 1) Tenants Create Tenant with subdomain Failure/Error: raise TenantNotFound, <<~EXCEPTION_MESSAGE Could not set search path to schemas, they may be invalid: "#{tenant}" #{full_search_path}. Original error: #{exception.class}: #{exception} EXCEPTION_MESSAGE Apartment::TenantNotFound: Could not set search path to schemas, they may be invalid: "earth" "public", "shared_extensions". Original error: ActiveRecord::StatementInvalid: Could not find schema earth ``` And we've found one of the causes: the AJAX requests done by Ahoy to track visits. Sometimes a test that creates or updates a tenant finishes but the Ahoy AJAX request to, say, `earth.lvh.me/ahoy/visits`, is handled by the next test, when the `earth` schema no longer exists, thus raising an `Apartment::TenantNotFound` exception. So by disabling these AJAX requests and tracking the visits in the server instead, we're killing two birds in one stone: we're fixing the bug regarding the visits count and we're reducing the flakiness in our test suite. It looks like we're also removing the "phantom ahoy cookie" we were getting since the mentioned commitb5220effd: an ahoy cookie was quickly set and unset in the browser. Note that, even though we aren't migrating any data, we're still adding the new fields, because some tests started to fail because, when tracking visits in the server without cookies, Ahoy expects the Visit model to have a `visit_token` field.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
Ahoy.api = true
|
||||
Ahoy.server_side_visits = :when_needed
|
||||
Ahoy.mask_ips = true
|
||||
Ahoy.cookies = :none
|
||||
|
||||
|
||||
Reference in New Issue
Block a user