Tracking: "standalone" sendAnalyticsTrackingEvent missing configuration
Bug Description
\Google\Site_Kit\Core\Util\Tracking::print_standalone_event_tracking_script defines a global sendAnalyticsTrackingEvent function in JavaScript for sending gtag events.
According to the method's docblock:
"This is used for when not on a screen owned by Site Kit."
The problem is that the main part of this function is dynamically defined in PHP like so:
<?php if ( ! empty( $track_id ) ) { ?>
if ( window.googlesitekitTrackingEnabled ) {
gtag( 'event', eventName, {
send_to: '<?php echo esc_attr( self::TRACKING_ID ); ?>',
event_category: eventCategory,
} );
}
<?php } ?>
The local variable $track_id is never provided in the given scope so this conditional will always be falsy in all cases, resulting in the actual call to gtag() not appearing in the function.
https://github.com/google/site-kit-wp/blob/9a262cd18c33995ce5ec81bc300ff055dff2a153/includes/Core/Util/Tracking.php#L177-L196
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- When on the WP dashboard or plugins screen (the latter only when the user isn't authenticated yet), the source code should contain the
sendAnalyticsTrackingEvent()function, and it should include a call to thegtag()function (wrapped in a check if tracking is enabled).
Implementation Brief
- Remove the wrapping
if ( ! empty( $track_id ) )conditional referencing the undefined$track_idvariable.
Changelog entry
- Fix tracking script being broken in WP dashboard and plugins screen due to an undefined variable PHP notice.
@felixarntz Brief ready for review
Implementation brief looking good.
@lucymtc - Could you review this one and add an estimate label ASAP? We'd like to determine whether or not this is something that could make it into this week's release (potentially replacing other tickets to do so, if needed). Thanks so much!
QA looking good!