Skip to content

Commit

Permalink
Add separate tracking data for home link on breadcrumb
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmudH authored and oscarwyatt committed Nov 8, 2018
1 parent 7e4e28d commit 309ec85
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,8 @@ DEPENDENCIES
webmock (~> 3.0.1)
yard

RUBY VERSION
ruby 2.5.3p105

BUNDLED WITH
1.17.1
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,33 @@
inverse ||= false
collapse_class = collapse_on_mobile && breadcrumbs.any? { |crumb| crumb[:is_page_parent] } ? "gem-c-breadcrumbs--collapse-on-mobile" : ""
invert_class = inverse ? "gem-c-breadcrumbs--inverse" : ""
structured_data = GovukPublishingComponents::Presenters::Breadcrumbs.new(breadcrumbs, request.path).structured_data
breadcrumb_presenter = GovukPublishingComponents::Presenters::Breadcrumbs.new(breadcrumbs, request.path)
%>

<script type="application/ld+json">
<%= raw structured_data.to_json %>
<%= raw breadcrumb_presenter.structured_data.to_json %>
</script>

<div class="gem-c-breadcrumbs govuk-breadcrumbs <%= invert_class %>" data-module="track-click">
<ol class="govuk-breadcrumbs__list">
<% breadcrumbs.each_with_index do |crumb, index| %>
<%
is_link = crumb[:url].present? || crumb[:is_current_page]
path = crumb[:is_current_page] ? '#content' : crumb[:url]
aria_current = crumb[:is_current_page] ? 'page' : 'false'
%>
<li class="govuk-breadcrumbs__list-item" aria-current="<%= aria_current %>">
<% if is_link %>
<% breadcrumb = GovukPublishingComponents::Presenters::Breadcrumb.new(crumb, index) %>
<li class="govuk-breadcrumbs__list-item" aria-current="<%= breadcrumb.aria_current %>">
<% if breadcrumb.is_link? %>
<%= link_to(
crumb[:title],
path,
data: {
track_category: 'breadcrumbClicked',
track_action: index + 1,
track_label: path,
track_options: {
dimension28: breadcrumbs.length.to_s,
dimension29: crumb[:title]
}
},
breadcrumb[:title],
breadcrumb.path,
data: breadcrumb.tracking_data(breadcrumbs.length),
class: "govuk-breadcrumbs__link",
aria: {
current: aria_current,
current: breadcrumb.aria_current,
}
) %>
<% else %>
<%= crumb[:title] %>
<%= breadcrumb[:title] %>
<% end %>
</li>
<% end %>
</ol>
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="govuk-header__container govuk-width-container">

<div class="govuk-header__logo">
<a href="/" class="govuk-header__link govuk-header__link--homepage">
<a href="/" class="govuk-header__link govuk-header__link--homepage" data-module="track-click" data-tracking-category="homeLinkClicked">
<span class="govuk-header__logotype">
<svg role="presentation" focusable="false" class="govuk-header__logotype-crown" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 132 97" height="32" width="36">
<path fill="currentColor" fill-rule="evenodd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def breadcrumbs
{
title: parent.title,
url: parent.base_path,
is_page_parent: index.zero?
is_page_parent: index.zero?,
}
end

Expand Down
63 changes: 58 additions & 5 deletions lib/govuk_publishing_components/presenters/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,68 @@ def structured_data

def item_list_element
breadcrumbs.each_with_index.map do |crumb, index|
{
"@type" => "ListItem",
"position" => index + 1,
"item" => list_item_item(crumb)
Breadcrumb.new(crumb, index).item_list_element(@request_path)
end
end
end

class Breadcrumb
extend Forwardable
def_delegators :@crumb, :[]

def initialize(crumb, index)
@crumb = crumb
@index = index + 1
end

def item_list_element(request_path)
{
"@type" => "ListItem",
"position" => index,
"item" => list_item_item(request_path)
}
end

def is_link?
crumb[:url].present? || crumb[:is_current_page]
end

def path
crumb[:is_current_page] ? '#content' : crumb[:url]
end

def aria_current
crumb[:is_current_page] ? 'page' : 'false'
end

def tracking_data(breadcrumbs_length)
data = {
track_category: 'breadcrumbClicked',
track_action: index,
track_label: path,
track_options: {
dimension28: breadcrumbs_length.to_s,
dimension29: crumb[:title]
}
}

is_homepage = crumb[:url] == "/"

if is_homepage
data[:track_category] = 'homeLinkClicked'
data[:track_action] = 'homeBreadcrumb'
data[:track_label] = ''
data[:track_options] = '{}'
end

data
end

def list_item_item(crumb)
private

attr_reader :crumb, :index

def list_item_item(request_path)
path = crumb[:is_current_page] ? request_path : crumb[:url]
item = { "name" => crumb[:title] }
item["@id"] = Plek.new.website_root + path if path
Expand Down
21 changes: 21 additions & 0 deletions spec/components/breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ def assert_link_with_text_in(selector, link, text)
assert_select ".govuk-breadcrumbs__list-item:first-child a[data-track-options='#{expected_tracking_options.to_json}']", 1
end

it "a link to the homepage has separate tracking data" do
render_component(breadcrumbs: [{ title: 'Section', url: '/section' }, { title: 'Home', url: '/' }])

expected_tracking_options = {
dimension28: "2",
dimension29: "Section"
}

assert_select '.gem-c-breadcrumbs[data-module="track-click"]', 1

assert_select '.govuk-breadcrumbs__list-item:nth-child(1) a[data-track-action="1"]', 1
assert_select '.govuk-breadcrumbs__list-item:nth-child(1) a[data-track-label="/section"]', 1
assert_select '.govuk-breadcrumbs__list-item:nth-child(1) a[data-track-category="breadcrumbClicked"]', 1
assert_select ".govuk-breadcrumbs__list-item:nth-child(1) a[data-track-options='#{expected_tracking_options.to_json}']", 1

assert_select '.govuk-breadcrumbs__list-item:nth-child(2) a[data-track-action="homeBreadcrumb"]', 1
assert_select '.govuk-breadcrumbs__list-item:nth-child(2) a[data-track-label=""]', 1
assert_select '.govuk-breadcrumbs__list-item:nth-child(2) a[data-track-category="homeLinkClicked"]', 1
assert_select ".govuk-breadcrumbs__list-item:nth-child(2) a[data-track-options='{}']", 1
end

it "tracks the total breadcrumb count on each breadcrumb" do
breadcrumbs = [
{ title: 'Section 1', url: '/section-1' },
Expand Down

0 comments on commit 309ec85

Please sign in to comment.