diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb index 06d292728..7f8e6fbef 100644 --- a/app/controllers/faqs_controller.rb +++ b/app/controllers/faqs_controller.rb @@ -1,4 +1,5 @@ class FaqsController < ApplicationController + include AhoyTracking skip_before_action :authenticate_user!, only: [ :index, :show ] before_action :set_faq, only: [ :show, :edit, :update, :destroy ] @@ -8,10 +9,12 @@ def index @faqs = faqs.search_by_params(params.to_unsafe_h.slice("query", "published")) .by_position .page(params[:page]) + track_index_intent(Faq, @faqs, params) end def show authorize! @faq + track_view(@faq) end def new diff --git a/spec/requests/faqs_spec.rb b/spec/requests/faqs_spec.rb index 5cc465fd8..3ffd5cf06 100644 --- a/spec/requests/faqs_spec.rb +++ b/spec/requests/faqs_spec.rb @@ -23,6 +23,38 @@ let!(:unpublished_faq) { create(:faq, question: "Unpublished FAQ", answer: "Unpublished FAQ Body") } let!(:public_faq) { create(:faq, :published, question: "Public FAQ", answer: "Public FAQ Body", publicly_visible: true) } + describe "Ahoy tracking" do + before { sign_in admin } + + describe "GET /index" do + it "tracks a search event when a query param is present" do + expect(Analytics::AhoyTracker).to receive(:track_index_intent).with( + anything, Faq, params: anything, result_count: anything + ) + + get faqs_path, params: { query: "Published" } + end + + it "calls track_index_intent even without search params" do + expect(Analytics::AhoyTracker).to receive(:track_index_intent).with( + anything, Faq, params: anything, result_count: anything + ) + + get faqs_path + end + end + + describe "GET /show" do + it "tracks a view event" do + expect(Analytics::AhoyTracker).to receive(:track).with( + anything, :view, published_faq + ) + + get faq_path(published_faq) + end + end + end + describe "GET /index" do context "as an admin" do before { sign_in admin }