-
Notifications
You must be signed in to change notification settings - Fork 37
[PP-7355] Support Google Tag Manager #436
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
base: main
Are you sure you want to change the base?
Conversation
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
68f9f11 to
54bd8b6
Compare
54bd8b6 to
acd18ba
Compare
richardTowers
left a comment
There was a problem hiding this 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
acd18ba to
b794eb5
Compare
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
b794eb5 to
4d7dd3d
Compare
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 |
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