Skip to content

Commit

Permalink
Merge pull request #3417 from alphagov/flatten-ga4-attributes
Browse files Browse the repository at this point in the history
Flatten GA4 attributes
  • Loading branch information
andysellick authored Oct 27, 2023
2 parents ff32b02 + 96ab6d8 commit d8a5671
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 139 deletions.
5 changes: 2 additions & 3 deletions app/helpers/topic_list_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

{
Expand Down
8 changes: 3 additions & 5 deletions app/presenters/taxon_organisations_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
8 changes: 3 additions & 5 deletions app/presenters/taxon_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
4 changes: 1 addition & 3 deletions app/views/browse/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
4 changes: 1 addition & 3 deletions app/views/browse/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
ga4_link: {
event_name: "navigation",
type: "browse card",
index: {
index_link: index + 1,
},
index_link: index + 1,
index_total: total_links,
},
},
Expand Down
16 changes: 6 additions & 10 deletions app/views/components/_topic_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@
<%= tag.ul(class: ul_classes, lang: "en") do %>
<% items.each_with_index do |item, index| %>
<li class="app-c-topic-list__item">
<%
<%
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(
Expand All @@ -47,19 +45,17 @@
<% end %>
<% if see_more_link %>
<li class="app-c-topic-list__item">
<%
<%
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(
Expand Down
12 changes: 5 additions & 7 deletions app/views/coronavirus_landing_page/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@
</div>
</section>

<%= 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"
},
Expand Down
2 changes: 1 addition & 1 deletion app/views/organisations/_latest_documents.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Content" }'
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Content" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", @show.subscription_links %>
Expand Down
8 changes: 3 additions & 5 deletions app/views/second_level_browse_page/_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
6 changes: 3 additions & 3 deletions app/views/shared/_announcements.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
margin_bottom: 5,
} %>

<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Content" }'
<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Content" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", {
Expand Down
2 changes: 1 addition & 1 deletion app/views/subtopics/_subtopic.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="govuk-grid-column-one-third govuk-!-margin-top-8 govuk-!-margin-bottom-3">
<span
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Top" }'
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Top" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", {
Expand Down
2 changes: 1 addition & 1 deletion app/views/taxons/_common.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div
class="full-page-width-wrapper"
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Top" }'
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Top" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/signup_link", {
Expand Down
10 changes: 4 additions & 6 deletions app/views/taxons/_organisation_logos_and_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -29,7 +27,7 @@
<% if organisations_without_logos.any? %>
<div
data-module="ga4-link-tracker <%= "gem-track-click" if track_click?(organisations_without_logos) %>"
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
Expand Down
6 changes: 2 additions & 4 deletions app/views/taxons/_organisations.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
%>
Expand Down
9 changes: 3 additions & 6 deletions app/views/taxons/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions app/views/topical_events/show.html.erb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -129,7 +127,7 @@

<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Content" }'
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Content" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", {
Expand Down
6 changes: 3 additions & 3 deletions app/views/world_location_news/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
<p class="govuk-body"><%= I18n.t("world_location_news.no_updates") %></p>
<% end %>

<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Content" }'
<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Content" }'
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", {
Expand Down
6 changes: 3 additions & 3 deletions app/views/world_wide_taxons/_email_alerts.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% if taxon_is_live?(presented_taxon) %>
<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index": { "index_link": 1 }, "index_total": 1, "section": "Top" }'
<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Top" }'
data-ga4-track-links-only
>
<% if presented_taxon.renders_as_accordion? %>
Expand Down
22 changes: 7 additions & 15 deletions spec/components/topic_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/features/services_and_information_browsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit d8a5671

Please sign in to comment.