Merge pull request #5760 from consuldemocracy/ahoy_visits_server_side

Track ahoy visits on the server side
This commit is contained in:
Javi Martín
2024-11-07 14:08:46 +01:00
committed by GitHub
6 changed files with 47 additions and 6 deletions

View File

@@ -58,7 +58,6 @@
//= require ckeditor/loader
//= require_directory ./ckeditor
//= require social-share-button
//= require ahoy
//= require app
//= require check_all_none
//= require comments

View File

@@ -1,5 +1,3 @@
Ahoy.api = true
Ahoy.server_side_visits = :when_needed
Ahoy.mask_ips = true
Ahoy.cookies = :none

View File

@@ -0,0 +1,5 @@
class RemoveSearchKeywordFromVisits < ActiveRecord::Migration[7.0]
def change
remove_column :visits, :search_keyword, :string
end
end

View File

@@ -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

View File

@@ -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_05_13_135357) 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"
@@ -1686,7 +1686,6 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_13_135357) do
t.text "landing_page"
t.integer "user_id"
t.string "referring_domain"
t.string "search_keyword"
t.string "browser"
t.string "os"
t.string "device_type"
@@ -1704,9 +1703,13 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_13_135357) 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|

View File

@@ -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