Skip to content

Commit

Permalink
Merge pull request #399 from alphagov/track-document-collection-clicks
Browse files Browse the repository at this point in the history
Track document collection clicks
  • Loading branch information
carvil authored Jul 18, 2017
2 parents 6349fb4 + cfc98c8 commit cb44cb5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 56 deletions.
43 changes: 43 additions & 0 deletions app/views/content_items/_document_collection_body.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<% if @content_item.body.present? %>
<%= render 'govuk_component/govspeak', @content_item.govspeak_body %>
<% end %>

<% @content_item.groups.each_with_index do |group, group_index| %>
<%= @content_item.group_heading(group) %>
<% if group["body"].present? %>
<%= render 'govuk_component/govspeak',
content: group["body"],
direction: page_text_direction %>
<% end %>
<ol class="group-document-list" data-module="track-click">
<% @content_item.group_document_links(group).each_with_index do |link, link_index| %>
<li class="group-document-list-item">
<h3 class="group-document-list-item-title">
<%=
link_to(
link[:title],
link[:base_path],
data: {
track_category: 'navDocumentCollectionLinkClicked',
track_action: "#{group_index + 1}.#{link_index + 1}",
track_label: link[:base_path],
track_options: {
dimension28: @content_item.group_document_links(group).count.to_s,
dimension29: link[:title]
}
}
)
%>
</h3>
<ul class="group-document-list-item-attributes">
<li>
<time datetime="<%= link[:public_updated_at].iso8601 %>">
<%= l(link[:public_updated_at], format: :short_ordinal) %>
</time>
</li>
<li><%= t("content_item.schema_name.#{link[:document_type]}", count: 1) %></li>
</ul>
</li>
<% end %>
</ol>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,7 @@
<%= render 'shared/description', description: @content_item.description %>
<%= render 'shared/sidebar_contents', contents: @content_item.contents %>

<% if @content_item.body.present? %>
<%= render 'govuk_component/govspeak', @content_item.govspeak_body %>
<% end %>
<% @content_item.groups.each do |group| %>
<%= @content_item.group_heading(group) %>
<% if group["body"].present? %>
<%= render 'govuk_component/govspeak',
content: group["body"],
direction: page_text_direction %>
<% end %>
<ol class="group-document-list">
<% @content_item.group_document_links(group).each do |link| %>
<li class="group-document-list-item">
<h3 class="group-document-list-item-title">
<%= link_to(link[:title], link[:base_path]) %>
</h3>
<ul class="group-document-list-item-attributes">
<li>
<time datetime="<%= link[:public_updated_at].iso8601 %>">
<%= l(link[:public_updated_at], format: :short_ordinal) %>
</time>
</li>
<li><%= t("content_item.schema_name.#{link[:document_type]}", count: 1) %></li>
</ul>
</li>
<% end %>
</ol>
<% end %>
<%= render 'document_collection_body' %>
</div>

<div class="column-third">
Expand Down
29 changes: 1 addition & 28 deletions app/views/content_items/document_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,7 @@
</div>
<% end %>
<div class="column-two-thirds <% unless @content_item.contents.any? %>offset-one-third<% end %>">
<% if @content_item.body.present? %>
<%= render 'govuk_component/govspeak', @content_item.govspeak_body %>
<% end %>
<% @content_item.groups.each do |group| %>
<%= @content_item.group_heading(group) %>
<% if group["body"].present? %>
<%= render 'govuk_component/govspeak',
content: group["body"],
direction: page_text_direction %>
<% end %>
<ol class="group-document-list">
<% @content_item.group_document_links(group).each do |link| %>
<li class="group-document-list-item">
<h3 class="group-document-list-item-title">
<%= link_to(link[:title], link[:base_path]) %>
</h3>
<ul class="group-document-list-item-attributes">
<li>
<time datetime="<%= link[:public_updated_at].iso8601 %>">
<%= l(link[:public_updated_at], format: :short_ordinal) %>
</time>
</li>
<li><%= t("content_item.schema_name.#{link[:document_type]}", count: 1) %></li>
</ul>
</li>
<% end %>
</ol>
<% end %>
<%= render 'document_collection_body' %>
</div>
<div data-sticky-element class="govuk-sticky-element">
<%= render 'shared/back_to_content_link' %>
Expand Down
50 changes: 50 additions & 0 deletions test/integration/document_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,56 @@ class DocumentCollectionTest < ActionDispatch::IntegrationTest
end
end

test 'includes tracking data on all collection documents' do
setup_and_visit_content_item('document_collection')
groups = page.all('.group-document-list')

groups.each do |group|
assert_equal(
"track-click",
group['data-module'],
"Expected the module 'track-click' to be set in the group #{group.inspect}"
)
end

first_section_links = groups.first.all('.group-document-list-item-title a')
first_link = first_section_links.first

assert_equal(
'navDocumentCollectionLinkClicked',
first_link['data-track-category'],
'Expected a tracking category to be set in the data attributes'
)

assert_equal(
'1.1',
first_link['data-track-action'],
'Expected the link position to be set in the data attributes'
)

assert_match(
first_link['data-track-label'],
first_link[:href],
'Expected the content item base path to be set in the data attributes'
)

assert first_link['data-track-options'].present?

data_options = JSON.parse(first_link['data-track-options'])

assert_equal(
first_section_links.count.to_s,
data_options['dimension28'],
'Expected the total number of content items within the section to be present in the tracking options'
)

assert_equal(
first_link.text,
data_options['dimension29'],
'Expected the subtopic title to be present in the tracking options'
)
end

test "withdrawn collection" do
setup_and_visit_content_item('document_collection_withdrawn')
assert page.has_css?('title', text: "[Withdrawn]", visible: false)
Expand Down

0 comments on commit cb44cb5

Please sign in to comment.