Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Last active March 16, 2021 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixarntz/39fbb8c342ec8aaa10a06f6ed2029085 to your computer and use it in GitHub Desktop.
Save felixarntz/39fbb8c342ec8aaa10a06f6ed2029085 to your computer and use it in GitHub Desktop.
Example of using the Site Kit Widgets API
const { useSelect } = window.googlesitekit.data;
const {
registerWidget,
WIDGET_WIDTHS,
} = window.googlesitekit.widgets;
function CustomAnalyticsWidget( props ) {
const {
Widget,
WidgetNull,
WidgetActivateModuleCTA,
WidgetCompleteModuleActivationCTA,
} = props;
// If there is no Analytics module at all, do not render the widget at all.
const analyticsModule = useSelect( ( select ) => select( 'core/modules' ).getModule( 'analytics' ) );
if ( ! analyticsModule ) {
return <WidgetNull />;
}
const { active, connected } = analyticsModule;
// If the Analytics module is inactive, render the corresponding CTA for the widget.
if ( ! active ) {
return <WidgetActivateModuleCTA moduleSlug="analytics" />;
}
// If the Analytics module activation flow has not been completed yet, render the corresponding CTA for the widget.
if ( ! connected ) {
return <WidgetCompleteModuleActivationCTA moduleSlug="analytics" />;
}
// Include <Widget> wrapper because wrapWidget is false below.
return (
<Widget>
Widget content goes here.
</Widget>
);
}
window.wp.domReady( () => {
registerWidget( 'customAnalyticsWidget', {
component: CustomAnalyticsWidget,
width: WIDGET_WIDTHS.FULL,
wrapWidget: false,
}, [ 'dashboardAllTraffic' ] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment