From ff5ab67669eedf8d5d137df680a8cbb5118ffae6 Mon Sep 17 00:00:00 2001 From: James Chan Date: Fri, 25 Aug 2023 16:17:20 +0100 Subject: [PATCH] Add signup links index to data_attributes --- app/presenters/signup_links_presenter.rb | 52 +++++++++++++++++++++--- app/views/finders/show.html.erb | 10 ++--- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/app/presenters/signup_links_presenter.rb b/app/presenters/signup_links_presenter.rb index 81635bfda2..1ce9733fe8 100644 --- a/app/presenters/signup_links_presenter.rb +++ b/app/presenters/signup_links_presenter.rb @@ -6,17 +6,57 @@ def initialize(content_item, facets, keywords) end def signup_links - { - feed_link:, + # if there are 4 links, we start from 2 + # if there are 2 links, we start from 1 + [ + get_signup_link(0), + get_signup_link(count_signup_links / 2), + ] + end + +private + + attr_reader :content_item, :facets, :keywords + + def get_signup_link(pos) + total_links = count_signup_links + + data_attributes = { hide_heading: true, small_form: true, + feed_link:, + feed_link_data_attributes: {}, email_signup_link: email_signup_link.presence, + email_signup_link_data_attributes: {}, }.compact - end -private + if email_signup_link && feed_link + email_index_link = pos + 1 + feed_index_link = pos + 2 + elsif email_signup_link + email_index_link = pos + 1 + elsif feed_link + feed_index_link = pos + 1 + end - attr_reader :content_item, :facets, :keywords + data_attributes[:email_signup_link_data_attributes][:ga4_index] = { + index_link: email_index_link, + index_total: total_links + } + data_attributes[:feed_link_data_attributes][:ga4_index] = { + index_link: feed_index_link, + index_total: total_links + } + + data_attributes + end + + def count_signup_links + total = 0 + total += 1 if feed_link + total += 1 if email_signup_link + total * 2 + end def email_signup_link signup_link = content_item.signup_link @@ -43,4 +83,4 @@ def query_string(params) query_string = params.compact.to_query query_string.blank? ? query_string : "?#{query_string}" end -end +end \ No newline at end of file diff --git a/app/views/finders/show.html.erb b/app/views/finders/show.html.erb index 771ceca3cb..6fa92cdb6b 100644 --- a/app/views/finders/show.html.erb +++ b/app/views/finders/show.html.erb @@ -4,8 +4,8 @@ <% content_for :title, content_item.title %> <% end %> <% content_for :head do %> - <% if signup_links[:feed_link] %> - <%= auto_discovery_link_tag(:atom, signup_links[:feed_link]) %> + <% if signup_links[0][:feed_link] %> + <%= auto_discovery_link_tag(:atom, signup_links[0][:feed_link]) %> <% end %> <%= render 'finder_meta', content_item: content_item %> <% end %> @@ -82,9 +82,8 @@ class="govuk-grid-column-one-half govuk-!-text-align-right subscription-links subscription-links--desktop" data-module="ga4-link-tracker" data-ga4-track-links-only - data-ga4-set-indexes data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "section": "Top" }'> - <%= render "govuk_publishing_components/components/subscription_links", signup_links %> + <%= render "govuk_publishing_components/components/subscription_links", signup_links[0] %>
@@ -111,9 +110,8 @@ id="subscription-links-footer" data-module="ga4-link-tracker" data-ga4-track-links-only - data-ga4-set-indexes data-ga4-link='{ "event_name": "navigation", "type": "subscribe", "section": "Footer" }'> - <%= render "govuk_publishing_components/components/subscription_links", signup_links %> + <%= render "govuk_publishing_components/components/subscription_links", signup_links[1] %>