diff --git a/app/views/govuk_component/analytics_meta_tags.raw.html.erb b/app/views/govuk_component/analytics_meta_tags.raw.html.erb
index 717ec0912..f52340c83 100644
--- a/app/views/govuk_component/analytics_meta_tags.raw.html.erb
+++ b/app/views/govuk_component/analytics_meta_tags.raw.html.erb
@@ -2,19 +2,27 @@
# 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.
- links_hash = content_item.to_h.deep_symbolize_keys[:links] || {}
+ content_item_hash = content_item.to_h.deep_symbolize_keys
+ links_hash = content_item_hash[:links] || {}
+ meta_tags = {}
+
+ meta_tags["govuk:format"] = content_item_hash[:format] if content_item_hash[:format]
+
+ need_ids = content_item_hash[:need_ids] || []
+ meta_tags["govuk:need-ids"] = need_ids.join(',') if need_ids.any?
organisations = []
organisations += links_hash[:organisations] || []
organisations += links_hash[:lead_organisations] || []
organisations += links_hash[:supporting_organisations] || []
organisations += links_hash[:worldwide_organisations] || []
+ organisations_content = organisations.map { |link| "<#{link[:analytics_identifier]}>" }.join
+ meta_tags["govuk:analytics:organisations"] = organisations_content if organisations.any?
world_locations = links_hash[: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?
%>
-<% if organisations.any? %>
-
-<% end %>
-<% if world_locations.any? %>
-
+<% meta_tags.each do |name, content| %>
+
<% end %>
diff --git a/test/govuk_component/analytics_meta_tags_test.rb b/test/govuk_component/analytics_meta_tags_test.rb
index d39efe660..7baf4d5b5 100644
--- a/test/govuk_component/analytics_meta_tags_test.rb
+++ b/test/govuk_component/analytics_meta_tags_test.rb
@@ -6,6 +6,20 @@ def component_name
"analytics_meta_tags"
end
+ test "no meta tags are rendered when there's no trackable data" do
+ assert_empty render_component(content_item: {})
+ end
+
+ test "renders format in a meta tag" do
+ render_component(content_item: { format: "case_study" })
+ assert_meta_tag('govuk:format', 'case_study')
+ end
+
+ test "renders need IDs as a comma separated list" do
+ render_component(content_item: { need_ids: [100001, 100002] })
+ assert_meta_tag('govuk:need-ids', '100001,100002')
+ end
+
test "renders organisations in a meta tag with angle brackets" do
content_item = {
links: {
@@ -17,8 +31,7 @@ def component_name
}
render_component(content_item: content_item)
-
- assert_select "meta[name='govuk:analytics:organisations'][content='']"
+ assert_meta_tag('govuk:analytics:organisations', '')
end
test "renders world locations in a meta tag with angle brackets" do
@@ -36,8 +49,7 @@ def component_name
}
render_component(content_item: content_item)
-
- assert_select "meta[name='govuk:analytics:world-locations'][content='']"
+ assert_meta_tag('govuk:analytics:world-locations', '')
end
test "handling of string keys and/or GdsApi::Response objects" do
@@ -54,7 +66,10 @@ def component_name
api_response = GdsApi::Response.new(net_http_response)
render_component(content_item: api_response)
+ assert_meta_tag('govuk:analytics:world-locations', '')
+ end
- assert_select "meta[name='govuk:analytics:world-locations'][content='']"
+ def assert_meta_tag(name, content)
+ assert_select "meta[name='#{name}'][content='#{content}']"
end
end