Dashboard crashes when report doesn't contain rows
Bug Description
During testing the plugin on my local machine, I encountered a situation when Google Analytics API returns a report with an empty rows property. I suspect it happens because the selected Analytics account has no data, which is fine. What is not fine is that the DashboardSearchVisitorsWidget widget tries to access the report data rows length without checking whether rows is an actual array: https://github.com/google/site-kit-wp/blob/0c40e3a70d81d9dbe1ce22ec86762cbd753b4e2c/assets/js/modules/analytics/components/dashboard/DashboardSearchVisitorsWidget.js#L174-L182
This leads to fatal errors and the whole dashboard crashes. We need to make sure that widgets display zero states if there are no rows in the report data.
Additional details:
Report arguments
metrics[0][expression]: ga:users
metrics[0][alias]: Users
dimensions[0][name]: ga:date
dimensions[1][name]: ga:channelGrouping
startDate: 2021-08-05
endDate: 2021-09-01
dimensionFilters[ga:channelGrouping]: Organic Search
Report response
[
{
"nextPageToken": null,
"columnHeader": {
"dimensions": [
"ga:date",
"ga:channelGrouping"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "Users",
"type": "INTEGER"
}
]
}
},
"data": {
"dataLastRefreshed": null,
"isDataGolden": null,
"rowCount": null,
"samplesReadCounts": null,
"samplingSpaceSizes": null,
"totals": [
{
"values": [
"0"
]
}
]
}
}
]
Screenshots

Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Dashboard widgets should not raise fatal errors if zero rows are returned from the API
- This is possible with Analytics even when
includeEmptyRowsis enabled for a request.
- This is possible with Analytics even when
Implementation Brief
- Inside
assets/js/modules/analytics/components/dashboard/DashboardSearchVisitorsWidget.js, default thedataRowsassignment to an empty array if thesparkDataarray is empty, for example by using optional chaining:
const dataRows = sparkData?.[ 0 ]?.data?.rows || [];
- Inside
site-kit-wp/assets/js/modules/analytics/components/dashboard/DashboardGoalsWidget.js, repeat the step above to default thedataRowsassignment on line 164 to an empty array.
Test Coverage
- No new tests required.
Visual Regression Changes
- None anticipated.
QA Brief
- Similar to #3880, to recreate the issue you will need observe the Unique Visitors from Search widget and the Goals widget under the search funnel section of the dashboard in situations where their reports contain empty rows. For example:
- Zero total users, including from search
- One or more total users, but none from search
Changelog entry
- N/A
Note: this is possibly related to #3880
Thanks, @johnPhillips. Looks like the same problem exists in the DashboardGoalsWidget component. Let's take care of it too.
https://github.com/google/site-kit-wp/blob/f54254f2bf2e71d6660c4f5f3510c89027a048bd/assets/js/modules/analytics/components/dashboard/DashboardGoalsWidget.js#L164
Thanks @eugene-manuilov, good catch. I just spent a bit of time looking for any more but I think that's all of them now. I'll update the IB with this one 👍
IB :heavy_check_mark:
Note: I ended up implementing this as part of #3880 as there was some overlap with that issue. Moving to QA.
QA Update: ✅
For the Unique Visitors from Search and Goals Completed widgets:
- Can confirm that Zero total users loaded with no errors.
- Can confirm that One or more total users, but none from search loaded with no errors.
