From 2dd670873ff605740228b5abc9b6502f2f878864 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:08:24 +0100 Subject: [PATCH 1/2] Duplicate ecommerce attributes, appending ga4 to the names This is to decouple our GA4 analytics code from universal analytics --- app/assets/javascripts/live_search.js | 8 ++++++++ app/presenters/search_result_presenter.rb | 2 ++ app/views/finders/show.html.erb | 4 ++++ spec/presenters/result_set_presenter_spec.rb | 4 ++++ spec/presenters/search_result_presenter_spec.rb | 4 ++++ 5 files changed, 22 insertions(+) diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index e6e7404a6..f0bcc52ef 100644 --- a/app/assets/javascripts/live_search.js +++ b/app/assets/javascripts/live_search.js @@ -146,6 +146,14 @@ var consentCookie = GOVUK.getConsentCookie() if (consentCookie && consentCookie.settings) { + if (this.$resultsWrapper) { + this.$resultsWrapper.setAttribute('data-ga4-search-query', this.currentKeywords()) + var sortedBy = this.$resultsWrapper.querySelector('.js-order-results') + if (sortedBy) { + this.$resultsWrapper.setAttribute('data-ga4-ecommerce-variant', sortedBy.options[sortedBy.selectedIndex].text) + } + } + GOVUK.analyticsGa4.Ga4EcommerceTracker.init(referrer) } else { window.addEventListener('cookie-consent', function () { diff --git a/app/presenters/search_result_presenter.rb b/app/presenters/search_result_presenter.rb index d2ecda90e..922ebf050 100644 --- a/app/presenters/search_result_presenter.rb +++ b/app/presenters/search_result_presenter.rb @@ -97,6 +97,8 @@ def ga4_ecommerce_data(path) { ga4_ecommerce_path: path, ga4_ecommerce_content_id: @document.content_id, + ga4_ecommerce_row: 1, + ga4_ecommerce_index: document.index, } end diff --git a/app/views/finders/show.html.erb b/app/views/finders/show.html.erb index 6fa92cdb6..c827b7043 100644 --- a/app/views/finders/show.html.erb +++ b/app/views/finders/show.html.erb @@ -50,10 +50,14 @@ data-analytics-ecommerce data-ga4-ecommerce data-ecommerce-start-index="<%= result_set_presenter.start_offset %>" + data-ga4-ecommerce-start-index="<%= result_set_presenter.start_offset %>" data-list-title="<%= content_item.title %>" + data-ga4-list-title="<%= content_item.title %>" data-search-query="<%= result_set_presenter.user_supplied_keywords %>" + data-ga4-search-query="<%= result_set_presenter.user_supplied_keywords %>" <% unless result_set_presenter.sort_option.nil? %> data-ecommerce-variant="<%= result_set_presenter.sort_option[:data_track_label] %>" + data-ga4-ecommerce-variant="<%= result_set_presenter.sort_option[:data_track_label] %>" <% end %> data-module="gem-track-click" > diff --git a/spec/presenters/result_set_presenter_spec.rb b/spec/presenters/result_set_presenter_spec.rb index 7fb1f679e..9303e29d9 100644 --- a/spec/presenters/result_set_presenter_spec.rb +++ b/spec/presenters/result_set_presenter_spec.rb @@ -137,6 +137,8 @@ ecommerce_path: "/path/to/doc", ga4_ecommerce_path: "/path/to/doc", ga4_ecommerce_content_id: "content_id", + ga4_ecommerce_row: 1, + ga4_ecommerce_index: 1, ecommerce_row: 1, ecommerce_index: 1, track_category: "navFinderLinkClicked", @@ -205,6 +207,8 @@ data_attributes: { ga4_ecommerce_path: "/path/to/doc", ga4_ecommerce_content_id: "content_id", + ga4_ecommerce_row: 1, + ga4_ecommerce_index: 1, }, }, metadata: { diff --git a/spec/presenters/search_result_presenter_spec.rb b/spec/presenters/search_result_presenter_spec.rb index ff2dfdcf7..b37278418 100644 --- a/spec/presenters/search_result_presenter_spec.rb +++ b/spec/presenters/search_result_presenter_spec.rb @@ -67,6 +67,8 @@ data_attributes: { ga4_ecommerce_path: link, ga4_ecommerce_content_id: "content_id", + ga4_ecommerce_row: 1, + ga4_ecommerce_index: 1, ecommerce_path: link, ecommerce_row: 1, ecommerce_index: 1, @@ -110,6 +112,8 @@ data_attributes: { ga4_ecommerce_path: "#{link}/part-path", ga4_ecommerce_content_id: "content_id", + ga4_ecommerce_row: 1, + ga4_ecommerce_index: 1, ecommerce_path: "#{link}/part-path", ecommerce_row: 1, ecommerce_index: 1, From b19ec6d06cd6de2b017ab71a881a06743efc7a07 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:29:50 +0100 Subject: [PATCH 2/2] Add GA4 ecommerce feature test --- features/step_definitions/analytics_steps.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/features/step_definitions/analytics_steps.rb b/features/step_definitions/analytics_steps.rb index edcd73e49..21034d63b 100644 --- a/features/step_definitions/analytics_steps.rb +++ b/features/step_definitions/analytics_steps.rb @@ -36,6 +36,24 @@ expect(first_link["data-ecommerce-path"]).to eq("/restrictions-on-usage-of-spells-within-school-grounds") end +Then(/^the GA4 ecommerce tracking tags are present$/) do + visit finder_path("search/all", q: "breakfast") + + expect(page).to have_selector(".js-live-search-results-block[data-ga4-ecommerce]") + + form = page.find(".js-live-search-results-block[data-ga4-ecommerce]") + expect(form["data-ga4-ecommerce-start-index"]).to eq("1") + expect(form["data-ga4-list-title"]).to eq("Search") + expect(form["data-ga4-search-query"]).to eq("breakfast") + + results = page.all("a[data-ga4-ecommerce-row]") + expect(results.count).to be_positive + + first_link = results.first + + expect(first_link["data-ga4-ecommerce-path"]).to eq("/restrictions-on-usage-of-spells-within-school-grounds") +end + And "I search for lunch" do stub_search_api_request_with_query_param_no_results("lunch")