Skip to content

Commit

Permalink
Add GA4 tracking to organisation pages
Browse files Browse the repository at this point in the history
Adds or updates tracking to:
- image cards
- document list/topic list components
- 'see all latest documents' and 'Get emails' link
- stops redacting email links in contact sections
- adds 'section' to share links
  • Loading branch information
AshGDS committed Feb 22, 2024
1 parent 17788fd commit 04cad4e
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 28 deletions.
1 change: 1 addition & 0 deletions app/presenters/organisations/people_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def all_people
{
type: person_type,
title: I18n.t("organisations.people.#{person_type}"),
ga4_english_title: I18n.t("organisations.people.#{person_type}", locale: :en),
lang: t_fallback("organisations.people.#{person_type}"),
people: people.map { |person| formatted_person_data(person, person_type) },
}
Expand Down
4 changes: 3 additions & 1 deletion app/views/components/_topic_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
margin_bottom ||= false
ga4_data ||= {}

ul_data_attributes = { "module": "ga4-link-tracker", "ga4-track-links-only": "" } if !ga4_data.empty?

brand ||= false
brand_helper = GovukPublishingComponents::AppHelpers::BrandHelper.new(brand)

Expand All @@ -15,7 +17,7 @@
ul_classes << brand_helper.brand_class if brand
%>
<% if items.any? %>
<%= tag.ul(class: ul_classes, lang: "en") do %>
<%= tag.ul(class: ul_classes, lang: "en", data: ul_data_attributes) do %>
<% items.each_with_index do |item, index| %>
<li class="app-c-topic-list__item">
<%
Expand Down
13 changes: 12 additions & 1 deletion app/views/organisations/_contacts.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<% if contacts.present? %>
<%
ga4_section ||= nil
ga4_email_link_data = { event_name: "navigation", type: "email" }
ga4_email_link_data[:section] = ga4_section if ga4_section
%>
<% contacts.each do |contact| %>
<div class="organisation__contact-section <%= "organisation__contact-section--border-top" if local_assigns[:border] %>" <%= "lang=#{contact[:locale]}" if contact[:locale] %>>
<%= render "govuk_publishing_components/components/heading", {
Expand Down Expand Up @@ -28,7 +33,13 @@
<% end %>
<% contact[:email_addresses].each do |email| %>
<p><%= email.html_safe %></p>
<p
data-module="ga4-link-tracker"
data-ga4-track-links-only
data-ga4-do-not-redact
data-ga4-link="<%= ga4_email_link_data.to_json %>">
<%= email.html_safe %>
</p>
<% end %>
</div>
<% end %>
Expand Down
8 changes: 4 additions & 4 deletions app/views/organisations/_corporate_information.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% end %>
<% if @show.corporate_information[:corporate_information_links][:items].any? && !@organisation.is_no_10? %>
<div class="<%= "organisation__section-border-top" if @organisation.is_promotional_org? %>">
<%= render "components/topic_list", @show.corporate_information[:corporate_information_links] %>
<%= render "components/topic_list", @show.corporate_information[:corporate_information_links].merge({ ga4_data: { section: t('organisations.corporate_information', locale: :en) } }) %>
</div>
<% end %>
Expand All @@ -22,7 +22,7 @@
font_size: 24,
lang: t_fallback('organisations.jobs_contracts')
} %>
<%= render "components/topic_list", @show.corporate_information[:job_links] %>
<%= render "components/topic_list", @show.corporate_information[:job_links].merge({ ga4_data: { section: t('organisations.jobs_contracts', locale: :en) } }) %>
<% end %>
<% if @organisation.secondary_corporate_information && !@organisation.is_no_10? %>
Expand All @@ -39,11 +39,11 @@
font_size: 19,
padding: true,
border_top: 5,
brand: @organisation.brand,
brand: @organisation.brand,
lang: t_fallback('organisations.follow_us')
} %>
<div lang="en">
<%= render "govuk_publishing_components/components/share_links", @what_we_do.share_links %>
<%= render "govuk_publishing_components/components/share_links", @what_we_do.share_links.merge({ ga4_extra_data: { section: t('organisations.follow_us', locale: :en) }}) %>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/organisations/_courts_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

<div class="govuk-grid-column-one-third">
<%= render "govuk_publishing_components/components/translation_nav", @header.translation_links %>
<%= render "components/topic_list", @header.ordered_featured_links %>
<%= render "components/topic_list", @header.ordered_featured_links.merge({ ga4_data: { section: @header.org.title } }) %>
</div>
</div>
13 changes: 11 additions & 2 deletions app/views/organisations/_featured_news.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<% if @documents.has_featured_news? %>
<%
ga4_image_card_json = {
"event_name": "navigation",
"type": "image card",
"section": t('shared.featured', locale: :en)
}.to_json
%>
<section class="brand--<%= @organisation.brand %> brand__border-color organisation__brand-border-top organisation__section-wrap organisation__margin-bottom" id="featured-documents">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
Expand All @@ -12,11 +19,13 @@
</div>

<% if @organisation.is_news_organisation? && !@organisation.is_no_10? %>
<%= render "govuk_publishing_components/components/image_card", @documents.first_featured_news %>
<div data-module="ga4-link-tracker" data-ga4-link="<%= ga4_image_card_json %>">
<%= render "govuk_publishing_components/components/image_card", @documents.first_featured_news %>
</div>
<% end %>
<% @documents.remaining_featured_news.in_groups_of(3, false) do |news| %>
<div class="govuk-grid-row">
<div class="govuk-grid-row" data-module="ga4-link-tracker" data-ga4-track-links-only data-ga4-link="<%= ga4_image_card_json %>">
<% news.each do |news_item| %>
<% classes = %w[] %>
<% classes << "govuk-grid-column-one-third" unless news_item[:large] %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/organisations/_freedom_of_information.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<% end %>
<% if @contacts.has_foi_contacts? %>
<%= render partial: 'contacts', locals: { contacts: @contacts.foi_contacts } %>
<%= render partial: 'contacts', locals: { contacts: @contacts.foi_contacts, ga4_section: t('organisations.foi.make_an_foi_request', locale: :en) } %>
<% end %>
<% end %>
</section>
2 changes: 1 addition & 1 deletion app/views/organisations/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<% if @organisation.is_live? %>
<div class="<%= @header.link_wrapper_class %> organisation__margin-bottom">
<%= render "govuk_publishing_components/components/translation_nav", @header.translation_links %>
<%= render "components/topic_list", @header.ordered_featured_links %>
<%= render "components/topic_list", @header.ordered_featured_links.merge({ ga4_data: { section: @header.org.title } }) %>
</div>
<% end %>
</div>
Expand Down
12 changes: 8 additions & 4 deletions app/views/organisations/_latest_documents.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/document_list", @documents.latest_documents %>

<% ga4_section = t('organisations.latest_from', title: @show.prefixed_title, locale: :en) %>
<%= render "govuk_publishing_components/components/document_list", @documents.latest_documents.merge({ ga4_extra_data: { section: ga4_section }}) %>
<p class="organisation__margin-bottom brand--<%= @organisation.brand %>">
<a href="<%= "/search/all?organisations[]=#{@organisation.slug}&order=updated-newest&parent=#{@organisation.slug}" %>" class="govuk-link brand__color" <%= t_lang('organisations.see_all_latest_documents') %>>
<a
href="<%= "/search/all?organisations[]=#{@organisation.slug}&order=updated-newest&parent=#{@organisation.slug}" %>"
class="govuk-link brand__color" <%= t_lang('organisations.see_all_latest_documents') %>
data-module="ga4-link-tracker"
data-ga4-link="<%= { event_name: "navigation", type: "see all", section: ga4_section }.to_json %>">
<%= t('organisations.see_all_latest_documents') %>
</a>
</p>

<div
data-module="ga4-link-tracker"
data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": "Content" }'
data-ga4-link="<%= { "event_name": "navigation", "type": "subscribe", "index_link": 1, "index_total": 1, "section": ga4_section }.to_json %>"
data-ga4-track-links-only
>
<%= render "govuk_publishing_components/components/subscription_links", @show.subscription_links %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
heading_level: 3,
lang: supergroup[:lang]
} %>
<%= render "govuk_publishing_components/components/document_list", supergroup[:documents] %>
<%= render "govuk_publishing_components/components/document_list", supergroup[:documents].merge({ ga4_extra_data: { section: t('organisations.document_types.documents', locale: :en)}}) %>
<a href="<%= supergroup[:finder_link][:path] %>" class="govuk-link brand__color" <%= supergroup[:finder_link][:lang] %>>
<%= supergroup[:finder_link][:text] %>
</a>
Expand Down
2 changes: 1 addition & 1 deletion app/views/organisations/_org_contacts.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
lang: t_fallback("organisations.contact.contact_organisation", { organisation: @show.acronym })
} %>
<%= render partial: 'contacts', locals: { contacts: contacts.contacts, border: true } %>
<%= render partial: 'contacts', locals: { contacts: contacts.contacts, border: true, ga4_section: t('organisations.contact.contact_organisation', organisation: @show.acronym, locale: :en) } %>
</section>
<% end %>
9 changes: 8 additions & 1 deletion app/views/organisations/_promotional_features.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<% if @documents.has_promotional_features? %>
<section class="organisation__margin-bottom organisation__section-wrap">
<% @documents.promotional_features.each do |feature| %>
<div class="organisation__promotion">
<%
ga4_image_card_json = {
"event_name": "navigation",
"type": "image card",
"section": feature[:title]
}.to_json
%>
<div class="organisation__promotion" data-module="ga4-link-tracker" data-ga4-track-links-only data-ga4-link="<%= ga4_image_card_json %>">
<%= render "govuk_publishing_components/components/heading", {
text: feature[:title],
heading_level: 2,
Expand Down
11 changes: 9 additions & 2 deletions app/views/organisations/_related_people.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<% if people.any? %>
<section class="brand--<%= brand %> brand__border-color organisation__brand-border-top organisation__margin-bottom">
<%
ga4_image_card_json = {
"event_name": "navigation",
"type": "image card",
"section": ga4_english_title
}.to_json
%>
<section class="brand--<%= brand %> brand__border-color organisation__brand-border-top organisation__margin-bottom" data-people-id="<%= title.downcase.split(' ').join('-')%>">
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/heading", {
Expand All @@ -12,7 +19,7 @@
</div>

<% people.in_groups_of(4, false) do |people| %>
<div class="govuk-grid-row">
<div class="govuk-grid-row" data-module="ga4-link-tracker" data-ga4-track-links-only data-ga4-link="<%= ga4_image_card_json %>">
<% people.each do |person| %>
<div class="govuk-grid-column-one-quarter">
<%= render "govuk_publishing_components/components/image_card", person %>
Expand Down
1 change: 1 addition & 0 deletions app/views/organisations/_standard_org_docs_people.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= render partial: 'related_people', locals: {
people: people_data[:people],
title: people_data[:title],
ga4_english_title: people_data[:ga4_english_title],
lang: people_data[:lang],
brand: @organisation.brand
} %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
padding: true,
margin_bottom: 3
} %>
<%= render "components/topic_list", @show.high_profile_groups %>
<%= render "components/topic_list", @show.high_profile_groups.merge({ ga4_data: { section: @show.high_profile_groups[:title] } }) %>
</section>
<% end %>
2 changes: 1 addition & 1 deletion app/views/organisations/_what_we_do.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
lang: t_fallback('organisations.follow_us')
} %>
<div lang="en">
<%= render "govuk_publishing_components/components/share_links", @what_we_do.share_links %>
<%= render "govuk_publishing_components/components/share_links", @what_we_do.share_links.merge({ ga4_extra_data: { section: t('organisations.what_we_do', locale: :en) }}) %>
</div>
</div>
<% end %>
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 @@ -31,6 +31,6 @@
</div>
</header>

<div class="browse-container full-width" data-module="gem-track-click ga4-link-tracker">
<div class="browse-container full-width" data-module="gem-track-click">
<%= yield %>
</div>
2 changes: 1 addition & 1 deletion app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<%= render "govuk_publishing_components/components/title", title: title_with_suffix %>
</header>

<div class="browse-container full-width topics-page" data-module="gem-track-click ga4-link-tracker">
<div class="browse-container full-width topics-page" data-module="gem-track-click">
<nav class="topics">
<% tracking_category = topic.base_path == '/topic' ? 'navTopicLinkClicked' : 'navSubtopicLinkClicked' %>
<%= render 'components/topic_list', topic_list_params(topic.children, category: tracking_category, list_title: title_with_suffix) %>
Expand Down
3 changes: 2 additions & 1 deletion spec/components/topic_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def component_name
}.to_json

render_component(items: [simple_item], ga4_data:)

assert_select "ul[data-module=ga4-link-tracker]"
assert_select "ul[data-ga4-track-links-only]"
assert_select ".govuk-link" do |link|
expect(link.attr("data-ga4-link").to_s).to eq expected
end
Expand Down
49 changes: 49 additions & 0 deletions spec/features/organisation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,53 @@
org_schema = schemas.detect { |schema| schema["@type"] == "GovernmentOrganization" }
expect("Student Loans Company").to eq(org_schema["name"])
end

it "has GA4 tracking on image cards" do
visit "/government/organisations/prime-ministers-office-10-downing-street"
test_ga4_image_cards("#featured-documents div")
test_ga4_image_cards("div.organisation__promotion")

visit "/government/organisations/attorney-generals-office"
test_ga4_image_cards("#featured-documents div")
test_ga4_image_cards("[data-people-id=our-ministers] div")
test_ga4_image_cards("[data-people-id=our-management] div")

visit "/government/organisations/office-of-the-secretary-of-state-for-wales.cy"
test_ga4_image_cards("#featured-documents div", "Featured")
test_ga4_image_cards("[data-people-id=ein-gweinidogion] div", "Our ministers")
test_ga4_image_cards("[data-people-id=ein-rheolwyr] div", "Our management")
end

it "has GA4 tracking on the see all latest documents link" do
visit "/government/organisations/prime-ministers-office-10-downing-street"
test_ga4_see_all_link

visit "/government/organisations/attorney-generals-office"
test_ga4_see_all_link

visit "/government/organisations/office-of-the-secretary-of-state-for-wales.cy"
test_ga4_see_all_link("Latest from the Office of the Secretary of State for Wales", "Gweld yr holl ddogfennau diweddaraf")
end

it "has GA4 tracking on the get emails link" do
visit "/government/organisations/prime-ministers-office-10-downing-street"
test_ga4_get_emails_link

visit "/government/organisations/attorney-generals-office"
test_ga4_get_emails_link

visit "/government/organisations/office-of-the-secretary-of-state-for-wales.cy"
test_ga4_get_emails_link("Latest from the Office of the Secretary of State for Wales")
end

it "has GA4 tracking on email links" do
visit "/government/organisations/prime-ministers-office-10-downing-street"
test_ga4_email_links("#org-contacts")

visit "/government/organisations/attorney-generals-office"
test_ga4_email_links("#org-contacts")

visit "/government/organisations/office-of-the-secretary-of-state-for-wales.cy"
test_ga4_email_links("#freedom-of-information", "Make an FOI request")
end
end
2 changes: 1 addition & 1 deletion spec/features/subtopic_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def oil_and_gas_subtopic_item(subtopic_slug, params = {})

visit "/topic/oil-and-gas/offshore"

expect(page).to have_selector('.browse-container[data-module="gem-track-click ga4-link-tracker"]')
expect(page).to have_selector('.browse-container[data-module="gem-track-click"]')

oil_rig_safety_requirements = page.find(
"a",
Expand Down
4 changes: 2 additions & 2 deletions spec/features/topic_browsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def oil_and_gas_topic_item(params = {})

visit "/topic"

expect(page).to have_selector('.topics-page[data-module="gem-track-click ga4-link-tracker"]')
expect(page).to have_selector('.topics-page[data-module="gem-track-click"]')

topic_link = page.find("a", text: "Oil and Gas")

Expand Down Expand Up @@ -135,7 +135,7 @@ def oil_and_gas_topic_item(params = {})

visit "/topic/oil-and-gas"

expect(page).to have_selector('.topics-page[data-module="gem-track-click ga4-link-tracker"]')
expect(page).to have_selector('.topics-page[data-module="gem-track-click"]')

within ".app-c-topic-list" do
within "li:nth-child(1)" do
Expand Down
6 changes: 6 additions & 0 deletions spec/presenters/organisations/people_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,42 @@
{
type: :ministers,
title: "Our ministers",
ga4_english_title: "Our ministers",
people: [],
lang: false,
},
{
type: :military_personnel,
title: "Our senior military officials",
ga4_english_title: "Our senior military officials",
people: [],
lang: false,
},
{
type: :board_members,
title: "Our management",
ga4_english_title: "Our management",
people: [],
lang: false,
},
{
type: :traffic_commissioners,
title: "Traffic commissioners",
ga4_english_title: "Traffic commissioners",
people: [],
lang: false,
},
{
type: :special_representatives,
title: "Special representatives",
ga4_english_title: "Special representatives",
people: [],
lang: false,
},
{
type: :chief_professional_officers,
title: "Chief professional officers",
ga4_english_title: "Chief professional officers",
people: [],
lang: false,
},
Expand Down
Loading

0 comments on commit 04cad4e

Please sign in to comment.