Skip to content

Commit

Permalink
Merge pull request #3195 from alphagov/ga4-ecommerce-attr-change
Browse files Browse the repository at this point in the history
Duplicate ecommerce attributes, appending ga4 to the names
  • Loading branch information
AshGDS authored Oct 26, 2023
2 parents 3c64204 + b19ec6d commit 74b3a0a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/live_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions app/presenters/search_result_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions app/views/finders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
Expand Down
18 changes: 18 additions & 0 deletions features/step_definitions/analytics_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 4 additions & 0 deletions spec/presenters/result_set_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 4 additions & 0 deletions spec/presenters/search_result_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 74b3a0a

Please sign in to comment.