site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Dashboard crashes when report doesn't contain rows

Open eugene-manuilov opened this issue 5 years ago • 6 comments

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

2021-09-02_19-07


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 includeEmptyRows is enabled for a request.

Implementation Brief

  • Inside assets/js/modules/analytics/components/dashboard/DashboardSearchVisitorsWidget.js, default the dataRows assignment to an empty array if the sparkData array 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 the dataRows assignment 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

eugene-manuilov avatar Sep 02 '21 16:09 eugene-manuilov

Note: this is possibly related to #3880

johnPhillips avatar Sep 07 '21 11:09 johnPhillips

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

eugene-manuilov avatar Sep 07 '21 19:09 eugene-manuilov

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 👍

johnPhillips avatar Sep 08 '21 08:09 johnPhillips

IB :heavy_check_mark:

eugene-manuilov avatar Sep 08 '21 08:09 eugene-manuilov

Note: I ended up implementing this as part of #3880 as there was some overlap with that issue. Moving to QA.

johnPhillips avatar Sep 14 '21 14:09 johnPhillips

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.

image

wpdarren avatar Sep 14 '21 15:09 wpdarren