Last active
March 16, 2021 22:33
-
-
Save felixarntz/39fbb8c342ec8aaa10a06f6ed2029085 to your computer and use it in GitHub Desktop.
Example of using the Site Kit Widgets API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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