Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GA4 code #3224

Merged
merged 13 commits into from
Dec 13, 2023
Merged

Refactor GA4 code #3224

merged 13 commits into from
Dec 13, 2023

Conversation

andysellick
Copy link
Contributor

@andysellick andysellick commented Nov 29, 2023

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

What

Refactor some of the GA4 code in finder-frontend, based on @floehopper's previous work in #3166. This moves the logic for generating indexes for GA4 tracking on search elements (the option select and expander components) from the JS into Ruby.

Now depends upon this change in the components gem: alphagov/govuk_publishing_components#3750

Why

We want our code to be clean and readable.

Visual changes

None.

Trello card: https://trello.com/c/OdYXlYqQ/673-finder-frontend-code-optimisations

@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 09:48 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 12:20 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 12:24 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 13:59 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 13:59 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 14:08 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 14:10 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 14:18 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 14:26 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 16:25 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 29, 2023 16:30 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 November 30, 2023 09:12 Inactive
Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, though a small is change needed 👍

app/views/finders/_radio_facet.html.erb Outdated Show resolved Hide resolved
AshGDS
AshGDS previously approved these changes Dec 5, 2023
Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@andysellick
Copy link
Contributor Author

Thanks @AshGDS. I'm currently looking at expanding this PR further, which is going to involve some changes in the gem first, so I'm going to pause on this for the moment (you might want to withdrawn your approval).

@AshGDS
Copy link
Contributor

AshGDS commented Dec 5, 2023

@andysellick No worries 👍

@andysellick
Copy link
Contributor Author

@AshGDS have added some more changes to this PR. This should now fully remove the JS for event listening and setting indexes on the option select and expander component buttons. It's related to this change in the gem for the option select, so will need to be tested together and I'll figure out how to deploy these two changes simultaneously.

Haven't updated the docs yet, that's another thing to look at, but should be enough to review for now.

@andysellick andysellick changed the title [DO NOT MERGE] Refactor GA4 code Refactor GA4 code Dec 12, 2023
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 12, 2023 14:06 Inactive
@andysellick
Copy link
Contributor Author

@AshGDS I've bumped the gem in this PR to include the needed changes to the option select component. If you have time can you give this a final once over to check I've not overlooked anything?

I haven't looked at documentation yet, I've just realised... I might do that in a separate PR for the sake of simplicity.

Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

}
var buttonAttributes = this.$module.getAttribute('data-button-data-attributes')
if (buttonAttributes) {
buttonAttributes = JSON.parse(buttonAttributes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be in a try/catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout, will match the same thing in the option select. Have added.

@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 12, 2023 14:46 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 12, 2023 14:49 Inactive
- allow it to accept data attributes to be applied to the expand/collapse button
- will allow us to add GA4 tracking via a Rails/component approach and remove some JS
- likely to be a breaking change as will depend on some other stuff in a subsequent commit
- brings the component more in line with the option select component changes coming here: alphagov/govuk_publishing_components#3750
- removes the need for some of the JS around setting indexes on JS generated elements
- includes the change to the option select component, which allows us to pass data attributes for the JS generated button
Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the try/catch. I may have noticed something regarding the regex

try {
buttonAttributes = JSON.parse(buttonAttributes)
for (var rawKey in buttonAttributes) {
var key = rawKey.replace(/_/i, '-').toLowerCase()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed something with this regex which also applies to the one in govuk_publishing_components

Would /i be irrelevant here as it's capturing a symbol rather than a character, so setting it to insensitive isn't necessary?

Also, should it have the /g flag instead so that it replaces every instance of an underscore in the key? With the current regex, I think it only works if the key only has one underscore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really helpful, thanks @AshGDS. Yes I think you're right, although for readability I've gone with the replaceAll function. I've also updated the tests to reflect this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I'm happy with this I'll make the same change on the option select component in the gem as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andysellick - just to confirm, are we dropping IE11 support? If I recall correctly replaceAll doesn't work in IE11 (and it seems to not be working when I check in IE11 via BrowserStack). If we're not supporting IE11 anymore then happy to approve the change 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for spotting that, hadn't realised. Have reverted it back to replace.

@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 12, 2023 15:04 Inactive
@andysellick
Copy link
Contributor Author

@AshGDS going to hold off on merging this until tomorrow now. Feel free to take your time if you want to look over it again - it's already proved useful!

@andysellick andysellick changed the title Refactor GA4 code [DO NOT MERGE] Refactor GA4 code Dec 12, 2023
Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andysellick 👍 Looks like there's one small thing to fix in the test

@@ -131,7 +131,8 @@ describe('An expander module', function () {
ga4_expandable: '',
ga4_event: {
event_name: 'select_content',
type: 'finder'
type: 'finder',
test_attribute_with_many_underscores: 'oh yes'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be outside of ga4_event otherwise it isn't being treated as a data-attribute, so the replace code doesn't get tested against this key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this, have corrected. Have also updated the related PR alphagov/govuk_publishing_components#3767 with the same change.

@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 13, 2023 08:51 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3224 December 13, 2023 08:51 Inactive
@andysellick andysellick changed the title [DO NOT MERGE] Refactor GA4 code Refactor GA4 code Dec 13, 2023
Copy link
Contributor

@AshGDS AshGDS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@andysellick andysellick merged commit 8ccdaf6 into main Dec 13, 2023
12 checks passed
@andysellick andysellick deleted the ga4-refactor branch December 13, 2023 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants