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-data
Script Data Asset should be registered which populates a new_googlesitekitTrackingData
global- 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
$dependencies
used by all SK screens inAssets
- The tracking configuration in
assets/js/util/tracking/index.js
should be updated to source the tracking configuration values from the new tracking-specific global only- The one exception to this is
GOOGLESITEKIT_VERSION
which 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-data
to the top of the$dependencies
array list. - Add a
new Script_Data()
with thehandle
ofgooglesitekit-tracking-data
and global of_googlesitekitTrackingData
. - In the
data_callback
make a call to a new method$this->get_inline_tracking_data
which contains following properties sourced from the base and user globals bytrackEvent
:referenceSiteURL
,userIDHash
,
- The
get_inline_tracking_data
method should returnapply_filters( 'googlesitekit_inline_tracking_data', $inline_data )
. - Remove the
userIDHash
property from theget_inline_base_data()
method.
- Add
-
In
includes/Core/Authentication/Authentication.php
, add a private methodinline_js_tracking_data
with the following:- Should receive
$data
param - The
isAuthenticated
property gets data fromis_authenticated()
method. - The
userRoles
property gets data fromwp_get_current_user()->roles
method. - Return the
$data
. - In the
register
method 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_data
method toinline_js_tracking_data
. - In the
register
method 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_data
similar 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_modules
to reuse in both the hooks. -
In
assets/js/util/tracking/index.js
:- Use
global._googlesitekitTrackingData
to destructure the events instead ofglobal._googlesitekitBaseData
.
- Use
-
In
.storybook/utils/resetGlobals.js
, add all thetrackEvent
properties underglobal._googlesitekitTrackingData = {}
. -
Remove the properties we are removing from the
global._googlesitekitBaseData
object.
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
_googlesitekitTrackingData
variable.
Issue Analytics
- State:
- Created a year ago
- Comments:10
@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 ✔️