Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine Google's Global Site Tag with Subresource Integrity?

I'm implementing global tracking on my site following the steps described by Google. But I want to also keep my Subresource Integrity (SRI) up to date. So I ran the following command to find the integrity hash for gtag.js.

> curl -s https://www.googletagmanager.com/gtag/js |\
  openssl dgst -sha384 -binary |\
  openssl base64 -A

Adding this as the integrity attribute to the script tag with the crossorigin="anonymous" attribute, causes the browser to fail loading the gtag script. Reason:

Subresource Integrity: The resource 'https://www.googletagmanager.com/gtag/js' has an integrity attribute, but the resource requires the request to be CORS enabled to check the integrity, and it is not. The resource has been blocked because the integrity cannot be enforced.

Apparent reason is the access-control-allow-origin header which google returns and only allows the same-host origin.

Does anyone know if there is a different host for this script? Is there another way to adopt gtag in your site?

like image 405
Roald Bankras Avatar asked Jun 30 '18 11:06

Roald Bankras


People also ask

Can you use GTAG and Google Tag Manager?

Google Ads and Google Marketing Platform tags are fully supported by Tag Manager, and there is no need to deploy additional gtag. js-based code on your site if Tag Manager is already in use. If you're already using gtag. js, you can always upgrade to Tag Manager at a later date.

Is Global site tag the same as Google Analytics?

The Google tag uses the gtag. js JavaScript library to send data to Google Analytics. In addition to the Google tag, you can use Google Tag Manager to send data to Google Analytics. The Google tag was previously referred to as the global site tag.

Where do I put global site tags on my website?

A global site tag is made up of two snippets of JavaScript: a global snippet and an event snippet. Insert the global site tag between the <head> and </head> tags on every page of your site. The global snippet is placed on all pages, and the event snippet is additionally placed on pages with events you're tracking.

Where do I put the tag code on Google sites?

Place the <script> code snippet in the <head> of your web page's HTML output, preferably as close to the opening <head> tag as possible, but below any dataLayer declarations.


1 Answers

The only workaround for this may be to simply fetch the resource, store it on the same server, and then set your own Access-Control-Allow-Origin header.

This is a bit silly - because not only does it reduce the value of distributing the resource via CDN, it effectively makes the SRI pointless (because you're working with a static local copy, not a remote one the warrants reducing the risk of manipulation by verifying it with SRI).

This will also only work as long as the resource remains static. But since SRI depends on the resource being static (so that the hash will match), any change to the resource on the server would result in SRI enforcement causing the resource to fail anyway.

So it may be no more fragile than using SRI in the first place (unless you're generating the checksum of the resource on the fly ... at which point there's less of a point in using SRI in the first place).

UPDATE 2020-09-22: It looks like the issue may have been resolved on the Google side in April 2020.

like image 59
Royce Williams Avatar answered Oct 05 '22 23:10

Royce Williams