From 6e5ef9795e49bb0a9b4fc806d96df860f772124e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 26 Oct 2024 13:30:59 +0200 Subject: [PATCH] Track ahoy visits on the server side In commit 96ae69fe9, 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 commit b5220effd), 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 commit b5220effd: 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. --- app/assets/javascripts/application.js | 1 - config/initializers/ahoy.rb | 2 -- .../20241026112901_add_tokens_to_visits.rb | 11 ++++++++ db/schema.rb | 6 ++++- spec/system/admin/stats_spec.rb | 27 ++++++++++++++++++- 5 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20241026112901_add_tokens_to_visits.rb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 60f6be495..9351e8ad2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -58,7 +58,6 @@ //= require ckeditor/loader //= require_directory ./ckeditor //= require social-share-button -//= require ahoy //= require app //= require check_all_none //= require comments diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index b4e8817ec..3879d14b0 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -1,5 +1,3 @@ -Ahoy.api = true -Ahoy.server_side_visits = :when_needed Ahoy.mask_ips = true Ahoy.cookies = :none diff --git a/db/migrate/20241026112901_add_tokens_to_visits.rb b/db/migrate/20241026112901_add_tokens_to_visits.rb new file mode 100644 index 000000000..c519cd24a --- /dev/null +++ b/db/migrate/20241026112901_add_tokens_to_visits.rb @@ -0,0 +1,11 @@ +class AddTokensToVisits < ActiveRecord::Migration[7.0] + def change + change_table :visits do |t| + t.string :visit_token + t.string :visitor_token + end + + add_index :visits, :visit_token, unique: true + add_index :visits, [:visitor_token, :started_at] + end +end diff --git a/db/schema.rb b/db/schema.rb index dc9ee1997..2ddb1065a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_10_26_111636) do +ActiveRecord::Schema[7.0].define(version: 2024_10_26_112901) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "plpgsql" @@ -1703,9 +1703,13 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_26_111636) do t.string "utm_content" t.string "utm_campaign" t.datetime "started_at", precision: nil + t.string "visit_token" + t.string "visitor_token" t.index ["started_at"], name: "index_visits_on_started_at" t.index ["user_id"], name: "index_visits_on_user_id" + t.index ["visit_token"], name: "index_visits_on_visit_token", unique: true t.index ["visitor_id", "started_at"], name: "index_visits_on_visitor_id_and_started_at" + t.index ["visitor_token", "started_at"], name: "index_visits_on_visitor_token_and_started_at" end create_table "votation_types", force: :cascade do |t| diff --git a/spec/system/admin/stats_spec.rb b/spec/system/admin/stats_spec.rb index 2ea281266..b93d18996 100644 --- a/spec/system/admin/stats_spec.rb +++ b/spec/system/admin/stats_spec.rb @@ -13,7 +13,32 @@ describe "Stats", :admin do expect(page).to have_content "DEBATES\n1" expect(page).to have_content "PROPOSALS\n2" expect(page).to have_content "COMMENTS\n3" - expect(page).to have_content "VISITS\n4" + expect(page).to have_content "VISITS\n5" + end + + scenario "Visits" do + visit admin_stats_path + refresh + + expect(page).to have_content "VISITS\n1" + + allow_any_instance_of(ActionDispatch::Request).to receive(:user_agent).and_return("Different") + + in_browser(:different) do + visit root_path + + click_link "Debates" + expect(page).to have_link "Start a debate" + + click_link "Proposals" + expect(page).to have_link "Create a proposal" + end + + allow_any_instance_of(ActionDispatch::Request).to receive(:user_agent).and_call_original + + refresh + + expect(page).to have_content "VISITS\n2" end scenario "Votes" do