Skip to content

Commit

Permalink
Change tracking
Browse files Browse the repository at this point in the history
- Update tracking to be more flexible, more data attributes can be passed with any value. On the links only.[1]

[1]#2656

Update CHANGELOG
  • Loading branch information
Chris Yoong committed Mar 16, 2022
1 parent 7be7942 commit 6fbf996
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Migrate cross domain tracking script from static ([PR #2607](https://github.com/alphagov/govuk_publishing_components/pull/2607))
* Update card tracking ([PR #2679](https://github.com/alphagov/govuk_publishing_components/pull/2679))

## 28.9.1

Expand Down
34 changes: 9 additions & 25 deletions app/views/govuk_publishing_components/components/_cards.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
sub_heading_level ||= 3
two_column_layout ||= false
local_assigns ||= nil
data_attributes ||= nil

ul_classes = %w[gem-c-cards__list]
ul_classes << 'gem-c-cards__list--two-column-desktop' if two_column_layout
Expand All @@ -16,42 +17,25 @@
<%= content_tag(shared_helper.get_heading_level, class: "gem-c-cards__heading govuk-heading-m") do %>
<%= heading %>
<% end %>
<% end %>
<%= content_tag 'ul', class: ul_classes do %>
<% end %>

<%= content_tag('ul', class: ul_classes) do %>
<%
items.each do |item|
link = item[:link]

if !link[:path].present?
raise ArgumentError, "The cards component requires a href for all the links"
end

data_attributes = nil
attributes = link[:tracking_attributes].presence

if attributes && attributes[:track_category] && attributes[:track_action]
data_attributes = {
track_action: attributes[:track_action],
track_category: attributes[:track_category],
track_dimension_index: attributes[:track_dimension_index],
track_dimension: link[:text],
track_label: link[:path],
}
end

link = capture do
link_to(link[:text], link[:path], {
class: "govuk-link gem-c-cards__link",
data: data_attributes,
})
end
%>

<li class="gem-c-cards__list-item">
<%= content_tag("h#{sub_heading_level}", class: "gem-c-cards__sub-heading govuk-heading-s") do %>
<%= link %>
<%=
link_to link[:text], link[:path],
class: "govuk-link gem-c-cards__link",
data: link[:data_attributes]
%>
<% end %>

<% if item[:description] %>
<p class="govuk-body gem-c-cards__description"><%= item[:description] %></p>
<% end %>
Expand Down
18 changes: 12 additions & 6 deletions app/views/govuk_publishing_components/components/docs/cards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,30 @@ examples:
text: Benefits
path: http://www.gov.uk
description: Includes eligibility, appeals, tax credits and Universal Credit
with_tracking_attributes:
with_data_attributes:
description: |
Tracking can be enabled on the cards component by passing a minimum of data_track_category and data_track_action. Other tracking attributes are optional. Note: tracking events do not currently fire within the component guide.
Data attributes can be passed to individual links within the component as shown.
data:
items:
- link:
text: Benefits
path: http://www.gov.uk
tracking_attributes:
track_action: track_action
data_attributes:
track_category: homepageClicked
track_action: track-action
track_label: track-label
track_dimension: track-dimension
track_dimension_index: 29
track_value: 9
description: Includes eligibility, appeals, tax credits and Universal Credit
- link:
text: Births, deaths, marriages and&nbsp;care
path: http://www.gov.uk
tracking_attributes:
track_action: track_action
data_attributes:
track_category: homepageClicked
track_action: track-action
track_label: track-label
track_dimension: track-dimension
track_dimension_index: 29
track_value: 9
description: Parenting, civil partnerships, divorce and Lasting Power of Attorney
22 changes: 2 additions & 20 deletions spec/components/cards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ def component_name
link: {
text: "Benefits",
path: "http://www.gov.uk",
tracking_attributes: {
data_attributes: {
track_action: "some_action",
track_category: "homepageClicked",
track_dimension_index: 29,
track_label: "http://www.gov.uk",
},
},
},
Expand All @@ -143,23 +144,4 @@ def component_name
assert_select ".govuk-link.gem-c-cards__link[data-track-category='homepageClicked']", count: 1
assert_select ".govuk-link.gem-c-cards__link[data-track-dimension-index='29']", count: 1
end

it "doesn't render any tracking attributes if the required attributes are not set" do
test_data = {
items: [
{
link: {
text: "Benefits",
path: "http://www.gov.uk",
tracking_attributes: {
# This omits action and category which are required
track_dimension_index: 29,
},
},
},
],
}
render_component(test_data)
assert_select ".govuk-link.gem-c-cards__link[data-track-dimension-index='1']", count: 0
end
end

0 comments on commit 6fbf996

Please sign in to comment.