After updating to Drupal 10.1, the patch in #18 stopped working for me. When the url was being built, it didn't properly parse out the assets folder, so I ended up with pages linking to src="//js/google-analytics-XX"
.
In my case, I don't have an assets folder specified, but with the logic in the patch, it comes back as existing and uses the same value as the public folder. I found that a small change from $settings->get('file_assets_path')
to AssetsStream::basePath()
made the difference for me.
Otherwise the rest of the changes from r.van.doorn work great. I'm uploading my patch which is basically all of r.van.doorn's work with two lines changed, as well as an interdiff. This works for me on D10.1, and still seems to work as expected on D9.5 as well.
The patch in #18 seems to be working for me as well. Thanks very much.
Hopefully this can get picked up, as it seems to be the best way for GA4 on a site with a strong CSP.