Make filtering Analytics options available to developers
Feature Description
Introduce a filter for analytics parameters on the FE.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- There is already a
googlesitekit_gtag_optfilter for the regular Analytics configuration options in https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Analytics.php#L276. That filter is sufficient for this part, however its doc-block should clarify that it filters configuration options for the Analytics snippet. Not as many people understand what "gtag" is right away (at the same time, the doc-block should continue to mention that this is for gtag, to differentiate it from other Analytics snippet types people might be familiar with). - To cover AMP, an additional
googlesitekit_amp_gtag_optfilter should be introduced, for the object that is passed toamp-analyticsas JSON in the Analytics module. The docs should have a@seeannotation pointing to https://developers.google.com/gtagjs/devguide/amp. - A key requirement for the AMP filter is that the tracking ID itself must not be changed (for the non-AMP filter, the tracking ID isn't part of the options anyway). In order to ensure that, logic should be added after running the filter that resets the tracking ID value to the original one (in case someone tried to change it). All other properties should be freely customizable.
Implementation brief
-
Update doc-block for
googlesitekit_gtag_optfilter clarifying that it filters configuration options for the Analytics snippet. Keep mention that is for gtag. https://github.com/google/site-kit-wp/blob/4c691659af7d975205be2957e2e7d36b26f9e8e7/includes/Modules/Analytics.php#L268 -
Add filter
googlesitekit_amp_gtag_optto make it available for developers to modify the$configoptions https://github.com/google/site-kit-wp/blob/4c691659af7d975205be2957e2e7d36b26f9e8e7/includes/Modules/Analytics.php#L317 -
Add @see annotation in the doc-block of the filter pointing to https://developers.google.com/gtagjs/devguide/amp
-
After running the filter ensure
$config['vars']['gtag_id']exists and set it to the original$tracking_id
Changelog entry
- Allow filtering the Google Analytics
gtagconfiguration options for both AMP and non-AMP context.
@lucymtc The implementation brief for the AMP implementation misses a point (apologies if that didn't become clear from the acceptance criteria):
- Not just the
vars.configproperty in the object should be filtered, but the entire object needs to be filtered. The reason is that there are other top-level properties thanvarsthat developers may want to update. - After running the filter, it has to be ensured that
vars.gtag_idstill exists and is set to the original tracking ID.
Implementation brief looks good!
QA is good!