Skip to content

Commit

Permalink
add ga4 tracking to filter summary remove buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbowen committed Oct 2, 2024
1 parent 7ce7d76 commit c27521c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/views/components/_filter_summary.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
component_helper.add_class(shared_helper.get_margin_bottom) if local_assigns[:margin_bottom]
component_helper.add_data_attribute({ module: "ga4-event-tracker" })
component_helper.add_class("app-c-filter-summary")
%>
Expand All @@ -25,7 +26,14 @@
<ul class="app-c-filter-summary__remove-filters">
<% filters.each do |filter| %>
<li>
<%= link_to filter[:remove_href], class: "app-c-filter-summary__remove-filter" do %>
<%= link_to filter[:remove_href],
class: "app-c-filter-summary__remove-filter",
"data-ga4-event" => {
event_name: "select_content",
type: "finder",
section: filter[:label],
action: "remove"
}.to_json do %>
<span class="app-c-filter-summary__remove-filter-text">
<span class="govuk-visually-hidden"><%= filter[:visually_hidden_prefix] %></span>
<%= filter[:label] %>: <%= filter[:value] %>
Expand Down
17 changes: 17 additions & 0 deletions spec/components/filter_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ def render_component(locals)

assert_select "h4.app-c-filter-summary__heading", count: 1
end

it "renders ga4 tracking attributes to remove link" do
link_event_attributes = {
"event-name": "select_content",

Check failure on line 81 in spec/components/filter_summary_spec.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/FirstHashElementIndentation: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
type: "finder",
section: "Filter",
action: "remove"

Check failure on line 84 in spec/components/filter_summary_spec.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
}

render_component(filters:, data: link_event_attributes.to_json)

# doesnt work
# assert_select ".app-c-filter-summary__remove-filter[data-ga4-event='#{link_event_attributes.to_json}']"
# false positive as data can be anything and hence i guess is completely ignored
# assert_select ".app-c-filter-summary__remove-filter[data-ga4-event]", data: { "ga4-event": link_event_attributes.to_json }
# assert_select ".app-c-filter-summary__remove-filter[data-ga4-event]", data: { "ga4-event": "abc" }
end
end

0 comments on commit c27521c

Please sign in to comment.