Check whether a post has Analytics data to determine whether to show admin bar menu
Current: Admin bar does not display if search console doesn't have stats.
Expected: G logo should appear in the admin bar and expand upon hover if there are stats from any modules (eg. Analytics). This should appear on the Front end and in the admin.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- In order to determine whether to display the Admin bar:
- check that there are GA stats available for that specific post
- check that both these stats are not zero
If we have a situation where SC is "gathering data", GA stats are both zero, but we still show the Admin bar, that would be rather underwhelming for users.
(this screenshot is from the main dashboard, but the stats would look similar in the Admin bar)
Implementation Brief
Analytics specific
Add action googlesitekit_show_admin_bar_menu in analytics module to check if analytics has data for the post, the same way search console calls has_data_for_post() https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Search_Console.php#L578 in this hook,
add similar method in Analytics module by requesting overview datapoint with permalink argument and save result in transient.
Update delete_all_transients() in Reset.php to remove added transient when reseting Site Kit.
Opportunity for abstraction (possibly separate ticket?)
Modules could be Admin bar aware, and able to inform the Admin Bar whether or not it is active for the current URL and thus better inform the Site Kit admin bar item if it should render or not.
- Add a new
Module_With_Admin_Barinterface- Defines a
public is_active_in_admin_bar( $url ) : boolmethod - Each module displayed in the admin bar could implement its own logic for whether or not it should be active in the admin bar
- Defines a
- Add a new
with_admin_bar() : Module[]method toModules- Returns an array of all active
Moduleinstances that are filtered to only those that implement theModule_With_Admin_Barinterface
- Returns an array of all active
- Add a new
set_modules( $modules ) : voidmethod onAdmin_Barwhere$modules : Module_With_Admin_Bar[]- After instantiating
Admin_BarinPlugin, pass the active modules to the instance
$admin_bar = ( new Core\Admin_Bar\Admin_Bar( $this->context, $assets ) )->register(); $admin_bar->set_modules( $modules->with_admin_bar() ) - After instantiating
- In
\Google\Site_Kit\Core\Admin_Bar\Admin_Bar::is_active, check if any of the provided modulesis_active_in_admin_bar( $url ) - No change would likely be necessary on the front yet, but this could be exposed in each module's JS info array if useful
Changelog entry
- Ensure admin bar displays when at least Search Console or Analytics have stats for the current URL.
@lucymtc The implementation brief should contain information on how the filter in the Analytics module behaves in relation to the Search_Console module, in regards to the following:
- If both modules have data for the post, the overall filter result should be
true. - If none of the two modules have data for the post > result
false. - If only Search Console has data for the post > result
true. - If only Analytics has data for the post > result
true.
In order to properly handle that while still keeping the filters in isolation, potentially a single centralized filter in the core Modules class would be better suited, calling the respective has_data_for_post() methods of each active module and making the necessary logic concatenation.
The implementation brief will furthermore need some adjustments after #312. We should consider this issue blocked by #312, and revisit the implementation brief once the work on that has been completed.
Moving this to "Revisit Later" as it's currently blocked by #312; since we'll want to revise the IB after #312 is completed, we should hold off on this IB so we don't write it 1.5 times 😉
@aaemnnosttv I really like the suggested abstraction, let's put that in place as part of this issue (this will clearly increase the size of this issue, but that's okay). IB ✅
This issue doesn't have super-high priority, let's just tackle it whenever we find some time for it after the critical bugs have been fixed.
Tested
Confirmed on pages without analytics data and no search console info the admin bar menu isn't displayed

Confirmed alternate scenario of no SC data but with Analytics during testing of https://github.com/google/site-kit-wp/issues/314

Passed QA ✅