From d62d169ad6801445dbb3319b1577041b313d090e Mon Sep 17 00:00:00 2001 From: Micaela Cunha Date: Wed, 18 Dec 2024 15:15:37 -0500 Subject: [PATCH 1/5] BCHDCC-107 Added ahoy tracking of locations search along with keywords used and number of results found --- app/controllers/locations_controller.rb | 5 ++- config/initializers/ahoy.rb | 3 ++ spec/controllers/locations_controller_spec.rb | 45 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 60a421e3..ecd96b21 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -49,6 +49,9 @@ def index @exact_match_found = @locations_search.exact_match_found? + # tracks info about the current search + ahoy.track("Perform Search", keywords: params[:keyword], results: @search.locations.total_count) + # caches the search results and renders the view cache_page(@search.locations) if @search.locations.present? @@ -67,7 +70,7 @@ def index def show id = params[:id].split('/').last @location = Location.get(id) - ahoy.track("Location Visit", id:"#{@location.id}") + ahoy.track("Location Visit", id: "#{@location.id}") if current_user.present? @current_user = current_user diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index bbd82749..83837e06 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -8,3 +8,6 @@ class Ahoy::Store < Ahoy::DatabaseStore # we recommend configuring local geocoding as well # see https://github.com/ankane/ahoy#geocoding Ahoy.geocode = false + +# allow RSpec testing of Ahoy tracking +Ahoy.track_bots = Rails.env.test? \ No newline at end of file diff --git a/spec/controllers/locations_controller_spec.rb b/spec/controllers/locations_controller_spec.rb index 015174f3..d0dc3755 100644 --- a/spec/controllers/locations_controller_spec.rb +++ b/spec/controllers/locations_controller_spec.rb @@ -9,4 +9,49 @@ end end end + + describe 'location search tracking' do + it 'tracks any search' do + get :index + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').count + + expect(ahoy_tracked).to eq(1) + end + + it 'tracks a search with params' do + get :index, params: {keyword: 'house'} + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').count + + expect(ahoy_tracked).to eq(1) + end + + it 'tracks the search keywords used' do + get :index, params: {keyword: 'house', main_category: ''} + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + + expect(ahoy_tracked.properties['keywords']).to eq('house') + expect(ahoy_tracked.properties['main_category']).to eq(nil) + end + + it 'tracks the number of search results listed' do + get :index, params: {keyword: 'house', main_category: ''} + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + + expect(ahoy_tracked.properties['results']).to eq(0) + end + + it 'tracks multiple searches within the same visit' do + get :index, params: {keyword: 'house'} + get :index, params: {keyword: 'diapers'} + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').all + + expect(ahoy_tracked.count).to eq(2) + expect(ahoy_tracked.first.visit_id).to eq(ahoy_tracked.last.visit_id) + end + end end From ea720b1e0fb8294d2a2e6c0ef242a549d48725d9 Mon Sep 17 00:00:00 2001 From: Micaela Cunha Date: Fri, 20 Dec 2024 14:52:31 -0500 Subject: [PATCH 2/5] BCHDCC-107 Added main category and subcategories to the search details tracked by ahoy and updated the locations controller tests accordingly --- Gemfile | 1 + app/controllers/locations_controller.rb | 6 ++- spec/controllers/locations_controller_spec.rb | 37 +++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 0231876a..c4e32f61 100644 --- a/Gemfile +++ b/Gemfile @@ -69,6 +69,7 @@ group :test do gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git', ref: '206d2dae4c3bad8b6450a1762d06d315c35801aa' gem 'simplecov', require: false gem 'webmock' + gem 'pry-rails' end group :development do diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ecd96b21..edb0105c 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -50,7 +50,11 @@ def index @exact_match_found = @locations_search.exact_match_found? # tracks info about the current search - ahoy.track("Perform Search", keywords: params[:keyword], results: @search.locations.total_count) + ahoy.track( "Perform Search", + keywords: params[:keyword], + main_category: @main_category_selected_name, + subcategories: @selected_categories, + results: @search.locations.total_count) # caches the search results and renders the view cache_page(@search.locations) if @search.locations.present? diff --git a/spec/controllers/locations_controller_spec.rb b/spec/controllers/locations_controller_spec.rb index d0dc3755..b55dd140 100644 --- a/spec/controllers/locations_controller_spec.rb +++ b/spec/controllers/locations_controller_spec.rb @@ -1,6 +1,18 @@ require 'rails_helper' describe LocationsController do + # create location with service under the 'Health' category + before do + nearby = create(:nearby_loc) + + health = create(:health) + + nearby.services.create!(attributes_for(:service)) + service = nearby.services.first + service.category_ids = [health.id] + service.save + end + describe "GET 'index'" do xit 'returns 200 status code' do VCR.use_cassette('all_results') do @@ -20,7 +32,7 @@ end it 'tracks a search with params' do - get :index, params: {keyword: 'house'} + get :index, params: {keyword: 'house', main_category: '', categories: []} ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').count @@ -28,25 +40,36 @@ end it 'tracks the search keywords used' do - get :index, params: {keyword: 'house', main_category: ''} + get :index, params: {keyword: 'house', main_category: '', categories: []} ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last expect(ahoy_tracked.properties['keywords']).to eq('house') - expect(ahoy_tracked.properties['main_category']).to eq(nil) + expect(ahoy_tracked.properties['main_category']).to eq('') + expect(ahoy_tracked.properties['subcategories']).to eq([]) + end + + it 'tracks the categories and subcategories used' do + get :index, params: {keyword: '', main_category: 'Health', categories: ['Dental Care', 'General Population']} + + ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + + expect(ahoy_tracked.properties['keywords']).to eq('') + expect(ahoy_tracked.properties['main_category']).to eq('Health') + expect(ahoy_tracked.properties['subcategories']).to eq(['Dental Care', 'General Population']) end it 'tracks the number of search results listed' do - get :index, params: {keyword: 'house', main_category: ''} + get :index, params: {keyword: 'house', main_category: 'Health', categories: ['Dental Care', 'General Population']} ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last expect(ahoy_tracked.properties['results']).to eq(0) end - it 'tracks multiple searches within the same visit' do - get :index, params: {keyword: 'house'} - get :index, params: {keyword: 'diapers'} + it 'tracks multiple searches with the same visit id' do + get :index, params: {keyword: 'house', main_category: '', categories: []} + get :index, params: {keyword: 'diapers', main_category: '', categories: []} ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').all From 546b434490f0fb9c92423fdd9f0fedebcf9d1358 Mon Sep 17 00:00:00 2001 From: Micaela Cunha Date: Thu, 26 Dec 2024 13:14:26 -0500 Subject: [PATCH 3/5] BCHDCC-107 Changed variable names in locations controller tests to make code easier to read --- config/initializers/ahoy.rb | 5 +-- spec/controllers/locations_controller_spec.rb | 43 ++++++++++--------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index b2bd6232..a39b8e6d 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -9,11 +9,8 @@ class Ahoy::Store < Ahoy::DatabaseStore # see https://github.com/ankane/ahoy#geocoding Ahoy.geocode = false -# allow RSpec testing of Ahoy tracking -Ahoy.track_bots = Rails.env.test? - # set the duration of a single Ahoy visit Ahoy.visit_duration = 12.hours # allow RSpec testing of Ahoy tracking -Ahoy.track_bots = Rails.env.test? \ No newline at end of file +Ahoy.track_bots = Rails.env.test? diff --git a/spec/controllers/locations_controller_spec.rb b/spec/controllers/locations_controller_spec.rb index 697d0f65..0b1e2d01 100644 --- a/spec/controllers/locations_controller_spec.rb +++ b/spec/controllers/locations_controller_spec.rb @@ -26,9 +26,9 @@ it 'tracks the visited location id' do get :show, params: {id: "#{@nearby.slug}"} - ahoy_tracked = Ahoy::Event.where(name: 'Location Visit', properties: {id: @nearby.id}).count + tracked_visit = Ahoy::Event.where(name: 'Location Visit', properties: {id: @nearby.id}).count - expect(ahoy_tracked).to eq(1) + expect(tracked_visit).to eq(1) end end @@ -50,59 +50,60 @@ end end - describe 'location search tracking' do + + describe 'track locations search with ahoy' do it 'tracks any search' do get :index - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').count + tracked_search = Ahoy::Event.where(name: 'Perform Search').count - expect(ahoy_tracked).to eq(1) + expect(tracked_search).to eq(1) end it 'tracks a search with params' do get :index, params: {keyword: 'house', main_category: '', categories: []} - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').count + tracked_search = Ahoy::Event.where(name: 'Perform Search').count - expect(ahoy_tracked).to eq(1) + expect(tracked_search).to eq(1) end it 'tracks the search keywords used' do get :index, params: {keyword: 'house', main_category: '', categories: []} - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + tracked_search = Ahoy::Event.where(name: 'Perform Search').last - expect(ahoy_tracked.properties['keywords']).to eq('house') - expect(ahoy_tracked.properties['main_category']).to eq('') - expect(ahoy_tracked.properties['subcategories']).to eq([]) + expect(tracked_search.properties['keywords']).to eq('house') + expect(tracked_search.properties['main_category']).to eq('') + expect(tracked_search.properties['subcategories']).to eq([]) end it 'tracks the categories and subcategories used' do get :index, params: {keyword: '', main_category: 'Health', categories: ['Dental Care', 'General Population']} - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + tracked_search = Ahoy::Event.where(name: 'Perform Search').last - expect(ahoy_tracked.properties['keywords']).to eq('') - expect(ahoy_tracked.properties['main_category']).to eq('Health') - expect(ahoy_tracked.properties['subcategories']).to eq(['Dental Care', 'General Population']) + expect(tracked_search.properties['keywords']).to eq('') + expect(tracked_search.properties['main_category']).to eq('Health') + expect(tracked_search.properties['subcategories']).to eq(['Dental Care', 'General Population']) end it 'tracks the number of search results listed' do get :index, params: {keyword: 'house', main_category: 'Health', categories: ['Dental Care', 'General Population']} - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').last + tracked_search = Ahoy::Event.where(name: 'Perform Search').last - expect(ahoy_tracked.properties['results']).to eq(0) + expect(tracked_search.properties['results']).to eq(0) end - it 'tracks multiple searches with the same visit id' do + it 'tracks multiple searches within the same visit' do get :index, params: {keyword: 'house', main_category: '', categories: []} get :index, params: {keyword: 'diapers', main_category: '', categories: []} - ahoy_tracked = Ahoy::Event.where(name: 'Perform Search').all + tracked_search = Ahoy::Event.where(name: 'Perform Search').all - expect(ahoy_tracked.count).to eq(2) - expect(ahoy_tracked.first.visit_id).to eq(ahoy_tracked.last.visit_id) + expect(tracked_search.count).to eq(2) + expect(tracked_search.first.visit_id).to eq(tracked_search.last.visit_id) end end end From 49e25fd0e0184c86f9ec694476813bbabc73d5cf Mon Sep 17 00:00:00 2001 From: Micaela Cunha Date: Mon, 30 Dec 2024 12:11:43 -0500 Subject: [PATCH 4/5] BCHDCC-107 Moved ahoy tracking of performed searches from locations controller to helper file --- app/controllers/locations_controller.rb | 6 +----- app/helpers/ahoy_events_helper.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 843c7dfa..9cd2d07f 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -50,11 +50,7 @@ def index @exact_match_found = @locations_search.exact_match_found? # tracks info about the current search - ahoy.track( "Perform Search", - keywords: params[:keyword], - main_category: @main_category_selected_name, - subcategories: @selected_categories, - results: @search.locations.total_count) + fire_perform_search_event # caches the search results and renders the view cache_page(@search.locations) if @search.locations.present? diff --git a/app/helpers/ahoy_events_helper.rb b/app/helpers/ahoy_events_helper.rb index 386b5ae1..654b0994 100644 --- a/app/helpers/ahoy_events_helper.rb +++ b/app/helpers/ahoy_events_helper.rb @@ -5,4 +5,12 @@ def fire_location_view_event def fire_location_edit_event end + + def fire_perform_search_event + ahoy.track( "Perform Search", + keywords: params[:keyword], + main_category: @main_category_selected_name, + subcategories: @selected_categories, + results: @search.locations.total_count) + end end From 93490b0f84b426d17c158d0943ba7e00e2514f3d Mon Sep 17 00:00:00 2001 From: Micaela Cunha Date: Thu, 9 Jan 2025 17:12:44 -0500 Subject: [PATCH 5/5] Removed JS function to look for a category matching the search keywords every time the main category changes --- .../component/search/_category.html.haml | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/app/views/component/search/_category.html.haml b/app/views/component/search/_category.html.haml index f650e9cb..741fd773 100644 --- a/app/views/component/search/_category.html.haml +++ b/app/views/component/search/_category.html.haml @@ -19,7 +19,6 @@ dataType: 'json', success: function(data) { updateSubcategories(data.sub_cat_array); - checkMatchedCategory(); }, error: function(jqXHR, textStatus, errorThrown) { if (jqXHR.status === 422) { @@ -67,31 +66,11 @@ } } - function checkMatchedCategory() { - var matchedCategoryName = "#{@matched_category&.name}"; - var matchedCategoryParentName = "#{@matched_category&.parent&.name || @matched_category&.name}"; - var keywordMatchedCategory = #{@keyword_matched_category}; - var clearCategories = #{@clear_categories}; - - - if (matchedCategoryName && keywordMatchedCategory) { - $('#main_category').val(matchedCategoryParentName); - - var checkbox = $('input[type="checkbox"][value="' + matchedCategoryName + '"]'); - if (checkbox.length) { - checkbox.prop('checked', true); - } - } else if (clearCategories) { - clearSubcategories(); - } - } - $('#main_category').change(function() { loadSubcategories($(this).val()); }); loadSubcategories($('#main_category').val()); - checkMatchedCategory(); function clearSubcategories() { $('#subcategoriesList').empty();