-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from alphagov/port-metatags
Port Analytics meta tags component
- Loading branch information
Showing
6 changed files
with
566 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/views/govuk_publishing_components/components/_meta_tags.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% meta_tags = GovukPublishingComponents::Presenters::MetaTags.new(content_item, local_assigns).meta_tags %> | ||
|
||
<% meta_tags.each do |name, content| %> | ||
<meta name="<%= name %>" content="<%= content %>"> | ||
<% end %> |
25 changes: 25 additions & 0 deletions
25
app/views/govuk_publishing_components/components/docs/meta_tags.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Meta Tags | ||
description: Meta tags to provide analytics information about the current page | ||
body: | | ||
This takes a content-store links hash like object which it can then turn into | ||
the correct analytics identifier metadata tags. | ||
The code which reads the meta tags can be found <a href="https://github.com/alphagov/static/blob/master/app/assets/javascripts/analytics/static-analytics.js#L76-L96">in static-analytics.js</a>. | ||
accessibility_criteria: | | ||
The analytics meta tags component should not be visible to any users. | ||
examples: | ||
with_organisations: | ||
data: | ||
content_item: | ||
links: | ||
organisations: | ||
- analytics_identifier: D1 | ||
- analytics_identifier: D3 | ||
worldwide_organisations: | ||
- analytics_identifier: EO3 | ||
with_world_locations: | ||
data: | ||
content_item: | ||
links: | ||
world_locations: | ||
- analytics_identifier: WL3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
lib/govuk_publishing_components/presenters/meta_tags.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
module GovukPublishingComponents | ||
module Presenters | ||
class MetaTags | ||
attr_reader :content_item, :details, :links, :local_assigns | ||
|
||
def initialize(content_item, local_assigns) | ||
# We have to call deep_symbolize_keys because we're often dealing with a | ||
# parsed JSON document which will have string keys by default, but our | ||
# components use symbol keys and we want consistency. | ||
@content_item = content_item.to_h.deep_symbolize_keys | ||
@details = @content_item[:details] || {} | ||
@links = @content_item[:links] || {} | ||
@local_assigns = local_assigns | ||
end | ||
|
||
def meta_tags | ||
meta_tags = {} | ||
meta_tags = add_core_tags(meta_tags) | ||
meta_tags = add_organisation_tags(meta_tags) | ||
meta_tags = add_political_tags(meta_tags) | ||
meta_tags = add_taxonomy_tags(meta_tags) | ||
meta_tags = add_step_by_step_tags(meta_tags) | ||
meta_tags | ||
end | ||
|
||
private | ||
|
||
def add_core_tags(meta_tags) | ||
meta_tags["govuk:format"] = content_item[:document_type] if content_item[:document_type] | ||
meta_tags["govuk:schema-name"] = content_item[:schema_name] if content_item[:schema_name] | ||
|
||
user_journey_stage = content_item[:user_journey_document_supertype] | ||
meta_tags["govuk:user-journey-stage"] = user_journey_stage if user_journey_stage | ||
|
||
navigation_document_type = content_item[:navigation_document_supertype] | ||
meta_tags["govuk:navigation-document-type"] = navigation_document_type if navigation_document_type | ||
|
||
meta_tags["govuk:content-id"] = content_item[:content_id] if content_item[:content_id] | ||
meta_tags["govuk:withdrawn"] = "withdrawn" if content_item[:withdrawn_notice].present? | ||
meta_tags["govuk:content-has-history"] = "true" if has_content_history? | ||
meta_tags["govuk:static-analytics:strip-postcodes"] = "true" if should_strip_postcode_pii?(content_item, local_assigns) | ||
|
||
meta_tags | ||
end | ||
|
||
def add_organisation_tags(meta_tags) | ||
organisations = [] | ||
organisations += links[:organisations] || [] | ||
organisations += links[:worldwide_organisations] || [] | ||
organisations_content = organisations.map { |link| "<#{link[:analytics_identifier]}>" }.uniq.join | ||
meta_tags["govuk:analytics:organisations"] = organisations_content if organisations.any? | ||
|
||
world_locations = links[:world_locations] || [] | ||
world_locations_content = world_locations.map { |link| "<#{link[:analytics_identifier]}>" }.join | ||
meta_tags["govuk:analytics:world-locations"] = world_locations_content if world_locations.any? | ||
|
||
meta_tags | ||
end | ||
|
||
def add_political_tags(meta_tags) | ||
if details.key?(:political) && details.key?(:government) | ||
political_status = "non-political" | ||
if details[:political] | ||
political_status = details[:government][:current] ? "political" : "historic" | ||
end | ||
|
||
meta_tags["govuk:political-status"] = political_status | ||
meta_tags["govuk:publishing-government"] = details[:government][:slug] | ||
end | ||
|
||
meta_tags | ||
end | ||
|
||
def add_taxonomy_tags(meta_tags) | ||
themes = root_taxon_slugs(content_item) | ||
meta_tags["govuk:themes"] = themes.to_a.sort.join(', ') unless themes.empty? | ||
|
||
taxons = if content_item[:document_type] == 'taxon' | ||
[content_item] | ||
else | ||
links[:taxons] || [] | ||
end | ||
|
||
taxons.sort_by! { |taxon| taxon[:title] } | ||
taxon_slugs_without_theme = taxons.map do |taxon| | ||
base_path = taxon[:base_path] || "" | ||
slug_without_theme = base_path[%r{/[^/]+/(.+)}, 1] | ||
# Return the slug without the theme, or in the special case of a root taxon, | ||
# just return the full slug (because it doesn't have a slug beneath the theme) | ||
slug_without_theme || base_path.sub(%r(^/), '') | ||
end | ||
taxon_ids = taxons.map { |taxon| taxon[:content_id] } | ||
|
||
meta_tags["govuk:taxon-id"] = taxon_ids.first unless taxon_ids.empty? | ||
meta_tags["govuk:taxon-ids"] = taxon_ids.join(',') unless taxon_ids.empty? | ||
meta_tags["govuk:taxon-slug"] = taxon_slugs_without_theme.first unless taxon_slugs_without_theme.empty? | ||
meta_tags["govuk:taxon-slugs"] = taxon_slugs_without_theme.join(',') unless taxon_slugs_without_theme.empty? | ||
meta_tags | ||
end | ||
|
||
def add_step_by_step_tags(meta_tags) | ||
stepnavs = links[:part_of_step_navs] || [] | ||
stepnavs_content = stepnavs.map { |stepnav| stepnav[:content_id] }.join(",") | ||
meta_tags["govuk:stepnavs"] = stepnavs_content if stepnavs_content.present? | ||
meta_tags | ||
end | ||
|
||
def has_content_history? | ||
(content_item[:public_updated_at] && details[:first_public_at] && content_item[:public_updated_at] != details[:first_public_at]) || | ||
(details[:change_history] && details[:change_history].size > 1) | ||
end | ||
|
||
def root_taxon_slugs(content_item) | ||
root_taxon_set = Set.new | ||
|
||
links = content_item[:links] | ||
# Taxons will have :parent_taxons, but content items will have :taxons | ||
parent_taxons = links[:parent_taxons] || links[:taxons] unless links.nil? | ||
|
||
if parent_taxons.blank? | ||
root_taxon_set << content_item[:base_path].sub(%r(^/), '') if content_item[:document_type] == 'taxon' | ||
else | ||
parent_taxons.each do |parent_taxon| | ||
root_taxon_set += root_taxon_slugs(parent_taxon) | ||
end | ||
end | ||
|
||
root_taxon_set | ||
end | ||
|
||
def should_strip_postcode_pii?(content_item, local_assigns) | ||
# allow override if we explicitly want to strip pii (or not) regardless of | ||
# document_type | ||
return local_assigns[:strip_postcode_pii] if local_assigns.key?(:strip_postcode_pii) | ||
|
||
formats_that_might_include_postcodes = %w[smart_answer search] | ||
formats_that_might_include_postcodes.include?(content_item[:document_type]) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.