Skip to content

Commit

Permalink
Merge pull request #2123 from alphagov/brexit_hub_analytics
Browse files Browse the repository at this point in the history
Add some analytics to the brexit child taxon pages
  • Loading branch information
hannako authored Jun 10, 2021
2 parents 52f0905 + a6e81c7 commit e9c40f6
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 9 deletions.
67 changes: 67 additions & 0 deletions app/lib/body_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class BodyParser
def initialize(body)
@body = body
end

def title_and_link_sections
if raw_title_and_link_sections.any?
raw_title_and_link_sections.map do |section|
s = parse(section)
{
title: parsed_title(s),
links: parsed_links(s),
}
end
end
end

private

attr_reader :body

def parse(html)
Nokogiri::HTML.parse(html)
rescue Nokogiri::XML::SyntaxError
""
end

def parsed_body
@parsed_body ||= parse(body)
end

def raw_body_content
if parsed_body.present?
parsed_body.search("div.govspeak").children
end
end

def raw_title_and_link_sections
if raw_body_content.any?
raw_body_content.to_s.split(/(?=<h2 )/)
end
end

def parsed_links(section)
raw_links = section.present? ? section.css("a") : []
if raw_links.any?
raw_links.map do |link|
{
path: format_path(link["href"]),
text: link.content,
}
end
else
[]
end
end

def parsed_title(section)
raw_title = section.present? ? section.at_css("h2") : ""
raw_title.present? ? raw_title.content : ""
end

def format_path(raw_url)
uri = URI.parse(raw_url)
uri.host == "www.gov.uk" ? uri.path : raw_url
end
end
4 changes: 4 additions & 0 deletions app/presenters/content_item/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ def govspeak_body
direction: text_direction,
}
end

def title_and_link_sections
BodyParser.new(body.html_safe).title_and_link_sections
end
end
end
2 changes: 2 additions & 0 deletions app/presenters/content_item/brexit_hub_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def brexit_links
ContentItem::BrexitHubPage::BREXIT_BUSINESS_PAGE_CONTENT_ID => {
text: I18n.t("brexit.citizen_link"),
path: BREXIT_CITIZEN_PAGE_PATH,
track_category: "brexit-business-page",
},
ContentItem::BrexitHubPage::BREXIT_CITIZEN_PAGE_CONTENT_ID => {
text: I18n.t("brexit.business_link"),
path: BREXIT_BUSINESS_PAGE_PATH,
track_category: "brexit-citizen-page",
},
}
end
Expand Down
33 changes: 24 additions & 9 deletions app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% title_and_context = @content_item.brexit_link ? { title: @content_item.title } : @content_item.title_and_context %>
<% brexit_link = @content_item.brexit_link %>
<% title_and_context = brexit_link ? { title: @content_item.title } : @content_item.title_and_context %>
<%= render 'govuk_publishing_components/components/title', title_and_context %>
</div>
<%= render 'shared/translations' %>
<div class="govuk-grid-column-two-thirds">
<% if @content_item.brexit_link %>
<% if brexit_link %>
<div class="govuk-body-l govuk-!-margin-bottom-7">
<p class="govuk-body">
<%= I18n.t("brexit.heading_prefix") %> <%= link_to @content_item.brexit_link[:text], @content_item.brexit_link[:path] %>.
<%= I18n.t("brexit.heading_prefix") %>
<%= link_to(
brexit_link[:text],
brexit_link[:path],
class: "govuk-link",
data: {
track_action: brexit_link[:path],
track_category: brexit_link[:track_category],
track_label: brexit_link[:text],
module: 'gem-track-click',
}) %>.
</p>
</div>
<% else %>
Expand All @@ -23,7 +34,7 @@
</div>
</div>

<%= render 'shared/publisher_metadata_with_logo' unless @content_item.brexit_link %>
<%= render 'shared/publisher_metadata_with_logo' unless brexit_link %>
<%= render 'shared/history_notice', content_item: @content_item %>
<%= render 'govuk_publishing_components/components/notice', @content_item.withdrawal_notice_component %>

Expand All @@ -35,24 +46,28 @@
<%= render "govuk_publishing_components/components/print_link", {
margin_top: 0,
margin_bottom: 6,
} unless @content_item.brexit_link %>
} unless brexit_link %>

<%= render 'govuk_publishing_components/components/govspeak', {} do %>
<%= raw(@content_item.govspeak_body[:content]) %>
<% if brexit_link %>
<%= render partial: 'content_items/detailed_guide/brexit_links', locals: { brexit_link: brexit_link } %>
<% else %>
<%= render 'govuk_publishing_components/components/govspeak', {} do %>
<%= raw(@content_item.govspeak_body[:content]) %>
<% end %>
<% end %>

<div class="responsive-bottom-margin">
<%= render 'components/published-dates', {
published: @content_item.published,
last_updated: @content_item.updated,
history: @content_item.history
} unless @content_item.brexit_link %>
} unless brexit_link %>
</div>
<% end %>
<%= render "govuk_publishing_components/components/print_link", {
margin_top: 0,
margin_bottom: 6,
} unless @content_item.brexit_link %>
} unless brexit_link %>
</div>
<%= render 'shared/sidebar_navigation' %>
</div>
Expand Down
28 changes: 28 additions & 0 deletions app/views/content_items/detailed_guide/_brexit_links.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="govuk-grid-row">
<% @content_item.title_and_link_sections.each do |section| %>
<% if section[:title].present? %>
<%= render "govuk_publishing_components/components/heading", {
text: section[:title],
heading_level: 2,
font_size: "m",
margin_bottom: 6,
} %>
<% end %>
<% if section[:links].present? %>
<% track_category = brexit_link[:track_category] %>
<% links = section[:links].map do |link|
link_to(link[:text], link[:path], class: "govuk-link", data: {
track_action: link[:path],
track_category: track_category,
track_label: section[:title] || "",
module: 'gem-track-click',
})
end
%>
<%= render "govuk_publishing_components/components/list", {
items: links,
visible_counters: true,
} %>
<% end %>
<% end %>
</div>
18 changes: 18 additions & 0 deletions test/integration/detailed_guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,23 @@ class DetailedGuideTest < ActionDispatch::IntegrationTest
assert_not page.has_text?(@content_item["description"])
link_text = "Brexit guidance for businesses"
assert page.has_link?(link_text, href: ContentItem::BrexitHubPage::BREXIT_BUSINESS_PAGE_PATH)

# adds GA tracking to the li links
track_action = find_link("Foreign travel advice")["data-track-action"]
track_category = find_link("Foreign travel advice")["data-track-category"]
track_label = find_link("Foreign travel advice")["data-track-label"]

assert_equal "/foreign-travel-advice", track_action
assert_equal "brexit-citizen-page", track_category
assert_equal "Travel to the EU", track_label

# adds GA tracking to the description field links
track_action = find_link("Brexit guidance for businesses")["data-track-action"]
track_category = find_link("Brexit guidance for businesses")["data-track-category"]
track_label = find_link("Brexit guidance for businesses")["data-track-label"]

assert_equal ContentItem::BrexitHubPage::BREXIT_BUSINESS_PAGE_PATH, track_action
assert_equal "brexit-citizen-page", track_category
assert_equal "Brexit guidance for businesses", track_label
end
end
21 changes: 21 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def setup_and_visit_content_item_with_taxons(name, taxons)
def setup_and_visit_brexit_child_taxon(type = nil)
@content_item = get_content_example("detailed_guide").tap do |item|
item["content_id"] = type == "business" ? brexit_business_id : brexit_citizen_id
item["details"]["body"] = brexit_body_content

stub_content_store_has_item(item["base_path"], item.to_json)
visit_with_cachebust((item["base_path"]).to_s)
end
Expand All @@ -192,6 +194,25 @@ def brexit_business_id
ContentItem::BrexitHubPage::BREXIT_BUSINESS_PAGE_CONTENT_ID
end

def brexit_body_content
'<div class="govspeak">'\
'<h2 id="travel-to-the-eu">Travel to the EU</h2>\n \n'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/foreign-travel-advice" '\
'class="govuk-link">Foreign travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-eu"'\
'class="govuk-link">Travelling to the EU'\
'</a></li>\n</ul>\n'\
'<h2 id="travel-to-the-uk">Travel to the UK</h2>\n \n'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/local-travel-advice" '\
'class="govuk-link">Local travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-uk"'\
'class="govuk-link">Travelling to the UK'\
'</a></li>\n</ul>\n'\
"</div>"
end

def setup_and_visit_random_content_item(document_type: nil)
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: schema_type) do |payload|
payload.merge!("document_type" => document_type) unless document_type.nil?
Expand Down
104 changes: 104 additions & 0 deletions test/unit/body_parser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require "test_helper"

class BodyParserTest < ActiveSupport::TestCase
def html
'<div class="govspeak">'\
'<h2 id="travel-to-the-eu">Travel to the EU</h2>\n \n'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/foreign-travel-advice" '\
'class="govuk-link">Foreign travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-eu"'\
'class="govuk-link">Travelling to the EU'\
'</a></li>\n</ul>\n'\
'<h2 id="travel-to-the-uk">Travel to the UK</h2>\n \n'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/local-travel-advice" '\
'class="govuk-link">Local travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-uk"'\
'class="govuk-link">Travelling to the UK'\
'</a></li>\n</ul>\n'\
"</div>"
end

def html_missing_section_headings
'<div class="govspeak">'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/foreign-travel-advice" '\
'class="govuk-link">Foreign travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-eu"'\
'class="govuk-link">Travelling to the EU'\
'</a></li>\n</ul>\n'\
'<ul>\n'\
'<li><a rel="external" href="https://www.gov.uk/local-travel-advice" '\
'class="govuk-link">Local travel advice</a></li>\n'\
'<li><a rel="external" href="https://www.gov.uk/visit-uk"'\
'class="govuk-link">Travelling to the UK'\
'</a></li>\n</ul>\n'\
"</div>"
end

def subject
@subject ||= BodyParser.new(html)
end

test "#title_and_link_sections" do
expected = [
{
title: "Travel to the EU",
links: [
{
path: "/foreign-travel-advice",
text: "Foreign travel advice",
},
{
path: "/visit-eu",
text: "Travelling to the EU",
},
],
},
{
title: "Travel to the UK",
links: [
{
path: "/local-travel-advice",
text: "Local travel advice",
},
{
path: "/visit-uk",
text: "Travelling to the UK",
},
],
},
]

assert_equal expected, subject.title_and_link_sections
end

test "when parsing html missing the section headings" do
subject = BodyParser.new(html_missing_section_headings)
expected = [
{
title: "",
links: [
{
path: "/foreign-travel-advice",
text: "Foreign travel advice",
},
{
path: "/visit-eu",
text: "Travelling to the EU",
},
{
path: "/local-travel-advice",
text: "Local travel advice",
},
{
path: "/visit-uk",
text: "Travelling to the UK",
},
],
},
]
assert_equal expected, subject.title_and_link_sections
end
end

0 comments on commit e9c40f6

Please sign in to comment.