Add Google Tag settings to the `Analytics_4` module
See original GitHub issueWhile overall the Google Tag is intended as a generic solution for tag management, in terms of Site Kit’s modules it only applies to GA4. Since the future of the tag implementation is unclear, the infrastructure should therefore be built specifically into the GA4 module.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- The
Analytics_4\Settings
class should receive 3 new settings:googleTagID
: A string in a similar shape to a GA4 measurement ID, however it can start in either “G-”, “GT-”, or “AW-”. Default should be an empty string.googleTagAccountID
: A string which technically is a Google Tag Manager account ID. Default should be an empty string.googleTagContainerID
: A string which technically is a Google Tag Manager container ID. Default should be an empty string.
- The JS store
modules/analytics-4
should support those 3 settings as well, including validation functions.
Implementation Brief
Note that “settings mentioned in the ACs” refers to: googleTagID
, googleTagAccountID
, and googleTagContainerID
- In
includes/Modules/Analytics_4/Settings.php
:- In the array returned from the
get_owned_keys()
method, include the settings mentioned in the ACs. - In the array returned from the
get_default()
method, include the settings mentioned in the ACs as keys and empty string as the value for all. - In the
get_sanitize_callback()
method, check if the$option
array includesgoogleTagID
, and if so, check if the string value for$option['googleTagID']
starts with eitherG-
,GT-
, orAW-
. If it doesn’t, returnfalse
from the function.
- In the array returned from the
- In
assets/js/modules/analytics-4/datastore/base.js
:- In the
ownedSettingsSlugs
andsettingSlugs
arrays in thecreateModuleStore
function, include the settings mentioned in the ACs.
- In the
- In
assets/js/modules/analytics-4/utils/validation.js
:- Add a new function named
isValidGoogleTagID()
which should copy the functionality fromisValidMeasurementID()
function located in the same file, with the exception considering it can start with eitherG-
,GT-
, orAW-
. - Add a new function named
isValidGoogleTagAccountID()
which should re-export theisValidAccountID()
function located inassets/js/modules/tagmanager/util/validation.js
. - Add a new function named
isValidGoogleTagContainerID()
which should re-export theisValidContainerID()
function located inassets/js/modules/tagmanager/util/validation.js
.
- Add a new function named
- In
assets/js/modules/analytics-4/datastore/__fixtures__/default-settings.json
:- In the JSON object, include the settings mentioned in the ACs as keys and empty string as the value for all.
Test Coverage
- Add test cases for all the newly added validation functions, including those that are exported, so that if the original functions change the Google Tag ones fail (to alert the developer that only the Tag Manager functions should change).
QA Brief
- No visual changed should be introduced.
QA:Eng Brief
- Verify the
modules/analytics-4
datastore owns the settings mentioned in the issue. - Verify the
Modules/Analytics_4/Settings.php
correctly validates thegoogleTagID
- Go to Settings -> Connected Services -> Analytics Edit
- In console, set the
googleTagId
programatically.window.googlesitekit.data.dispatch('modules/analytics-4').setGoogleTagID('GT-XXX');
- Click Save
- Verify the Tag is saved in the database or reload page and verify the setting is available.
window.googlesitekit.data.select('modules/analytics-4').getGoogleTagID()
- Do the same with an invalid
googleTagID
and the other newly added settings.
Changelog entry
- Add
googleTagID
,googleTagAccountID
andgoogleTagContainerID
tomodules/analytics-4
datastore.
Issue Analytics
- State:
- Created a year ago
- Comments:12
Top Results From Across the Web
Add a Google Analytics 4 property (to a site that already has ...
Get started · In Google Analytics, click · In the Account column, make sure that your desired account is selected. · In the...
Read more >How to set up Google Analytics 4 using Google Tag Manager
Step 1: To start setting up your GA4 tag, navigate to your desired account and container within GTM and select “Add a new...
Read more >How to set up Google Analytics 4 with Google Tag Manager
The quickest way to configure your Google Tag Manager Container is to use our auto import JSON config import file for Google Tag...
Read more >How to Setup and Install Google Analytics 4 with Google Tag ...
Here's the process of how to install Google Analytics 4 with Google Tag Manager:
Read more >GA4 (Google Analytics 4) Ecommerce Tracking via GTM
The best thing to do to keep both versions of analytics active and running is to update your current setup to add GA4...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@derweili Correct – the validation functions are mostly used in other selectors and actions, that don’t exist yet but this issue is laying the foundation for. It’s good to avoid introducing things we don’t use and that’s usually what we do, although there are some issues like this where things are added ahead of time. The tests should be sufficient for ensuring they function properly for now, but you can look at some of the validation functions in other stores to see the kinds of things they will be used for.
The basic getters/setters and related parts for settings are generated automatically as part of the internals in
createModuleStore
. We have a number of these kinds of meta-programming utilities to simplify things that are otherwise highly repetitive and offer little value in hand-coding every time (we used to do this though!).Generally speaking though, we primarily validate arguments passed to actions and some selectors although these sometimes have to be a bit flexible due to selectors sometimes returning
undefined
while data is resolving as selector functions themselves are synchronous pure functions.Hope that helps – let me know if you have any other questions 👍
@wpdarren, as this only adds some settings to the analytics datastore without making use of them and without changing the existing settings, I think testing following areas should be enough: