Skip to content

Commit

Permalink
Add signup links index to data_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesCGDS authored and andysellick committed Sep 11, 2023
1 parent 3153387 commit 7b8ab6f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 16 deletions.
55 changes: 50 additions & 5 deletions app/presenters/signup_links_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,62 @@ 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:,
email_signup_link: email_signup_link.presence,
}.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
if email_signup_link
data_attributes[:email_signup_link_data_attributes] = {}
data_attributes[:email_signup_link_data_attributes][:ga4_index] = {
index_link: email_index_link,
index_total: total_links,
}
end

if feed_link
data_attributes[:feed_link_data_attributes] = {}
data_attributes[:feed_link_data_attributes][:ga4_index] = {
index_link: feed_index_link,
index_total: total_links,
}
end

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
Expand Down
10 changes: 4 additions & 6 deletions app/views/finders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down Expand Up @@ -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] %>
</div>
</div>
<div id="js-facet-tag-wrapper" class="facet-tags__container" aria-live="assertive">
Expand All @@ -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] %>
</div>
</div>
</div>
Expand Down
114 changes: 109 additions & 5 deletions spec/presenters/signup_link_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
let(:facet_values) { [] }

it "returns the finder URL appended with /email-signup" do
expect(subject.signup_links[:email_signup_link]).to eql("/email_signup")
expect(subject.signup_links[0][:email_signup_link]).to eql("/email_signup")
end
end

Expand All @@ -93,7 +93,15 @@
end

it "returns the finder URL appended with permitted query params" do
expect(subject.signup_links[:email_signup_link]).to eql("/mosw-reports/email-signup?topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[0][:email_signup_link]).to eql("/mosw-reports/email-signup?topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[0][:email_signup_link_data_attributes][:ga4_index]).to eql({ index_link: 1, index_total: 4 })
expect(subject.signup_links[0][:feed_link]).to eql("/mosw-reports.atom?topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[0][:feed_link_data_attributes][:ga4_index]).to eql({ index_link: 2, index_total: 4 })

expect(subject.signup_links[1][:email_signup_link]).to eql("/mosw-reports/email-signup?topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[1][:email_signup_link_data_attributes][:ga4_index]).to eql({ index_link: 3, index_total: 4 })
expect(subject.signup_links[1][:feed_link]).to eql("/mosw-reports.atom?topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[1][:feed_link_data_attributes][:ga4_index]).to eql({ index_link: 4, index_total: 4 })
end
end
end
Expand All @@ -104,7 +112,7 @@
[]
end
it "returns the finder URL appended with .atom" do
expect(subject.signup_links[:feed_link]).to eql("/mosw-reports.atom")
expect(subject.signup_links[0][:feed_link]).to eql("/mosw-reports.atom")
end
end

Expand All @@ -118,7 +126,7 @@
end

it "returns the finder URL appended with permitted query params" do
expect(subject.signup_links[:feed_link]).to eql("/mosw-reports.atom?keywords=micropig&topic%5B%5D=hidden_facet_content_id")
expect(subject.signup_links[0][:feed_link]).to eql("/mosw-reports.atom?keywords=micropig&topic%5B%5D=hidden_facet_content_id")
end
end

Expand All @@ -132,9 +140,105 @@
end

it "returns nil" do
expect(subject.signup_links[:feed_link]).to be nil
expect(subject.signup_links[0][:feed_link]).to be nil
end
end
end
end

describe "only a feed link" do
let(:content_item) do
content_item_hash = {
content_id: "content_id",
base_path: "/mosw-reports",
title: "A finder",
name: "A finder",
links: {},
signup_link: false,
email_alert_signup: false,
details: {
show_summaries: true,
document_noun: "case",
sort: [
{
"name" => "Most viewed",
"key" => "-popularity",
},
{
"name" => "Relevance",
"key" => "-relevance",
},
{
"name" => "Updated (newest)",
"key" => "-public_timestamp",
"default" => true,
},
],
},
}
ContentItem.new(content_item_hash.deep_stringify_keys)
end

let(:email_signup_hash) do
{}
end

it "returns data for the feed link only" do
expect(subject.signup_links[0][:feed_link]).to eql("/mosw-reports.atom")
expect(subject.signup_links[0][:feed_link_data_attributes][:ga4_index]).to eql({ index_link: 1, index_total: 2 })
expect(subject.signup_links[0][:email_signup_link]).to eql(nil)
expect(subject.signup_links[0][:email_signup_link_data_attributes]).to eql(nil)

expect(subject.signup_links[1][:feed_link]).to eql("/mosw-reports.atom")
expect(subject.signup_links[1][:feed_link_data_attributes][:ga4_index]).to eql({ index_link: 2, index_total: 2 })
expect(subject.signup_links[1][:email_signup_link]).to eql(nil)
expect(subject.signup_links[1][:email_signup_link_data_attributes]).to eql(nil)
end
end

describe "only an email link" do
let(:content_item) do
content_item_hash = {
content_id: "content_id",
base_path: "/find-licences",
title: "A finder",
name: "A finder",
links: {
email_alert_signup: Array.wrap(email_signup_hash),
},
details: {
show_summaries: true,
document_noun: "case",
sort: [
{
"name" => "Most viewed",
"key" => "-popularity",
},
{
"name" => "Relevance",
"key" => "-relevance",
},
{
"name" => "Updated (newest)",
"key" => "-public_timestamp",
"default" => true,
},
],
},
}
ContentItem.new(content_item_hash.deep_stringify_keys)
end

it "returns data for the email signup link only" do
expect(subject.signup_links[0][:feed_link]).to eql(nil)
expect(subject.signup_links[0][:feed_link_data_attributes]).to eql(nil)
expect(subject.signup_links[0][:email_signup_link]).to eql("/email_signup")
expect(subject.signup_links[0][:email_signup_link_data_attributes][:ga4_index]).to eql({ index_link: 1, index_total: 2 })

expect(subject.signup_links[1][:feed_link]).to eql(nil)
expect(subject.signup_links[1][:feed_link_data_attributes]).to eql(nil)
expect(subject.signup_links[1][:email_signup_link]).to eql("/email_signup")
expect(subject.signup_links[1][:email_signup_link_data_attributes][:ga4_index]).to eql({ index_link: 2, index_total: 2 })
end
end
end

0 comments on commit 7b8ab6f

Please sign in to comment.