Skip to content

Commit

Permalink
Step nav enable GA4 by default
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Nov 15, 2023
1 parent aeadb42 commit 9672b5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

step_nav_helper = GovukPublishingComponents::Presenters::StepByStepNavHelper.new

ga4_tracking ||= false
disable_ga4 ||= false
%>
<% if steps %>
<div
data-module="gemstepnav<% if ga4_tracking %> ga4-event-tracker<% end %>"
<%= "data-ga4-expandable" if ga4_tracking %>
data-module="gemstepnav<% unless disable_ga4 %> ga4-event-tracker<% end %>"
<%= "data-ga4-expandable" unless disable_ga4 %>
class="gem-c-step-nav js-hidden <% if small %>govuk-!-display-none-print<% end %> <% unless small %>gem-c-step-nav--large<% end %>"
<%= "data-remember" if remember_last_step %>
<%= "data-id=#{tracking_id}" if tracking_id %>
Expand Down Expand Up @@ -76,7 +76,7 @@
<%
ga4_link_data = {}

if ga4_tracking
unless disable_ga4
ga4_link_data = {
"module": "ga4-link-tracker",
"ga4-track-links-only": "",
Expand All @@ -99,7 +99,7 @@
options[:step_title] = step[:title]
options[:step_index] = step_index
options[:link_index] = 0
options[:ga4_tracking] = ga4_tracking
options[:disable_ga4] = disable_ga4
%>
<% step[:contents].each do |element| %>
<%= step_nav_helper.render_step_nav_element(element, options) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ examples:
]
}
]
with_google_analytics_4_tracking:
without_google_analytics_4_tracking:
description: |
The ga4_tracking boolean allows you to add Google Analytics 4 (GA4) tracking to your component.
Setting this to true will add the `ga4-event-tracker` module to your component. The JavaScript will then add a `data-ga4-event` attribute and `data-ga4-expandable` attribute to the "Show all steps" button, and each clickable Step heading.
GA4 will then track these elements when they are expanded or collapsed.
`data-ga4-event` contains a JSON with event data relevant to our tracking. `data-ga4-expandable` enables the value of `aria-expanded` to be captured.
See the [ga4-event-tracker](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-ga4/ga4-event-tracker.md) docs for more information.
Links are also tracked with the [ga4-link-tracker](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-ga4/ga4-link-tracker.md).
Therefore the `ga4-link-tracker` data module also exists on these step by step components. Any links will contain a data attribute of `ga4-link`, which contains the relevant tracking data as a JSON.
Disables GA4 tracking on the component. Tracking is enabled by default. This includes the [ga4-event-tracker](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-ga4/ga4-event-tracker.md) module as well as `data-ga4-event` and `data-ga4-expandable` attributes on the "Show all steps" button, and each clickable Step heading. `data-ga4-event` contains JSON with event data relevant to tracking. `data-ga4-expandable` enables the value of `aria-expanded` to be captured.
Links within steps are tracked with the [ga4-link-tracker](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-ga4/ga4-link-tracker.md). Links are given a data attribute of `ga4-link`, which contains the relevant tracking data as JSON.
data:
ga4_tracking: true
disable_ga4: true
steps: [
{
title: "Do something",
Expand Down Expand Up @@ -128,7 +124,7 @@ examples:
]
with_unique_tracking:
description: |
In order to identify the step by step navigation the component is part of, we need to track a unique ID of the navigation in Google Analytics. This ID should be the same across all pages that are linked from and are part of that navigation. Its value is included in any tracking events, specifically in dimension96. It refers to the ID of the step nav page.
In order to identify the step by step navigation the component is part of, we need to track a unique ID of the navigation in Universal Analytics. This ID should be the same across all pages that are linked from and are part of that navigation. Its value is included in any tracking events, specifically in dimension96. It refers to the ID of the step nav page.
This includes show/hide all, show/hide step and any link clicks.
data:
Expand Down Expand Up @@ -531,7 +527,7 @@ examples:
No change. Active step by step will be expanded.
4. Content item is part of multiple primary step by steps and a single secondary step by step
4. Content item is part of multiple primary step by steps and a single secondary step by step
No change. Show "Part of" step by steps list. Secondary step by step won't be shown.
Expand Down
13 changes: 10 additions & 3 deletions spec/components/step_by_step_nav_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,19 @@ def stepnav
end

it "adds ga4 attributes" do
render_component(steps: stepnav, ga4_tracking: true)
render_component(steps: stepnav)

assert_select ".gem-c-step-nav[data-ga4-expandable]"
assert_select ".gem-c-step-nav[data-module='gemstepnav ga4-event-tracker']"
end

it "allows GA4 to be disabled" do
render_component(steps: stepnav, disable_ga4: true)

assert_select ".gem-c-step-nav[data-ga4-expandable]", false
assert_select ".gem-c-step-nav[data-module='gemstepnav ga4-event-tracker']", false
end

it "adds ga4 link attributes when there is one section per step" do
render_component(steps: [
{
Expand Down Expand Up @@ -291,7 +298,7 @@ def stepnav
},
],
},
], ga4_tracking: true)
])

expected = {
"event_name": "navigation",
Expand Down Expand Up @@ -323,7 +330,7 @@ def stepnav
end

it "adds ga4 link attributes when there are variable sections per step" do
render_component(steps: stepnav, ga4_tracking: true)
render_component(steps: stepnav)

expected = {
"event_name": "navigation",
Expand Down

0 comments on commit 9672b5a

Please sign in to comment.