Skip to content

Commit

Permalink
Merge pull request #358 from bchd/BCHDCC-107-search-related-register-…
Browse files Browse the repository at this point in the history
…all-locations-index-views-with-result-details

[BCHDCC-107] Search Related- Register all location index views with details about results
  • Loading branch information
micaelacunhadev authored Jan 13, 2025
2 parents c4afc0e + 93490b0 commit 17e8a1a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def index

@exact_match_found = @locations_search.exact_match_found?

# tracks info about the current search
fire_perform_search_event

# caches the search results and renders the view
cache_page(@search.locations) if @search.locations.present?

Expand Down
8 changes: 8 additions & 0 deletions app/helpers/ahoy_events_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ def fire_location_view_event
def fire_location_update_event
ahoy.track("Location Update", id: @location.id)
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
21 changes: 0 additions & 21 deletions app/views/component/search/_category.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
dataType: 'json',
success: function(data) {
updateSubcategories(data.sub_cat_array);
checkMatchedCategory();
},
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 422) {
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions config/initializers/ahoy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +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?
61 changes: 59 additions & 2 deletions spec/controllers/locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -49,4 +49,61 @@
expect(ahoy_events.first.visit_id).to eq(ahoy_events.last.visit_id)
end
end


describe 'track locations search with ahoy' do
it 'tracks any search' do
get :index

tracked_search = Ahoy::Event.where(name: 'Perform Search').count

expect(tracked_search).to eq(1)
end

it 'tracks a search with params' do
get :index, params: {keyword: 'house', main_category: '', categories: []}

tracked_search = Ahoy::Event.where(name: 'Perform Search').count

expect(tracked_search).to eq(1)
end

it 'tracks the search keywords used' do
get :index, params: {keyword: 'house', main_category: '', categories: []}

tracked_search = Ahoy::Event.where(name: 'Perform Search').last

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']}

tracked_search = Ahoy::Event.where(name: 'Perform Search').last

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']}

tracked_search = Ahoy::Event.where(name: 'Perform Search').last

expect(tracked_search.properties['results']).to eq(0)
end

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: []}

tracked_search = Ahoy::Event.where(name: 'Perform Search').all

expect(tracked_search.count).to eq(2)
expect(tracked_search.first.visit_id).to eq(tracked_search.last.visit_id)
end
end
end

0 comments on commit 17e8a1a

Please sign in to comment.