site-kit-wp: WebFont global existing causes Google Charts with custom fontName to throw exception `Cannot read properties of null (reading '__eventTarget')`

Bug Description

We’ve had 3 reports of the following error after updating to the latest version of the plugin, 1.79.0 Cannot read properties of null (reading '__eventTarget')

This appears as a dashboard error, with more details below:

Cannot read properties of null (reading ‘__eventTarget’)

    in GoogleChart
    in div
    in div
    in UserDimensionsPieChart
    in div
    in Cell
    in div
    in ForwardRef
    in div
    in ForwardRef
    in div
    in div
    in Widget
    in WithWidgetSlug(Widget)
    in DashboardAllTrafficWidget
    in WhenAnalyticsActive(DashboardAllTrafficWidget)
    in WidgetRenderer
    in div
    in Cell
    in WidgetCellWrapper
    in div
    in ForwardRef
    in div
    in div
    in ForwardRef
    in WidgetAreaRenderer
    in div
    in WidgetContextRenderer
    in DashboardMainApp
    in DashboardEntryPoint
    in RestoreSnapshots
    in ErrorHandler
    in StrictMode
    in Root
Console Errors
TypeError: Cannot read properties of null (reading ‘__eventTarget’)
at gvjs_Zm (jsapi_compiled_default_module.js:412:29)
at Object.gvjs_Ym [as addListener] (jsapi_compiled_default_module.js:411:392)
at googlesitekit-modules-search-console-227f86b858fbc87c820e.js:31:11241
at Ui (googlesitekit-vendor-4167c42429c777851281.js:7:724946)
at va (googlesitekit-vendor-4167c42429c777851281.js:7:747869)
at t.unstable_runWithPriority (googlesitekit-vendor-4167c42429c777851281.js:7:767492)
at Mr (googlesitekit-vendor-4167c42429c777851281.js:7:692598)
at Yj (googlesitekit-vendor-4167c42429c777851281.js:7:744298)
at Lj (googlesitekit-vendor-4167c42429c777851281.js:7:734658)
at googlesitekit-vendor-4167c42429c777851281.js:7:692889

Steps to reproduce

The below are the only known steps to consistently recreate so far:

  1. Set up a new WP site, with SK set up and Google Analytics also connected
  2. Install and activate the Futurio Extra plugin
  3. Install and activate the Futurio theme
  4. Install one of the theme demos (Customizer > One Click Demo Import)
  5. After importing, revisit your SK dashboard. The error appears. If the error doesn’t appear, change the reporting period, or wait until cached response expire.

Note that upon refreshing the page with the error the dashboards loaded as expected in my case, as the reporting period switches back to the 28 day report, with the dashboard loading fine for this period

Screenshots

https://user-images.githubusercontent.com/41326532/179794406-4017eb35-7353-4011-a744-4ca2bda4bab8.mp4

image

Additional Context

  • Occurred after updating to SK 1.79.0
  • The same occurs if uninstalling and reinstalling the plugin
  • On a site where I could reproduce this error the only network requests on the Site Kit dashboard coming from the plugins highlighted above (in the steps to recreate) are web font related (example).

Impacted Users

Insights & Troubleshooting Checks performed

  • Health Check & Troubleshooting plugin check
  • Front end check (mixed content, minified content, combined JS)
  • Incognito mode
  • Checked users Site Health info, no identified causes
  • Uninstalled and reinstalled plugin
  • User confirmed no ““Critical errors”” in their Site Health status
  • Attempted setup/viewing dashboards from another device
  • Temporary disabled WAF rules at host level - including ModSecurity
  • Checked site setup and dashboard from another WordPress site on the same hosting platform
  • Reset Site Kit
  • Reviewed error logs
  • Reviewed XHR requests

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • When the Google Web Font loader script is loaded (eg. when the WebFont JS global exists), Google Charts components should not throw an exception (causing the entire Dashboard to crash/error).

Implementation Brief

  • Do not use the fontName (eg. don’t supply a custom fontName property) to any react-google-charts component when the WebFont global exists in JS.
  • Merge #5580.

Test Coverage

  • None needed.

QA Brief

  • Follow the steps to reproduce above, or use one of the PHP blocks in the first section of this comment: https://github.com/google/site-kit-wp/issues/5572#issuecomment-1190433825 in a custom plugin/functions.php file on a site with Analytics setup (and with Analytics data).
  • There should be no error on the dashboard when using the latest main/1.79.1. To verify the fix, make sure you can replicate the issue on the same site using Site Kit version 1.79.0.

Changelog entry

  • Fix an issue that could cause Site Kit’s dashboard to crash when a theme/plugin loads the Google Web Font Loader JS on Site Kit screens.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21

Most upvoted comments

Looks like this is a compatibility issue with the Web Font Loader library.

The following is a snippet that can help reproduce the issue on your local machine even without futurio plugin and theme:

add_action( 'admin_footer', function() {
    ?><script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
    <script>
        WebFont.load({
            google: {
                families: ['Droid Sans', 'Droid Serif']
            }
        });
    </script><?php
} );

QA ❌

@tofumatt while testing, we found that it is still possible to raise the same error while toggling the date range. I wasn’t able to reproduce this at first but was able to eventually so this does not seem to happen consistently. Perhaps there is a bit of a race condition going on but this warrants a bit more testing and exploration.

So, this all seems to stem from the WebFont global being written/available when React Google Charts tries to load a custom font for certain elements like the text in a pie chart slice. I think it only happens for content it renders inside an SVG, where we need to specify a custom font rather than it just relying on the plugin-wide fonts set here: https://github.com/google/site-kit-wp/blob/24e6a6368ce49d8eab433a052d1006c5aabe1e7a/assets/sass/config/_variables.scss#L242-L243

This can be seen with Eugene’s code above; even just using:

add_action( 'admin_footer', function() {
    ?>
    <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
    <?php
} );

will cause the bug to occur. In fact, even:

add_action( 'admin_footer', function() {
    ?>
    <script>window.WebFont = { load: console.log }</script>
    <?php
} );

causes the bug.


This only happens when another plugin/theme/etc. loads the Google WebFont loader, possible another version, and only affects a few, small elements rendered by our Charts. I am totally unaware of the actual root cause because the errors are opaque and all of this code is third-party and a bit of a black box. But I have found a reliable fix that only needs to be used when the WebFont global is loaded and it causes minimal issue.

I think the best fix is to load those web fonts in Google Chart components as such:

// Instead of this:
textStyle: {
	fontName: '"Google Sans Text", "Helvetica Neue", Helvetica, Arial, sans-serif',
	color: '#5f6561',
	fontSize: 10,
}

// Use this:
textStyle: {
	fontName:
		global?.WebFont?.load === undefined
			? '"Google Sans Text", "Helvetica Neue", Helvetica, Arial, sans-serif'
			: undefined,
	color: '#5f6561',
	fontSize: 10,
}

It fixes the problem and only causes a few, small items to be rendered using Arial instead of Google Sans. Here’s the different for the UserDimensionsPieChart.

Google Sans

CleanShot 2022-07-20 at 16 30 11

Arial

CleanShot 2022-07-20 at 16 31 00

It’s not Analytics but the React Google Charts library, or our usage of it. Enabling Analytics renders the Google Charts PieChart with fontName: 'Google Sans Text' which is what’s causing the error.

I’m still trying to figure out a cause, but as mentioned it might be best to disable custom fonts in our charts for now…

@tofumatt could you please help to troubleshoot this fonts issue? Assigning this ticket to you for now.

@jamesozzie Thanks for all that information. I can reproduce this issue locally and on an InstaWP site. Locally, it happens even when selecting the 7 days period - although on the InstaWP site it only occurred for 14 or 90 day periods. When I comment out the latest additions to GoogleChart, I do not get the error so this has been introduced in the last release. Not sure why those lines are an issue - haven’t dug deeper. Also not sure why this happens only with the install of Futurio either at this stage. But worth moving this to the Execution board as a bug to investigate further I guess?

I couldn’t reproduce the Data Errors - Invalid Credentials errors.

Just a quick comment that I’ve added steps to reproduce this within the main content of this issue, where I could recreate this with the Futurio theme and plugin. In my case the error appears on the 14 day or 90 day reporting period only. Testing ongoing.