From 96ab6d87fd4019bc937e25f35dd57c3918233936 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 18 Oct 2023 16:04:28 +0100 Subject: [PATCH] Flatten GA4 attributes - we no longer need to nest GA4 attributes attached to elements, notably the index attributes - these attributes when collected by trackers are automatically nested based on the structure of the GA4 schema, held in the gem - flattening them here saves code and complexity --- app/helpers/topic_list_helper.rb | 5 +- .../taxon_organisations_presenter.rb | 8 +-- app/presenters/taxon_presenter.rb | 8 +-- app/views/browse/index.html.erb | 4 +- app/views/browse/show.html.erb | 4 +- app/views/components/_topic_list.html.erb | 16 ++--- .../coronavirus_landing_page/show.html.erb | 12 ++-- .../organisations/_latest_documents.html.erb | 2 +- .../second_level_browse_page/_links.html.erb | 8 +-- app/views/shared/_announcements.html.erb | 6 +- app/views/subtopics/_subtopic.html.erb | 2 +- app/views/taxons/_common.html.erb | 2 +- .../_organisation_logos_and_list.html.erb | 10 ++- app/views/taxons/_organisations.html.erb | 6 +- app/views/taxons/show.html.erb | 9 +-- app/views/topical_events/show.html.erb | 6 +- app/views/world_location_news/show.html.erb | 6 +- .../world_wide_taxons/_email_alerts.html.erb | 6 +- spec/components/topic_list_spec.rb | 22 ++---- .../services_and_information_browsing_spec.rb | 12 ++-- .../taxon_organisations_presenter_spec.rb | 72 +++++++------------ 21 files changed, 87 insertions(+), 139 deletions(-) diff --git a/app/helpers/topic_list_helper.rb b/app/helpers/topic_list_helper.rb index 30c8d95c5..d9c933532 100644 --- a/app/helpers/topic_list_helper.rb +++ b/app/helpers/topic_list_helper.rb @@ -20,9 +20,8 @@ def topic_list_tracking_attributes(list_count, list_index, category) def topic_list_params(list, tracking_attributes: nil, list_index: nil, category: nil, list_count: nil, list_title: nil) tracking_attributes ||= topic_list_tracking_attributes(list.count, list_index, category) ga4_data = {} - ga4_data[:index] = {} - ga4_data[:index][:index_section] = list_index + 1 if list_index - ga4_data[:index][:index_section_count] = list_count if list_count + ga4_data[:index_section] = list_index + 1 if list_index + ga4_data[:index_section_count] = list_count if list_count ga4_data[:section] = list_title if list_title { diff --git a/app/presenters/taxon_organisations_presenter.rb b/app/presenters/taxon_organisations_presenter.rb index cb942f154..cc111defd 100644 --- a/app/presenters/taxon_organisations_presenter.rb +++ b/app/presenters/taxon_organisations_presenter.rb @@ -79,11 +79,9 @@ def data_attributes(base_path, link_text, index) ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: index, - index_section: @index_section, - index_section_count: @index_section_count, - }, + index_link: index, + index_section: @index_section, + index_section_count: @index_section_count, index_total: organisations.count, section: "Organisations", }, diff --git a/app/presenters/taxon_presenter.rb b/app/presenters/taxon_presenter.rb index 278a77be5..f2b9296f7 100644 --- a/app/presenters/taxon_presenter.rb +++ b/app/presenters/taxon_presenter.rb @@ -39,11 +39,9 @@ def options_for_child_taxon(index:) ga4_link: { event_name: "navigation", type: "subtopic list", - index: { - index_link: (index + 1).to_s, - index_section: page_section_total.to_s, - index_section_count: page_section_total.to_s, - }, + index_link: (index + 1).to_s, + index_section: page_section_total.to_s, + index_section_count: page_section_total.to_s, index_total: child_taxons.count, section: I18n.t("taxons.explore_sub_topics", locale: :en), }, diff --git a/app/views/browse/index.html.erb b/app/views/browse/index.html.erb index 2740da26d..c9566a1b4 100644 --- a/app/views/browse/index.html.erb +++ b/app/views/browse/index.html.erb @@ -48,9 +48,7 @@ ga4_link: { event_name: "navigation", type: "browse card", - index: { - index_link: index + 1, - }, + index_link: index + 1, index_total: total_links, }, } diff --git a/app/views/browse/show.html.erb b/app/views/browse/show.html.erb index e9dd42b80..302ccc174 100644 --- a/app/views/browse/show.html.erb +++ b/app/views/browse/show.html.erb @@ -78,9 +78,7 @@ ga4_link: { event_name: "navigation", type: "browse card", - index: { - index_link: index + 1, - }, + index_link: index + 1, index_total: total_links, }, }, diff --git a/app/views/components/_topic_list.html.erb b/app/views/components/_topic_list.html.erb index 65ed3b3cb..af351caca 100644 --- a/app/views/components/_topic_list.html.erb +++ b/app/views/components/_topic_list.html.erb @@ -18,21 +18,19 @@ <%= tag.ul(class: ul_classes, lang: "en") do %> <% items.each_with_index do |item, index| %>
  • - <% + <% ga4_link_data = ga4_data.empty? ? {} : { ga4_link: { event_name: "navigation", type: "document list", - index: { - index_link: index + 1, - **ga4_data[:index] || {}, - }, + index_link: index + 1, # As see_more_link is not included in the items array, we need to account for it here by adding 1 to items.count # if see_more_link has been passed index_total: (see_more_link ? items.count + 1 : items.count), section: ga4_data[:section], } } + ga4_link_data[:ga4_link] = ga4_link_data[:ga4_link].merge(ga4_data) unless ga4_data.empty? %> <%= link_to( @@ -47,19 +45,17 @@ <% end %> <% if see_more_link %>
  • - <% + <% ga4_link_data = ga4_data.empty? ? {} : { ga4_link: { event_name: "navigation", type: "document list", - index: { - index_link: items.count + 1, - **ga4_data[:index] || {}, - }, + index_link: items.count + 1, index_total: items.count + 1, section: ga4_data[:section], } } + ga4_link_data[:ga4_link] = ga4_link_data[:ga4_link].merge(ga4_data) unless ga4_data.empty? %> <%= link_to( diff --git a/app/views/coronavirus_landing_page/show.html.erb b/app/views/coronavirus_landing_page/show.html.erb index 8bd253b9f..e9aa51676 100644 --- a/app/views/coronavirus_landing_page/show.html.erb +++ b/app/views/coronavirus_landing_page/show.html.erb @@ -58,16 +58,14 @@ - <%= content_tag :section, - class: "covid__topic-wrapper", - data: { - module: "ga4-link-tracker", + <%= content_tag :section, + class: "covid__topic-wrapper", + data: { + module: "ga4-link-tracker", ga4_link: { event_name: "navigation", type: "subscribe", - index: { - index_link: 1 - }, + index_link: 1, index_total: 1, section: "Footer" }, diff --git a/app/views/organisations/_latest_documents.html.erb b/app/views/organisations/_latest_documents.html.erb index 24db112a9..64238ec7d 100644 --- a/app/views/organisations/_latest_documents.html.erb +++ b/app/views/organisations/_latest_documents.html.erb @@ -23,7 +23,7 @@
    <%= render "govuk_publishing_components/components/subscription_links", @show.subscription_links %> diff --git a/app/views/second_level_browse_page/_links.html.erb b/app/views/second_level_browse_page/_links.html.erb index 45cc6104d..b37631b21 100644 --- a/app/views/second_level_browse_page/_links.html.erb +++ b/app/views/second_level_browse_page/_links.html.erb @@ -21,11 +21,9 @@ ga4_link: { event_name: "navigation", type: "browse list", - index: { - index_link: index + 1, - index_section: list_index + 1, - index_section_count: index_section_count, - }, + index_link: index + 1, + index_section: list_index + 1, + index_section_count: index_section_count, index_total: total_links, section: section, }, diff --git a/app/views/shared/_announcements.html.erb b/app/views/shared/_announcements.html.erb index 4c5c7b760..12c37e2a1 100644 --- a/app/views/shared/_announcements.html.erb +++ b/app/views/shared/_announcements.html.erb @@ -6,9 +6,9 @@ margin_bottom: 5, } %> -
    <%= render "govuk_publishing_components/components/subscription_links", { diff --git a/app/views/subtopics/_subtopic.html.erb b/app/views/subtopics/_subtopic.html.erb index 6ffc2b3b7..5b4cac42d 100644 --- a/app/views/subtopics/_subtopic.html.erb +++ b/app/views/subtopics/_subtopic.html.erb @@ -14,7 +14,7 @@
    <%= render "govuk_publishing_components/components/subscription_links", { diff --git a/app/views/taxons/_common.html.erb b/app/views/taxons/_common.html.erb index 4834e6c3e..ef3686a95 100644 --- a/app/views/taxons/_common.html.erb +++ b/app/views/taxons/_common.html.erb @@ -17,7 +17,7 @@
    <%= render "govuk_publishing_components/components/signup_link", { diff --git a/app/views/taxons/_organisation_logos_and_list.html.erb b/app/views/taxons/_organisation_logos_and_list.html.erb index 7918b6832..8afdf2983 100644 --- a/app/views/taxons/_organisation_logos_and_list.html.erb +++ b/app/views/taxons/_organisation_logos_and_list.html.erb @@ -8,11 +8,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: index_link + index, - index_section: index_section, - index_section_count: index_section_count - }, + index_link: index_link + index, + index_section: index_section, + index_section_count: index_section_count, index_total: organisations_with_logos.count, section: "Organisations" } @@ -29,7 +27,7 @@ <% if organisations_without_logos.any? %>
    " - data-ga4-link="{'index':{'index_section':<%= index_section %>,'index_section_count':<%= index_section_count %>}}" + data-ga4-link="{'index_section':<%= index_section %>,'index_section_count':<%= index_section_count %>}" > <%= render 'govuk_publishing_components/components/document_list', items: organisations_without_logos diff --git a/app/views/taxons/_organisations.html.erb b/app/views/taxons/_organisations.html.erb index fbdafa00d..f458be536 100644 --- a/app/views/taxons/_organisations.html.erb +++ b/app/views/taxons/_organisations.html.erb @@ -27,10 +27,8 @@ ga4_attributes = { event_name: "select_content", type: "organisation logo", - index: { - index_section: index_section, - index_section_count: index_section_count - }, + index_section: index_section, + index_section_count: index_section_count, section: "Organisations", } %> diff --git a/app/views/taxons/show.html.erb b/app/views/taxons/show.html.erb index e08e6e955..9669ba34d 100644 --- a/app/views/taxons/show.html.erb +++ b/app/views/taxons/show.html.erb @@ -55,10 +55,8 @@ ga4_link: { event_name: "navigation", type: "document list", - index: { - index_section: index, - index_section_count: index_section_count - }, + index_section: index, + index_section_count: index_section_count, index_total: index_total, section: title } @@ -68,12 +66,11 @@ <%= render(partial: section[:partial_template], locals: { section: section }) %> <% end %> <% if section[:see_more_link] %> - <%= ga4_data[:ga4_link][:index][:index_link] %> <% ga4_data.except!(:ga4_set_indexes) ga4_data[:ga4_link][:type] = 'see all' docs = section[:documents_with_promoted] || section[:documents] - ga4_data[:ga4_link][:index][:index_link] = docs.length + 1 + ga4_data[:ga4_link][:index_link] = docs.length + 1 %> <%= content_tag(:div, data: ga4_data) do %> <%= link_to( diff --git a/app/views/topical_events/show.html.erb b/app/views/topical_events/show.html.erb index 61b89ca6b..eedd941be 100644 --- a/app/views/topical_events/show.html.erb +++ b/app/views/topical_events/show.html.erb @@ -1,9 +1,7 @@ <% ga4_link_attributes = { event_name: "navigation", type: "subscribe", - index: { - index_link: 1 - }, + index_link: 1, index_total: 1, section: "Content" }.to_json @@ -129,7 +127,7 @@
    <%= render "govuk_publishing_components/components/subscription_links", { diff --git a/app/views/world_location_news/show.html.erb b/app/views/world_location_news/show.html.erb index c4e3730b2..04cc2f415 100644 --- a/app/views/world_location_news/show.html.erb +++ b/app/views/world_location_news/show.html.erb @@ -71,9 +71,9 @@

    <%= I18n.t("world_location_news.no_updates") %>

    <% end %> -
    <%= render "govuk_publishing_components/components/subscription_links", { diff --git a/app/views/world_wide_taxons/_email_alerts.html.erb b/app/views/world_wide_taxons/_email_alerts.html.erb index ec7e2a524..b14a6ce5e 100644 --- a/app/views/world_wide_taxons/_email_alerts.html.erb +++ b/app/views/world_wide_taxons/_email_alerts.html.erb @@ -1,7 +1,7 @@ <% if taxon_is_live?(presented_taxon) %> -
    <% if presented_taxon.renders_as_accordion? %> diff --git a/spec/components/topic_list_spec.rb b/spec/components/topic_list_spec.rb index 36ce0913f..7edc85b6f 100644 --- a/spec/components/topic_list_spec.rb +++ b/spec/components/topic_list_spec.rb @@ -39,23 +39,19 @@ def component_name it "sets GA4 data attributes correctly" do ga4_data = { - index: { - index_section: 1, - index_section_count: 1, - }, + index_section: 1, + index_section_count: 1, section: "Simple item", } expected = { "event_name": "navigation", "type": "document list", - "index": { - "index_link": 1, - "index_section": 1, - "index_section_count": 1, - }, + "index_link": 1, "index_total": 1, "section": "Simple item", + "index_section": 1, + "index_section_count": 1, }.to_json render_component(items: [simple_item], ga4_data:) @@ -73,9 +69,7 @@ def component_name expected = { "event_name": "navigation", "type": "document list", - "index": { - "index_link": 1, - }, + "index_link": 1, "index_total": 1, "section": "Simple item", }.to_json @@ -99,9 +93,7 @@ def component_name expected = { "event_name": "navigation", "type": "document list", - "index": { - "index_link": index + 1, - }, + "index_link": index + 1, "index_total": 2, "section": "Simple item", }.to_json diff --git a/spec/features/services_and_information_browsing_spec.rb b/spec/features/services_and_information_browsing_spec.rb index 9a30e9115..2409a08b0 100644 --- a/spec/features/services_and_information_browsing_spec.rb +++ b/spec/features/services_and_information_browsing_spec.rb @@ -82,9 +82,9 @@ expect(ga4_data["event_name"]).to eq "navigation" expect(ga4_data["type"]).to eq "document list" - expect(ga4_data["index"]["index_link"]).to eq 1 - expect(ga4_data["index"]["index_section"]).to eq 1 - expect(ga4_data["index"]["index_section_count"]).to eq 2 + expect(ga4_data["index_link"]).to eq 1 + expect(ga4_data["index_section"]).to eq 1 + expect(ga4_data["index_section_count"]).to eq 2 expect(ga4_data["index_total"]).to eq 5 expect(ga4_data["section"]).to eq "Environmental permits" end @@ -95,9 +95,9 @@ expect(ga4_data["event_name"]).to eq "navigation" expect(ga4_data["type"]).to eq "document list" - expect(ga4_data["index"]["index_link"]).to eq 1 - expect(ga4_data["index"]["index_section"]).to eq 2 - expect(ga4_data["index"]["index_section_count"]).to eq 2 + expect(ga4_data["index_link"]).to eq 1 + expect(ga4_data["index_section"]).to eq 2 + expect(ga4_data["index_section_count"]).to eq 2 expect(ga4_data["index_total"]).to eq 5 expect(ga4_data["section"]).to eq "Waste" end diff --git a/spec/presenters/taxon_organisations_presenter_spec.rb b/spec/presenters/taxon_organisations_presenter_spec.rb index 23a2b84d4..2b0ac29f7 100644 --- a/spec/presenters/taxon_organisations_presenter_spec.rb +++ b/spec/presenters/taxon_organisations_presenter_spec.rb @@ -38,11 +38,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 1, - index_section: 6, - index_section_count: 7, - }, + index_link: 1, + index_section: 6, + index_section_count: 7, index_total: 1, section: "Organisations", }, @@ -78,11 +76,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 1, - index_section: 6, - index_section_count: 7, - }, + index_link: 1, + index_section: 6, + index_section_count: 7, index_total: 1, section: "Organisations", }, @@ -116,11 +112,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 1, - index_section: 6, - index_section_count: 7, - }, + index_link: 1, + index_section: 6, + index_section_count: 7, index_total: 1, section: "Organisations", }, @@ -158,11 +152,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: index + 1, - index_section: 6, - index_section_count: 7, - }, + index_link: index + 1, + index_section: 6, + index_section_count: 7, index_total: 5, section: "Organisations", }, @@ -198,11 +190,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: index + 1, - index_section: 6, - index_section_count: 7, - }, + index_link: index + 1, + index_section: 6, + index_section_count: 7, index_total: 5, section: "Organisations", }, @@ -242,11 +232,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: index + 1, - index_section: 6, - index_section_count: 7, - }, + index_link: index + 1, + index_section: 6, + index_section_count: 7, index_total: 4, section: "Organisations", }, @@ -269,11 +257,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 4, - index_section: 6, - index_section_count: 7, - }, + index_link: 4, + index_section: 6, + index_section_count: 7, index_total: 4, section: "Organisations", }, @@ -313,11 +299,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 6, - index_section: 6, - index_section_count: 7, - }, + index_link: 6, + index_section: 6, + index_section_count: 7, index_total: 6, section: "Organisations", }, @@ -353,11 +337,9 @@ ga4_link: { event_name: "navigation", type: "organisation logo", - index: { - index_link: 6, - index_section: 6, - index_section_count: 7, - }, + index_link: 6, + index_section: 6, + index_section_count: 7, index_total: 6, section: "Organisations", },