Skip to content

Commit ca92c35

Browse files
committed
Add tests and tracking for guidance / policy sections
Wrapping the taxonomy block in a track-click data module activates link tracking. I've checked this in GA debug and it seems to do the trick. Also switching the guard clauses on each section from any? to present? as this checks whether hashes/arrays are nil or empty. (The services data can be nil)
1 parent 0dddc9a commit ca92c35

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

app/presenters/supergroups/supergroup.rb

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module Supergroups
22
class Supergroup
33
private
44

5-
def format_document_data(documents, include_timestamp: true)
6-
documents&.map do |document|
5+
def format_document_data(documents, data_category: nil, include_timestamp: true)
6+
documents.each.with_index(1).map do |document, index|
77
data = {
88
link: {
99
text: document["title"],
10-
path: document["link"]
10+
path: document["link"],
11+
data_attributes: data_attributes(document["link"], document["title"], index)
1112
},
1213
metadata: {
1314
document_type: document["content_store_document_type"].humanize
@@ -18,8 +19,27 @@ def format_document_data(documents, include_timestamp: true)
1819
data[:metadata][:public_updated_at] = Date.parse(document["public_timestamp"])
1920
end
2021

22+
if data_category.present?
23+
data[:link][:data_attributes][:track_category] = data_module_label + data_category
24+
end
25+
2126
data
2227
end
2328
end
29+
30+
def data_attributes(base_path, link_text, index)
31+
{
32+
track_category: data_module_label + "DocumentListClicked",
33+
track_action: index,
34+
track_label: base_path,
35+
track_options: {
36+
dimension29: link_text
37+
}
38+
}
39+
end
40+
41+
def data_module_label
42+
self.class.name.demodulize.camelize(:lower)
43+
end
2444
end
2545
end

app/views/shared/_taxonomy_navigation.html.erb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<% if taxonomy_navigation.values().flatten.any? %>
2-
<div class="taxonomy-navigation">
2+
<div class="taxonomy-navigation" data-module="track-click">
33
<h2>More in
44
<% tagged_taxons.each do |tagged_taxon| %>
55
<a class="taxonomy-navigation__title-link" href="<%= tagged_taxon[:taxon_link] %>"><%= tagged_taxon[:taxon_name] %></a>
66
<% end %>
77
</h2>
88

9-
<% if taxonomy_navigation[:services].any? %>
9+
<% if taxonomy_navigation[:services].present? %>
1010
<div class="taxonomy-navigation__section">
1111
<%= render "govuk_publishing_components/components/heading", {
1212
text: t('supergroups.services'),
@@ -26,7 +26,7 @@
2626
</div>
2727
<% end %>
2828

29-
<% if taxonomy_navigation[:guidance_and_regulation].any? %>
29+
<% if taxonomy_navigation[:guidance_and_regulation].present? %>
3030
<div class="taxonomy-navigation__section">
3131
<%= render "govuk_publishing_components/components/heading", {
3232
text: t('supergroups.guidance_and_regulation'),
@@ -41,7 +41,7 @@
4141
</div>
4242
<% end %>
4343

44-
<% if taxonomy_navigation[:policy_and_engagement].any? %>
44+
<% if taxonomy_navigation[:policy_and_engagement].present? %>
4545
<div class="taxonomy-navigation__section">
4646
<%= render "govuk_publishing_components/components/heading", {
4747
text: t('supergroups.policy_and_engagement'),

test/integration/content_pages_navigation_test.rb

+70
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest
5757

5858
test "shows the Services section title and documents with tracking" do
5959
stub_rummager
60+
stub_empty_guidance
61+
stub_empty_policies
6062
setup_variant_b
6163

6264
taxons = SINGLE_TAXON
@@ -85,6 +87,74 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest
8587
refute page.has_css?('h3', text: "Services")
8688
end
8789

90+
test "shows the Policy section title and documents with tracking" do
91+
stub_rummager
92+
stub_empty_services
93+
stub_empty_guidance
94+
setup_variant_b
95+
96+
taxons = SINGLE_TAXON
97+
98+
setup_and_visit_content_item_with_taxons('guide', taxons)
99+
100+
assert page.has_css?('h3', text: "Policy and engagement")
101+
102+
assert page.has_css?('.gem-c-document-list__item a[data-track-category="policyAndEngagementDocumentListClicked"]', text: 'Free school meals form')
103+
assert page.has_css?('.gem-c-document-list__item a[data-track-action="1"]', text: 'Free school meals form')
104+
assert page.has_css?('.gem-c-document-list__item a[data-track-label="/government/publications/meals"]', text: 'Free school meals form')
105+
end
106+
107+
test "does not show the Policy section if there is no tagged content" do
108+
stub_empty_rummager
109+
setup_variant_b
110+
111+
taxons = SINGLE_TAXON
112+
113+
setup_and_visit_content_item_with_taxons('guide', taxons)
114+
115+
refute page.has_css?('h3', text: "Policy and engagement")
116+
end
117+
118+
test "shows the Guidance section title and documents with tracking" do
119+
stub_rummager
120+
stub_empty_services
121+
stub_empty_policies
122+
setup_variant_b
123+
124+
taxons = SINGLE_TAXON
125+
126+
setup_and_visit_content_item_with_taxons('guide', taxons)
127+
128+
assert page.has_css?('h3', text: "Guidance and regulation")
129+
130+
assert page.has_css?('.gem-c-document-list__item a[data-track-category="guidanceAndRegulationDocumentListClicked"]', text: 'Free school meals form')
131+
assert page.has_css?('.gem-c-document-list__item a[data-track-action="1"]', text: 'Free school meals form')
132+
assert page.has_css?('.gem-c-document-list__item a[data-track-label="/government/publications/meals"]', text: 'Free school meals form')
133+
end
134+
135+
test "does not show the Guidance section if there is no tagged content" do
136+
stub_empty_rummager
137+
setup_variant_b
138+
139+
taxons = SINGLE_TAXON
140+
141+
setup_and_visit_content_item_with_taxons('guide', taxons)
142+
143+
refute page.has_css?('h3', text: "Guidance and regulation")
144+
end
145+
146+
def stub_empty_services
147+
Supergroups::Services.any_instance.stubs(:all_services).returns({})
148+
end
149+
150+
def stub_empty_guidance
151+
Supergroups::GuidanceAndRegulation.any_instance.stubs(:tagged_content).returns([])
152+
end
153+
154+
def stub_empty_policies
155+
Supergroups::PolicyAndEngagement.any_instance.stubs(:tagged_content).returns([])
156+
end
157+
88158
def setup_variant_a
89159
ContentItemsController.any_instance.stubs(:show_new_navigation?).returns(false)
90160
end

0 commit comments

Comments
 (0)