Skip to content

Commit 8b543ca

Browse files
Merge pull request #3251 from alphagov/MAIN-7345_GA4_Flash-message-events
Update `flash` partial to add GA4 data-attributes to flash messages
2 parents c1a1b0d + 02bb022 commit 8b543ca

File tree

3 files changed

+150
-9
lines changed

3 files changed

+150
-9
lines changed

app/views/shared/_flash.html.erb

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1+
<% component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns) %>
2+
13
<% [:success, :info, :warning, :danger, :notice, :alert].select { |k| flash[k].present? }.each do |k| %>
4+
<% data_attributes = {
5+
module: "ga4-auto-tracker",
6+
"ga4-auto": {
7+
event_name: "flash_#{k.to_s}",
8+
action: "#{k.to_s}_alerts",
9+
text: sanitize(flash[k]),
10+
},
11+
} %>
12+
213
<% if k == :warning || k == :danger || k == :alert %>
3-
<%= render "govuk_publishing_components/components/error_alert", {
4-
message: sanitize(flash[k]),
5-
} %>
14+
<%= tag.div(**component_helper.all_attributes) do %>
15+
<%= render "govuk_publishing_components/components/error_alert", {
16+
message: sanitize(flash[k]),
17+
data_attributes: data_attributes,
18+
} %>
19+
<% end %>
620
<% elsif k == :notice || k == :info %>
7-
<%= render "govuk_publishing_components/components/notice", {
8-
description: sanitize(flash[k]),
9-
} %>
21+
<%= tag.div(**component_helper.all_attributes) do %>
22+
<%= render "govuk_publishing_components/components/notice", {
23+
description: sanitize(flash[k]),
24+
data_attributes: data_attributes,
25+
} %>
26+
<% end %>
1027
<% elsif k == :success %>
11-
<%= render "govuk_publishing_components/components/success_alert", {
12-
message: sanitize(flash[k]),
13-
} %>
28+
<%= tag.div(**component_helper.all_attributes) do %>
29+
<%= render "govuk_publishing_components/components/success_alert", {
30+
message: sanitize(flash[k]),
31+
data_attributes: data_attributes,
32+
} %>
33+
<% end %>
1434
<% end %>
1535
<% end %>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require "integration_test_helper"
2+
require "support/ga4_test_helpers"
3+
4+
class Ga4TrackingFlashMessagesTest < JavascriptIntegrationTest
5+
include Ga4TestHelpers
6+
7+
setup do
8+
setup_users
9+
@edition = FactoryBot.create(:edition)
10+
@ready_edition = FactoryBot.create(:edition, :ready)
11+
@published_edition = FactoryBot.create(:edition, :published)
12+
end
13+
14+
context "Danger alerts" do
15+
should "push the correct values to the dataLayer when a welsh_editor attempts to navigate to the 'Create new content' page" do
16+
login_as_welsh_editor
17+
visit new_artefact_path
18+
19+
assert page.has_css?(".gem-c-error-alert")
20+
21+
event_data = get_event_data
22+
23+
assert_equal "danger_alerts", event_data[0]["action"]
24+
assert_equal "flash_danger", event_data[0]["event_name"]
25+
assert_equal "You do not have permission to see this page.", event_data[0]["text"]
26+
end
27+
28+
should "push the correct values to the dataLayer when user does not have govuk_editor permission" do
29+
login_as(@no_editor)
30+
visit history_update_important_note_edition_path(@edition)
31+
32+
assert page.has_css?(".gem-c-error-alert")
33+
34+
event_data = get_event_data
35+
36+
assert_equal "danger_alerts", event_data[0]["action"]
37+
assert_equal "flash_danger", event_data[0]["event_name"]
38+
assert_equal "You do not have correct editor permissions for this action.", event_data[0]["text"]
39+
end
40+
41+
should "push the correct values to the dataLayer on a server error" do
42+
EditionProgressor.any_instance.expects(:progress).returns(false)
43+
44+
visit send_to_2i_page_edition_path(@edition)
45+
fill_in "Comment (optional)", with: "Some comment"
46+
click_button "Send to 2i"
47+
48+
assert page.has_css?(".gem-c-error-alert")
49+
50+
event_data = get_event_data
51+
52+
assert_equal "danger_alerts", event_data[0]["action"]
53+
assert_equal "flash_danger", event_data[0]["event_name"]
54+
assert_equal "Due to a service problem, the request could not be made", event_data[0]["text"]
55+
end
56+
57+
should "push the correct values to the dataLayer when the edition is not in a valid state to be sent to 2i" do
58+
visit send_to_2i_page_edition_path(@ready_edition)
59+
fill_in "Comment (optional)", with: "Some comment"
60+
click_button "Send to 2i"
61+
62+
assert page.has_css?(".gem-c-error-alert")
63+
64+
event_data = get_event_data
65+
66+
assert_equal "danger_alerts", event_data[0]["action"]
67+
assert_equal "flash_danger", event_data[0]["event_name"]
68+
assert_equal "Edition is not in a state where it can be sent to 2i", event_data[0]["text"]
69+
end
70+
end
71+
72+
context "Warning alerts" do
73+
should "push the correct values to the dataLayer when another user has already created a new edition" do
74+
visit edition_path(@published_edition)
75+
click_on "Admin"
76+
77+
FactoryBot.create(:edition, panopticon_id: @published_edition.artefact.id)
78+
79+
click_button "Save"
80+
81+
assert page.has_css?(".gem-c-error-alert")
82+
83+
event_data = get_event_data
84+
85+
assert_equal "warning_alerts", event_data[0]["action"]
86+
assert_equal "flash_warning", event_data[0]["event_name"]
87+
assert_equal "Another person has created a newer edition", event_data[0]["text"]
88+
end
89+
end
90+
91+
context "Success alerts" do
92+
should "push the correct flash message values to the dataLayer when edition is successfully saved" do
93+
visit edition_path(@edition)
94+
fill_in "Title", with: "The new title"
95+
click_button "Save"
96+
97+
assert page.has_css?(".gem-c-success-alert")
98+
99+
event_data = get_event_data
100+
101+
assert_equal "success_alerts", event_data[0]["action"]
102+
assert_equal "flash_success", event_data[0]["event_name"]
103+
assert_equal "Edition updated successfully.", event_data[0]["text"]
104+
end
105+
106+
should "push the correct flash message values to the dataLayer when edition is successfully sent to 2i" do
107+
visit send_to_2i_page_edition_path(@edition)
108+
fill_in "Comment (optional)", with: "Some comment"
109+
click_button "Send to 2i"
110+
111+
assert page.has_css?(".gem-c-success-alert")
112+
113+
event_data = get_event_data
114+
115+
assert_equal "success_alerts", event_data[0]["action"]
116+
assert_equal "flash_success", event_data[0]["event_name"]
117+
assert_equal "Sent to 2i", event_data[0]["text"]
118+
end
119+
end
120+
end

test/integration_test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def setup_users
2323
# tests that cover the oauth interaction properly
2424
@author = FactoryBot.create(:user, :govuk_editor, name: "Author", email: "test@example.com")
2525
@reviewer = FactoryBot.create(:user, :govuk_editor, name: "Reviewer", email: "test@example.com")
26+
@no_editor = FactoryBot.create(:user, name: "No Editor", email: "test@example.com")
2627
@other = FactoryBot.create(:user, :govuk_editor, :skip_review, name: "Other", email: "test@example.com")
2728
end
2829

0 commit comments

Comments
 (0)