Skip to content

Conversation

@yndajas
Copy link
Member

@yndajas yndajas commented Nov 14, 2025

What’s changed

This adds support for the Google Tag Manager (GTM) approach to Google Analytics 4

Previously #350 added Google Analytics 4 support, but though it mentioned Tag Manager, if my understanding is correct it actually added support for the non-GTM approach: gtag.js. See:

There is already a PR open to add GTM support (#187). This differs slightly: it adds a cookie banner consent step into the flow when using GTM, and doesn't include the no script (JavaScript-less) tracking (to avoid adding complexity to the consent process)

Identifying a user need

We need to use this on https://github.com/alphagov/govuk-content-api-docs to track usage

This code is specifically for the gtag.js approach to tracking with GA4.
This clarification is useful in advance of adding support for the other
approach: Google Tag Manager

https://support.google.com/tagmanager/answer/7582054
https://developers.google.com/tag-platform/tag-manager/datalayer
We already support the other approach to tracking data for Google
Analytics 4: gtag.js. We need to support the Google Tag Manager approach
to address new needs

https://support.google.com/tagmanager/answer/7582054
https://developers.google.com/tag-platform/tag-manager/datalayer
@yndajas yndajas changed the title Support Google Tag Manager [PP-7355] Support Google Tag Manager Nov 14, 2025
@yndajas yndajas marked this pull request as ready for review November 14, 2025 16:14
Copy link
Contributor

@richardTowers richardTowers left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me - one small comment inline. I think you'll also need to add this to the CHANGELOG and bump the version (unless you're planning on doing that in a separate PR?)

Once wired up, this will allow users to set their cookie preferences,
initially just for Google Tag Manager tracking

Cookie banners often link to a cookies page, but I've omitted that here
to keep the change focused. It might be good to add an option to include
this in future
The cookie banner/option to set cookie preferences will only be used
when attempting to track usage data via Google Tag Manager, at least
initially. The gem already supports the alternative gtag.js approach and
introducing a cookie banner consent step to that flow would be a
breaking change

The conditional used here is similar to one used by the gtag.js partial
The buttons on the cookie banner will now set a couple of cookies
indicating that cookie preferences have been set and what the user's
preference is regarding usage tracking

The preference cookie "cookies_policy" follows the pattern of
govuk_publishing_components: a stringified object. We only set the
`usage` property on this object for now, but the approach provides some
extensibility in case there's later a use for other preference-tracking
properties like those included in govuk_publishing_components

https://github.com/alphagov/govuk_publishing_components/blob/a35e0852f02f1fcfc1036ec2e254477e65b44e17/app/assets/javascripts/govuk_publishing_components/lib/cookie-functions.js#L7-L12

The cookie banner is now hidden by default and only shown if cookie
preferences haven't been set (in addition to the required config being
set, which is checked in the Ruby/ERB code)
We now record user cookie preferences in a cookie set by a cookie banner
when a Google Tag Manager tracking ID is set in the config. This ensures
we aren't tracking usage data without user consent when using Google Tag
Manager

Doing the same when using gtag.js (the other approach to tracking data
for Google Analytics 4) would be a breaking change, but it would be
good to start asking consent in both cases eventually
@yndajas
Copy link
Member Author

yndajas commented Nov 14, 2025

I think you'll also need to add this to the CHANGELOG and bump the version (unless you're planning on doing that in a separate PR?)

I've updated the CHANGELOG but it looks like the preference is to bump the version in a separate release PR in this repo: https://github.com/alphagov/tech-docs-gem#releasing-new-versions

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.

3 participants