diff --git a/app/presenters/search_result_presenter.rb b/app/presenters/search_result_presenter.rb index 7be021e1e..817f1a702 100644 --- a/app/presenters/search_result_presenter.rb +++ b/app/presenters/search_result_presenter.rb @@ -27,7 +27,7 @@ def document_list_component_data text: title, path: link, description: sanitize(summary_text), - data_attributes: ecommerce_data(link, title), + data_attributes: ga4_ecommerce_data(link).merge(ecommerce_data(link, title)), }, metadata: structure_metadata, metadata_raw: metadata, @@ -80,23 +80,29 @@ def structure_parts GovukError.notify(MalformedPartError.new, extra: { part:, link: }) next end + path = "#{link}/#{part[:slug]}" { link: { text: part[:title], - path: "#{link}/#{part[:slug]}", + path:, description: part[:body], - data_attributes: ecommerce_data("#{link}/#{part[:slug]}", part[:title], part_index: index), + data_attributes: ga4_ecommerce_data(path).merge(ecommerce_data(path, part[:title], part_index: index)), }, } end structured_parts.compact end + def ga4_ecommerce_data(path) + { + ga4_ecommerce_path: path, + } + end + def ecommerce_data(link, title, part_index: nil) return {} unless @include_ecommerce { - ga4_ecommerce_path: link, ecommerce_path: link, ecommerce_row: 1, ecommerce_index: document.index, diff --git a/spec/presenters/result_set_presenter_spec.rb b/spec/presenters/result_set_presenter_spec.rb index 20eac9bf8..35e7d94f7 100644 --- a/spec/presenters/result_set_presenter_spec.rb +++ b/spec/presenters/result_set_presenter_spec.rb @@ -183,7 +183,7 @@ end end - context "with ecommerce disabled" do + context "with universal analytics ecommerce disabled" do let(:enable_ecommerce) { false } let(:results) do [FactoryBot.build( @@ -195,13 +195,15 @@ )] end - it "doesn't include ecommerce attributes" do + it "doesn't include UA ecommerce attributes" do expected_hash = { link: { text: "document_title", path: "/path/to/doc", description: "document_description", - data_attributes: {}, + data_attributes: { + ga4_ecommerce_path: "/path/to/doc", + }, }, metadata: { "Organisations" => "Organisations: Department for Work and Pensions",