Skip to content

Commit

Permalink
Add GA4 attribute to start now buttons
Browse files Browse the repository at this point in the history
- add a custom GA4 attribute to be picked up by the link tracker for the 'start now' variant of the button component
  • Loading branch information
andysellick committed Mar 20, 2024
1 parent 02a6623 commit 70085f3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
@@ -1,5 +1,6 @@
<%
add_gem_component_stylesheet("button")
disable_ga4 ||= false

# button_helper.css_classes generates "gem-c-button"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
Expand Down
Expand Up @@ -44,6 +44,14 @@ examples:
href: '#'
start: true
rel: external
start_now_button_without_ga4_attributes:
description: By default the start now button version of this component includes a `data-ga4-attributes` attribute that is used by the specialist (external) link tracker to uniquely identify start now buttons. This attribute can be removed using the `disable_ga4` option.
data:
text: Start now
disable_ga4: true
href: '#'
start: true
rel: external
secondary_button:
data:
text: Secondary button
Expand Down
6 changes: 6 additions & 0 deletions lib/govuk_publishing_components/presenters/button_helper.rb
Expand Up @@ -28,6 +28,7 @@ class ButtonHelper
:aria_describedby

def initialize(local_assigns)
@disable_ga4 = local_assigns[:disable_ga4]
@href = local_assigns[:href]
@text = local_assigns[:text]
@title = local_assigns[:title]
Expand All @@ -45,6 +46,7 @@ def initialize(local_assigns)
@target = local_assigns[:target]
@type = local_assigns[:type]
@start = local_assigns[:start]
@data_attributes[:ga4_attributes] = ga4_attribute if start
@secondary = local_assigns[:secondary]
@secondary_quiet = local_assigns[:secondary_quiet]
@secondary_solid = local_assigns[:secondary_solid]
Expand Down Expand Up @@ -129,6 +131,10 @@ def get_margin_bottom(margin, info_text)

[*0..9].include?(margin) ? "govuk-!-margin-bottom-#{margin}" : legacy_class
end

def ga4_attribute
{ type: "start button" }.to_json unless @disable_ga4
end
end
end
end
9 changes: 9 additions & 0 deletions spec/components/button_spec.rb
Expand Up @@ -15,6 +15,7 @@ def component_name
assert_select ".govuk-button[type=submit]", text: "Submit"
assert_select ".govuk-button--start", false
assert_select ".gem-c-button__info-text", false
assert_select "[data-ga4-attributes]", false
end

it "renders text correctly" do
Expand Down Expand Up @@ -46,6 +47,14 @@ def component_name
render_component(text: "Start now", href: "#", start: true)
assert_select ".govuk-button[href='#'] span", text: /Start now/
assert_select ".govuk-button--start"
assert_select ".govuk-button--start[data-ga4-attributes='{\"type\":\"start button\"}']"
end

it "renders start now button with GA4 attributes disabled" do
render_component(text: "Start now", href: "#", start: true, disable_ga4: true)
assert_select ".govuk-button[href='#'] span", text: /Start now/
assert_select ".govuk-button--start"
assert_select ".govuk-button--start[data-ga4-attributes='{\"type\":\"start button\"}']", false
end

it "renders secondary button" do
Expand Down

0 comments on commit 70085f3

Please sign in to comment.