Extract new global for tracking configuration
See original GitHub issueFeature Description
Initially, custom metadata used for Site Kit’s custom dimensions when tracking internal events all came from the _googlesitekitBaseData global which is loaded across all wp-admin (for users that can use Site Kit).
As this has evolved, it now includes data from other globals which include additional data that we wouldn’t want to load everywhere. Similarly, there is some data within the base global which is only used for tracking that we don’t need globally either.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- A new
googlesitekit-tracking-dataScript Data Asset should be registered which populates a new_googlesitekitTrackingDataglobal- The data object should contain all of the properties sourced from base and user globals by
trackEvent - Any properties which are only used for tracking may be removed from their previous objects (e.g.
userIDHash), properties with existing non-tracking uses should be duplicated (e.g.referenceSiteURL)
- The data object should contain all of the properties sourced from base and user globals by
- The new data asset should be added to the top-level list of
$dependenciesused by all SK screens inAssets - The tracking configuration in
assets/js/util/tracking/index.jsshould be updated to source the tracking configuration values from the new tracking-specific global only- The one exception to this is
GOOGLESITEKIT_VERSIONwhich is an injected top-level global
- The one exception to this is
Implementation Brief
-
In
includes/Core/Assets/Assets.php,get_assets()method:- Add
googlesitekit-tracking-datato the top of the$dependenciesarray list. - Add a
new Script_Data()with thehandleofgooglesitekit-tracking-dataand global of_googlesitekitTrackingData. - In the
data_callbackmake a call to a new method$this->get_inline_tracking_datawhich contains following properties sourced from the base and user globals bytrackEvent:referenceSiteURL,userIDHash,
- The
get_inline_tracking_datamethod should returnapply_filters( 'googlesitekit_inline_tracking_data', $inline_data ). - Remove the
userIDHashproperty from theget_inline_base_data()method.
- Add
-
In
includes/Core/Authentication/Authentication.php, add a private methodinline_js_tracking_datawith the following:- Should receive
$dataparam - The
isAuthenticatedproperty gets data fromis_authenticated()method. - The
userRolesproperty gets data fromwp_get_current_user()->rolesmethod. - Return the
$data. - In the
registermethod add aapply_filter()withadd_filter( 'googlesitekit_inline_tracking_data', $this->get_method_proxy( 'inline_js_tracking_data' ) );
- Should receive
-
In
includes/Core/Tracking/Tracking.php:- Rename the
inline_js_base_datamethod toinline_js_tracking_data. - In the
registermethod replace theapply_filter()with
add_filter( 'googlesitekit_inline_tracking_data', $this->get_method_proxy( 'inline_js_tracking_data' ) ); - Rename the
-
In
includes/Core/Modules/Modules.php, addapply_filter()forgooglesitekit_inline_tracking_datasimilar togooglesitekit_inline_base_data. See: https://github.com/google/site-kit-wp/blob/fd56e5f5fb38b18afe98828d7473e3d13c1905da/includes/Core/Modules/Modules.php#L339-L355 -
Consider creating a private method
inline_js_tracking_active_modulesto reuse in both the hooks. -
In
assets/js/util/tracking/index.js:- Use
global._googlesitekitTrackingDatato destructure the events instead ofglobal._googlesitekitBaseData.
- Use
-
In
.storybook/utils/resetGlobals.js, add all thetrackEventproperties underglobal._googlesitekitTrackingData = {}. -
Remove the properties we are removing from the
global._googlesitekitBaseDataobject.
Test Coverage
- No new tests are to be added.
QA Brief
- This issue modifies the internal code which fetches data that is passed to GA events as dimension1 to dimension7. Thus, they should be tested using the GA Debugger. Verify these dimensions are populated as before and are not changed in any way.
QA Eng
- Verify a new global window._googlesitekitTrackingData is populated as per the AC.
Changelog entry
- Move tracking related data into its own global
_googlesitekitTrackingDatavariable.
Issue Analytics
- State:
- Created a year ago
- Comments:10

Top Related StackOverflow Question
@wpdarren Testing 3-5 events should be sufficient as if it works for a few, it should work for all.
Very nice. Thanks, @hussain-t. IB ✔️