Site Kit encountered an error: Error: Failed to construct ‘URL’: Invalid URL
Bug Description
After updating to WordPress 5.8 @westonruter is encountering the following dashboard error appeared when accessing the the primary Site Kit screen. (WP support forum topic)
Site Kit encountered an error
Failed to construct 'URL': Invalid URL
in DetailsPermaLinks
in Component
in div
in td
in tr
in tbody
in table
in div
in ReportTable
in div
in div
in TableOverflowContainer
in div
in div
in Widget
in WithWidgetSlug(Widget)
in DashboardPopularPagesWidget
in WhenAnalyticsActive(DashboardPopularPagesWidget)
in WidgetRenderer
in div
in Cell
in WidgetCellWrapper
in div
in Row
in div
in div
in Grid
in WidgetAreaRenderer
in div
in WidgetContextRenderer
in DashboardApp
in GoogleSitekitDashboard
in RestoreSnapshots
in ErrorHandler
in Root
This error remans when revisiting the Site Kit Summary dashboard widget.
One possible cause could be additional plugins using the Google Charts library, similar to other JS errors as per #3132.
We did encounter the same error previously in support (#1855) although this occurred for the impacted using during AdSense setup, with no further details provided or instances reported.
There are also references in the Lighthouse repository of the same specific error although as @westonruter pointed out the stack trace points includes DashboardPopularPagesWidget and WhenAnalyticsActive references.
Additional Context
- Site Kit 1.37.0.
- Site Health information provided
- Standard mode AMP website
- Unreproducible in support when upgrading to 5.8 on a standard AMP site (without using all the same plugins)
- Support forum topic
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Site Kit calls instantiating a
new URLin JS should be wrapped in a "try-catch" block if the value may not be a valid URL.- In case of an error an appropriate fallback should be used, e.g. an empty string or
false/null.
- In case of an error an appropriate fallback should be used, e.g. an empty string or
- While it would technically be safest to do it everywhere in production code, we should at least do it in instances where the value passed to it does not come from within Site Kit, i.e. it comes from a Google API.
- We can reliably assume that e.g. a
siteURLvalue from Site Kit orlocation.hreffrom the browser are valid URLs/paths. - Any URL checked with
isURLfrom@wordpress/urlis also valid because it does the same check internally - However we cannot reliably assume that all values from external APIs like Search Console, Analytics, or AdSense are valid URLs/paths.
- We can reliably assume that e.g. a
Implementation Brief
The getURLPath and getFullURL utilities inside assets/js/util/urls.js need to be refactored as follows.
getURLPath
- Wrap the call to
new URLin atry-catchblock - Inside the
catch, returnnull - Inside each consumer, refactor the logic at the
getURLPathcallsite, so that if it is falsey thepathis not appended to the report arguments. For example, inDashboardAllTrafficWidget:
You'll need to update the following files:// Before: if ( entityURL ) { reportArgs[ 'explorer-table.plotKeys' ] = '[]'; reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ getURLPath( entityURL ) }`; } // After: const path = !! entityURL && getURLPath( entityURL ); if ( path ) { reportArgs[ 'explorer-table.plotKeys' ] = '[]'; reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ path }`; }assets/js/modules/analytics/components/dashboard/DashboardBounceRateWidget.js 80: ? `analytics.pagePath:${ getURLPath( url ) }` assets/js/modules/analytics/components/dashboard/DashboardAllTrafficWidget/index.js 188: reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ getURLPath( assets/js/modules/analytics/components/dashboard/DashboardSearchVisitorsWidget.js 101: drilldowns.push( `analytics.pagePath:${ getURLPath( url ) }` ); - Update the unit tests for this function
getFullURL
- Wrap the call to
new URLin atry-catchblock - Inside the
catch, return a string which is a concatenation of the two params, i.e.siteURL+path - Update the unit tests for this function
Test Coverage
- Unit tests for the two utilities should be updated as described above.
Visual Regression Changes
- None anticipated.
QA Brief
- Check
getURLPathandgetFullURLfunctions to make sure that they wrapnew URL(...)calls withtry/catchblock. - Check
getURLPathfunction usage to verify that all calls account for possible NULL results.
Changelog entry
- Fix a potential error due to report data associated with an invalid URL.
Disconnecting Analytics did stop the error from happening. However, once I reconnected Analytics then the error returned.
Also, I did try using Health Check & Troubleshooting to isolate Site Kit as the only active plugin, but the error persisted.
I tried clearing the persistent object cache but the issue still occurs.
Interesting that the same occurs once reconnecting Analytics. Can you confirm if the same occurs if you select the option to create a new Analytics account as opposed to selecting a pre-existing Analytics account and property?
In the meantime I'll do some additional checks on my side.
@westonruter Are there any odd-looking URL values in your Analytics top pages when checking your Analytics data? Maybe we are missing a scenario there and need more “forgiving” logic. (copied from similar wp.org forum reply)
Unable to recreate this issue & performed following activities:
- Installed Site Kit 1.35.0
- Configured Search Console with the newly created Google account
- Setup Google Analytics
- Updated Site Kit to latest version 1.37.0
Are there any odd-looking URL values in your Analytics top pages when checking your Analytics data?
Yes, there are:
I am using the analytics snippet as provided by Site Kit. I'm seeing double trailing slashes for some reason. Perhaps that's the cause of the JS error?
@westonruter Yes that looks like the cause of the error. Curious how the URLs come up though. Are all of them with double slashes? Has that only started happening recently?
To be honest, I haven't looked at my analytics data in a long time so I don't know if it's a recent occurrence. Nevertheless, all of the URLs appear with the double slashes at the end of the path.
@felixarntz Just checking for an update here.
@bethanylang @aaemnnosttv @westonruter I've defined the ACs for this issue around fixing the JS error, where our code should be more defensively written. However, the weird situation with double slashes would still be a problem - however, I don't think we should add logic to specifically fix double slashes, since that would open a can of worms around resolving any sort of potential weirdness with URLs stored in Analytics. Any idea why those double-slashed URLs could happen? I can't imagine how that is caused by Site Kit since we merely inject the Analytics snippet 🤔
@johnPhillips Just one really minor thing: is there any reason not to return null instead of false? Returning a boolean here just… seems a bit wrong. The idea that the input can't create a URL and thus returns null seems to make a bit more semantic sense to be than it returning false.
Other than that this looks good, just let me know about the return value—if we can change it to null that'd be grand, and then this can go into the backlog 👍🏻
@tofumatt I think I chose false originally because of the AC, but I agree that null would be the better choice. I've updated the IB accordingly.
Ah, fair enough, I missed that. Since it wasn't particular about the value I've added null, as the intent is just to return something indicating the supplied URL wasn't usable.
IB ✅
QA ✅
- Check getURLPath and getFullURL functions to make sure that they wrap new URL(...) calls with try/catch block. ✅
- Check getURLPath function usage to verify that all calls account for possible NULL results. ✅
I can confirm the issue is fixed in terms of the error. I can access the Site Kit dashboard now.
Nevertheless, I still do see the doubled trailing slashes in the URLs. But that seems like an analytics configuration problem. This seems like a Site Kit concern though, as I'm using the analytics code as added by Site Kit.
Hi @westonruter. Could you please create a screenshot of your issue and start a new ticket for it referencing this one? It will help us to figure out the root of the problem and fix it.
@eugene-manuilov Here you go: https://github.com/google/site-kit-wp/issues/4036

