From 66b590327d02744d4b52894a199416e3d1735275 Mon Sep 17 00:00:00 2001 From: "alex.bowen" Date: Mon, 30 Sep 2024 15:20:29 +0100 Subject: [PATCH] add filter section open/close ga4 tracking --- .../javascripts/components/filter-section.js | 25 ++++++++++++ app/views/components/_filter_section.html.erb | 12 +++++- .../components/filter-section-spec.js | 39 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/components/filter-section.js create mode 100644 spec/javascripts/components/filter-section-spec.js diff --git a/app/assets/javascripts/components/filter-section.js b/app/assets/javascripts/components/filter-section.js new file mode 100644 index 000000000..962671b9e --- /dev/null +++ b/app/assets/javascripts/components/filter-section.js @@ -0,0 +1,25 @@ +window.GOVUK = window.GOVUK || {} +window.GOVUK.Modules = window.GOVUK.Modules || {}; + +(function (Modules) { + 'use strict' + + class FilterSection { + constructor ($module) { + this.$module = $module + this.$summary = this.$module.querySelector('.app-c-filter-section__summary') + } + + init () { + this.$summary.addEventListener('click', this.setTrackingData.bind(this)) + } + + setTrackingData () { + const eventTrackingData = JSON.parse(this.$summary.getAttribute('data-ga4-event')) + eventTrackingData.action = this.$module.hasAttribute('open') === false ? 'opened' : 'closed' + this.$summary.setAttribute('data-ga4-event', JSON.stringify(eventTrackingData)) + } + } + + Modules.FilterSection = FilterSection +})(window.GOVUK.Modules) diff --git a/app/views/components/_filter_section.html.erb b/app/views/components/_filter_section.html.erb index f90d39ec5..4b7a007d0 100644 --- a/app/views/components/_filter_section.html.erb +++ b/app/views/components/_filter_section.html.erb @@ -9,12 +9,22 @@ summary_aria_attributes ||= {} component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns) + component_helper.add_data_attribute({ module: "filter-section ga4-event-tracker" }) component_helper.add_class("app-c-filter-section") component_helper.set_open(open) %> <%= tag.details(**component_helper.all_attributes) do %> - <%= tag.summary class: "app-c-filter-section__summary", aria: summary_aria_attributes do %> + <%= tag.summary class: "app-c-filter-section__summary", aria: summary_aria_attributes, + "data-ga4-expandable": true, + "data-ga4-event" => { + event_name: "select_content", + type: "finder", + section: heading_text, + text: heading_text, + index: index_section + 1, + index_total: index_section_count + }.to_json do %> <%= content_tag("h#{heading_level}", class: "app-c-filter-section__summary-heading") do %> <% if visually_hidden_heading_prefix.present? %> <%= visually_hidden_heading_prefix %> diff --git a/spec/javascripts/components/filter-section-spec.js b/spec/javascripts/components/filter-section-spec.js new file mode 100644 index 000000000..9e095f830 --- /dev/null +++ b/spec/javascripts/components/filter-section-spec.js @@ -0,0 +1,39 @@ +describe('Filter section module', () => { + 'use strict' + + let filterSection, fixture + + const loadFilterSectionComponent = (markup) => { + fixture = document.createElement('div') + document.body.appendChild(fixture) + fixture.innerHTML = markup + filterSection = new GOVUK.Modules.FilterSection(fixture.querySelector('.app-c-filter-section')) + } + + const html = `
+
+ + filter section + +
content
+
+
` + + beforeEach(() => { + loadFilterSectionComponent(html) + filterSection.init() + }) + + afterEach(() => { + // fixture.remove() + }) + + it('sets correct ga4 tracking data when panel is opened and closed', () => { + console.log(filterSection) + filterSection.$summary.click() + expect(JSON.parse(filterSection.$summary.getAttribute('data-ga4-event')).action).toBe('opened') + + filterSection.$summary.click() + expect(JSON.parse(filterSection.$summary.getAttribute('data-ga4-event')).action).toBe('closed') + }) +})