From cbbb6ce38e7cf6cb63888daa81f7594d9220ae43 Mon Sep 17 00:00:00 2001 From: Tim Peter Date: Thu, 16 Jun 2016 20:35:45 +0200 Subject: [PATCH 1/4] Install ahoy analytics. See https://ankane.github.io/ahoy/ --- Gemfile | 5 ++++- Gemfile.lock | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 349f807..466ad6c 100644 --- a/Gemfile +++ b/Gemfile @@ -52,4 +52,7 @@ gem 'json' gem 'font-awesome-rails' # search with elasticsearch -gem 'searchkick' \ No newline at end of file +gem 'searchkick' + +# ahoy analytics +gem 'ahoy_matey' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 4af4313..5a9b648 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,9 +36,22 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.4.0) + ahoy_matey (1.4.0) + addressable + browser (~> 2.0) + geocoder + rack-attack + railties + referer-parser (>= 0.3.0) + request_store + safely_block + user_agent_parser + uuidtools arel (6.0.3) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) + browser (2.1.0) builder (3.2.2) byebug (8.0.1) database_cleaner (1.5.1) @@ -57,6 +70,7 @@ GEM elasticsearch-transport (1.0.17) faraday multi_json + errbase (0.0.3) erubis (2.7.0) execjs (2.6.0) factory_girl (4.5.0) @@ -68,6 +82,7 @@ GEM multipart-post (>= 1.2, < 3) font-awesome-rails (4.5.0.0) railties (>= 3.2, < 5.0) + geocoder (1.3.7) globalid (0.3.6) activesupport (>= 4.1.0) haml (4.0.7) @@ -107,6 +122,8 @@ GEM mini_portile (~> 0.6.0) pg (0.18.3) rack (1.6.4) + rack-attack (4.4.1) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.2.4) @@ -134,6 +151,8 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (10.4.2) + referer-parser (0.3.0) + request_store (1.3.1) rspec-core (3.3.2) rspec-support (~> 3.3.0) rspec-expectations (3.3.1) @@ -154,6 +173,8 @@ GEM ruby-progressbar (1.7.5) ruby_parser (3.8.1) sexp_processor (~> 4.1) + safely_block (0.1.1) + errbase sass (3.4.19) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -181,6 +202,8 @@ GEM uglifier (2.7.2) execjs (>= 0.3.0) json (>= 1.8.0) + user_agent_parser (2.3.0) + uuidtools (2.1.5) web-console (2.2.1) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) @@ -191,6 +214,7 @@ PLATFORMS ruby DEPENDENCIES + ahoy_matey byebug database_cleaner decent_exposure From 3925cefaa4cc9a2fcc2e110d6e272719c4651175 Mon Sep 17 00:00:00 2001 From: Tim Peter Date: Thu, 16 Jun 2016 20:37:57 +0200 Subject: [PATCH 2/4] Generate ahoy models and migrate. --- app/models/ahoy/event.rb | 8 +++ app/models/visit.rb | 4 ++ config/initializers/ahoy.rb | 3 + db/migrate/20160616183629_create_visits.rb | 57 +++++++++++++++++++ .../20160616183630_create_ahoy_events.rb | 19 +++++++ db/schema.rb | 46 ++++++++++++++- 6 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 app/models/ahoy/event.rb create mode 100644 app/models/visit.rb create mode 100644 config/initializers/ahoy.rb create mode 100644 db/migrate/20160616183629_create_visits.rb create mode 100644 db/migrate/20160616183630_create_ahoy_events.rb diff --git a/app/models/ahoy/event.rb b/app/models/ahoy/event.rb new file mode 100644 index 0000000..2aac3c5 --- /dev/null +++ b/app/models/ahoy/event.rb @@ -0,0 +1,8 @@ +module Ahoy + class Event < ActiveRecord::Base + self.table_name = "ahoy_events" + + belongs_to :visit + belongs_to :user + end +end diff --git a/app/models/visit.rb b/app/models/visit.rb new file mode 100644 index 0000000..6bb47fe --- /dev/null +++ b/app/models/visit.rb @@ -0,0 +1,4 @@ +class Visit < ActiveRecord::Base + has_many :ahoy_events, class_name: "Ahoy::Event" + belongs_to :user +end diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb new file mode 100644 index 0000000..e486b02 --- /dev/null +++ b/config/initializers/ahoy.rb @@ -0,0 +1,3 @@ +class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore + # customize here +end diff --git a/db/migrate/20160616183629_create_visits.rb b/db/migrate/20160616183629_create_visits.rb new file mode 100644 index 0000000..ca930ee --- /dev/null +++ b/db/migrate/20160616183629_create_visits.rb @@ -0,0 +1,57 @@ +class CreateVisits < ActiveRecord::Migration + def change + create_table :visits do |t| + t.string :visit_token + t.string :visitor_token + + # the rest are recommended but optional + # simply remove the columns you don't want + + # standard + t.string :ip + t.text :user_agent + t.text :referrer + t.text :landing_page + + # user + t.integer :user_id + # add t.string :user_type if polymorphic + + # traffic source + t.string :referring_domain + t.string :search_keyword + + # technology + t.string :browser + t.string :os + t.string :device_type + t.integer :screen_height + t.integer :screen_width + + # location + t.string :country + t.string :region + t.string :city + t.string :postal_code + t.decimal :latitude + t.decimal :longitude + + # utm parameters + t.string :utm_source + t.string :utm_medium + t.string :utm_term + t.string :utm_content + t.string :utm_campaign + + # native apps + # t.string :platform + # t.string :app_version + # t.string :os_version + + t.timestamp :started_at + end + + add_index :visits, [:visit_token], unique: true + add_index :visits, [:user_id] + end +end diff --git a/db/migrate/20160616183630_create_ahoy_events.rb b/db/migrate/20160616183630_create_ahoy_events.rb new file mode 100644 index 0000000..af9fc25 --- /dev/null +++ b/db/migrate/20160616183630_create_ahoy_events.rb @@ -0,0 +1,19 @@ +class CreateAhoyEvents < ActiveRecord::Migration + def change + create_table :ahoy_events do |t| + t.integer :visit_id + + # user + t.integer :user_id + # add t.string :user_type if polymorphic + + t.string :name + t.json :properties + t.timestamp :time + end + + add_index :ahoy_events, [:visit_id, :name] + add_index :ahoy_events, [:user_id, :name] + add_index :ahoy_events, [:name, :time] + end +end diff --git a/db/schema.rb b/db/schema.rb index 1ca0a25..9ebf106 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160613085819) do +ActiveRecord::Schema.define(version: 20160616183630) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,18 @@ t.text "relevant_codes_it" end + create_table "ahoy_events", force: :cascade do |t| + t.integer "visit_id" + t.integer "user_id" + t.string "name" + t.json "properties" + t.datetime "time" + end + + add_index "ahoy_events", ["name", "time"], name: "index_ahoy_events_on_name_and_time", using: :btree + add_index "ahoy_events", ["user_id", "name"], name: "index_ahoy_events_on_user_id_and_name", using: :btree + add_index "ahoy_events", ["visit_id", "name"], name: "index_ahoy_events_on_visit_id_and_name", using: :btree + create_table "drgs", force: :cascade do |t| t.string "code" t.string "version" @@ -90,4 +102,36 @@ t.integer "application_year" end + create_table "visits", force: :cascade do |t| + t.string "visit_token" + t.string "visitor_token" + t.string "ip" + t.text "user_agent" + t.text "referrer" + 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" + t.integer "screen_height" + t.integer "screen_width" + t.string "country" + t.string "region" + t.string "city" + t.string "postal_code" + t.decimal "latitude" + t.decimal "longitude" + t.string "utm_source" + t.string "utm_medium" + t.string "utm_term" + t.string "utm_content" + t.string "utm_campaign" + t.datetime "started_at" + end + + add_index "visits", ["user_id"], name: "index_visits_on_user_id", using: :btree + add_index "visits", ["visit_token"], name: "index_visits_on_visit_token", unique: true, using: :btree + end From 64b8f8f5f33c42f7cb42dc5bff5907d700b863d7 Mon Sep 17 00:00:00 2001 From: Tim Peter Date: Thu, 16 Jun 2016 20:57:37 +0200 Subject: [PATCH 3/4] Track events and visits. --- app/controllers/search_controller.rb | 6 ++++++ app/controllers/systems_controller.rb | 8 ++++++++ config/initializers/ahoy.rb | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 823e37c..08be369 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -2,6 +2,7 @@ class SearchController < ApplicationController include SearchHelper before_action :set_variables + after_filter :track_action # JSON / HTML API for hospital search # parameters: @@ -84,6 +85,11 @@ def all end end + protected + def track_action + ahoy.track "#{controller_name}##{action_name}", request.filtered_parameters + end + private def set_variables @system = System.find(params[:system_id]) diff --git a/app/controllers/systems_controller.rb b/app/controllers/systems_controller.rb index 3d5d22f..73b0fca 100644 --- a/app/controllers/systems_controller.rb +++ b/app/controllers/systems_controller.rb @@ -1,5 +1,6 @@ class SystemsController < ApplicationController before_action :set_variables, :except => ['index'] + after_filter :track_action def show end @@ -13,6 +14,13 @@ def index redirect_to system_path(@system) end + + protected + def track_action + ahoy.track "Processed #{controller_name}##{action_name}", request.filtered_parameters + ahoy.track_visit + end + private def set_variables @system = System.find(params[:id]) diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index e486b02..395898d 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -1,3 +1,3 @@ class Ahoy::Store < Ahoy::Stores::ActiveRecordTokenStore - # customize here + Ahoy.track_visits_immediately = true end From 1f8408e2aadec80c48b1932937336b3782870816 Mon Sep 17 00:00:00 2001 From: Tim Peter Date: Thu, 16 Jun 2016 20:59:22 +0200 Subject: [PATCH 4/4] Remove search tracking as it generates too much data and slows down search. --- app/controllers/search_controller.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 08be369..823e37c 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -2,7 +2,6 @@ class SearchController < ApplicationController include SearchHelper before_action :set_variables - after_filter :track_action # JSON / HTML API for hospital search # parameters: @@ -85,11 +84,6 @@ def all end end - protected - def track_action - ahoy.track "#{controller_name}##{action_name}", request.filtered_parameters - end - private def set_variables @system = System.find(params[:system_id])