加载分析时重定向。js在Chrome

时间:2021-12-20 14:04:38

I'm building a web app and using Google Analytics (analytics.js) for analytics. I recently noticed that analytics aren't working properly in Chrome.

我正在构建一个web应用程序,并使用谷歌Analytics (analytics.js)进行分析。我最近注意到,Chrome的分析功能并不正常。

I'm loading analytics using the standard code snippet in a separate module and included via requirejs. I've verified that this script runs as expected and executes the analytics snippet.

我将使用标准代码片段在一个单独的模块中加载分析,并通过requirejs进行加载。我已经验证了这个脚本是按照预期运行的,并执行分析代码片段。

When I inspect network traffic in Firefox, I can see that the analytics script is loaded from Google as expected (HTTP 200 response):

当我查看Firefox中的网络流量时,我可以看到分析脚本按预期从谷歌加载(HTTP 200响应):

加载分析时重定向。js在Chrome

However, when I run the exact same page in Chrome, I get an HTTP 307 response pointing to about:blank, and analytics does not run:

然而,当我在Chrome中运行完全相同的页面时,我得到了一个HTTP 307的响应,指向:blank,而分析没有运行:

加载分析时重定向。js在Chrome

However, if I paste the analytics URL directly into the Chrome address bar, the script is found. Any ideas what's going on here, or how to fix it?

但是,如果我将分析URL直接粘贴到Chrome地址栏中,就会找到这个脚本。你知道这是怎么回事吗?

2 个解决方案

#1


146  

307 Internal Redirect with Non-Authorative-Reason: Delegate indicates that the request was intercepted and modified (redirected) by a Chrome extension via the webRequest or declarative webRequest extension APIs.

307带有非授权原因的内部重定向:委托表明请求被通过webRequest或声明性webRequest扩展api的Chrome扩展拦截和修改(重定向)。

You can find out which extension triggered the redirect as follows:

您可以发现哪些扩展触发了重定向,如下所示:

  1. Visit chrome://net-internals/#events
  2. 访问chrome:/ / net-internals / #活动
  3. Trigger the request (google analytics, in your case).
  4. 触发请求(在您的例子中是谷歌analytics)。
  5. Go back to the chrome://net-internals/#events tab and look for a URL_REQUEST matching your request (you can use the searchbox to filter the search).
  6. 回到chrome://net-internals/#events选项卡,查找与您的请求匹配的URL_REQUEST(您可以使用searchbox来过滤搜索)。
  7. Click on the entry to show the log at the right side. You will see the extension name, extension ID and other information about the request:
  8. 单击条目以显示右边的日志。您将看到关于请求的扩展名、扩展名ID和其他信息:
t=7910 [st=0] +REQUEST_ALIVE  [dt=6]
t=7910 [st=0]   +URL_REQUEST_DELEGATE  [dt=5]
t=7910 [st=0]      DELEGATE_INFO  [dt=5]
                   --> delegate_info = "extension [Name of extension]"
t=7915 [st=5]      CHROME_EXTENSION_REDIRECTED_REQUEST
                   --> extension_id = "ebmlimjkpnhckbaejoagnjlgcdhdnjlb"
t=7915 [st=5]   -URL_REQUEST_DELEGATE
t=7915 [st=5]   +URL_REQUEST_START_JOB  [dt=1]
                 --> load_flags = 339804160 (BYPASS_DATA_REDUCTION_PROXY | MAYBE_USER_GESTURE | REPORT_RAW_HEADERS | VERIFY_EV_CERT)
                 --> method = "GET"
                 --> priority = "LOW"
                 --> url = "https://www.google-analytics.com/analytics.js"
t=7915 [st=5]      URL_REQUEST_REDIRECT_JOB
                   --> reason = "Delegate"
t=7915 [st=5]      URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED
                   --> HTTP/1.1 307 Internal Redirect
                       Location: about:blank
                       Non-Authoritative-Reason: Delegate

In this log sample, an extension with name "[Name of extension]" and extension ID "ebmlimjkpnhckbaejoagnjlgcdhdnjlb" redirected the request. After finding the extension name and/or ID, you can visit chrome://extensions and disable or remove the extension that modified the request.

在这个日志示例中,一个名为“(扩展名)”和扩展ID的扩展名“ebmlimjkpnhckbaejoagnjlgcdhdnjlb”重定向请求。在找到扩展名和/或ID之后,您可以访问chrome://扩展并禁用或删除修改请求的扩展。

#2


7  

In my case, the reason for the 307 redirect was more prosaic. Out of habit of using protocol-relative URLs, I've removed the protocol from the URL in the embedding script of the Google Universal Analytics, changing https://www.google-analytics.com/analytics.js to //www.google-analytics.com/analytics.js.

在我的例子中,307重定向的原因更加平淡无奇。出于使用协议相关URL的习惯,我从谷歌Universal Analytics的嵌入脚本的URL中删除了协议,将https://www.google-analytics.com/analytics.js更改为// //www.google-analytics.com/analytics.js。

For example (don't try this at home):

例如(不要在家里尝试):

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script',' https: //www.google-analytics.com/analytics.js','ga');

(函数(我,年代,o,g,r,a,m){我[' GoogleAnalyticsObject ']= r;我[r]=[r]| |函数(){(我[r]。q =[r]。q | |[]).push(参数)},我[r]。a= 1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];

This is inadvisable since Google apparently serves the script and tracking requests only over https. So removing the protocol causes a redirect both when first embedding the script as well as in any(!) subsequent tracking request. In addition, as stated by Paul Irish in an update to his canonical post about protocol-relative URLs, this technique is no longer encouraged or indeed has merit:

这是不可取的,因为谷歌显然只在https上服务脚本和跟踪请求。因此,在第一次嵌入脚本时,删除协议会导致重定向,在后续的跟踪请求中也是如此。此外,正如Paul Irish在他的关于协议相关url的权威文章中所指出的,这种技术不再受到鼓励,或者确实有价值:

Now that SSL is encouraged for everyone and doesn’t have performance concerns, this technique is now an anti-pattern. If the asset you need is available on SSL, then always use the https:// asset.

现在每个人都支持SSL,并且不关心性能问题,因此这种技术现在是一种反模式。如果您需要的资产在SSL上可用,则始终使用https:// asset。

#1


146  

307 Internal Redirect with Non-Authorative-Reason: Delegate indicates that the request was intercepted and modified (redirected) by a Chrome extension via the webRequest or declarative webRequest extension APIs.

307带有非授权原因的内部重定向:委托表明请求被通过webRequest或声明性webRequest扩展api的Chrome扩展拦截和修改(重定向)。

You can find out which extension triggered the redirect as follows:

您可以发现哪些扩展触发了重定向,如下所示:

  1. Visit chrome://net-internals/#events
  2. 访问chrome:/ / net-internals / #活动
  3. Trigger the request (google analytics, in your case).
  4. 触发请求(在您的例子中是谷歌analytics)。
  5. Go back to the chrome://net-internals/#events tab and look for a URL_REQUEST matching your request (you can use the searchbox to filter the search).
  6. 回到chrome://net-internals/#events选项卡,查找与您的请求匹配的URL_REQUEST(您可以使用searchbox来过滤搜索)。
  7. Click on the entry to show the log at the right side. You will see the extension name, extension ID and other information about the request:
  8. 单击条目以显示右边的日志。您将看到关于请求的扩展名、扩展名ID和其他信息:
t=7910 [st=0] +REQUEST_ALIVE  [dt=6]
t=7910 [st=0]   +URL_REQUEST_DELEGATE  [dt=5]
t=7910 [st=0]      DELEGATE_INFO  [dt=5]
                   --> delegate_info = "extension [Name of extension]"
t=7915 [st=5]      CHROME_EXTENSION_REDIRECTED_REQUEST
                   --> extension_id = "ebmlimjkpnhckbaejoagnjlgcdhdnjlb"
t=7915 [st=5]   -URL_REQUEST_DELEGATE
t=7915 [st=5]   +URL_REQUEST_START_JOB  [dt=1]
                 --> load_flags = 339804160 (BYPASS_DATA_REDUCTION_PROXY | MAYBE_USER_GESTURE | REPORT_RAW_HEADERS | VERIFY_EV_CERT)
                 --> method = "GET"
                 --> priority = "LOW"
                 --> url = "https://www.google-analytics.com/analytics.js"
t=7915 [st=5]      URL_REQUEST_REDIRECT_JOB
                   --> reason = "Delegate"
t=7915 [st=5]      URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED
                   --> HTTP/1.1 307 Internal Redirect
                       Location: about:blank
                       Non-Authoritative-Reason: Delegate

In this log sample, an extension with name "[Name of extension]" and extension ID "ebmlimjkpnhckbaejoagnjlgcdhdnjlb" redirected the request. After finding the extension name and/or ID, you can visit chrome://extensions and disable or remove the extension that modified the request.

在这个日志示例中,一个名为“(扩展名)”和扩展ID的扩展名“ebmlimjkpnhckbaejoagnjlgcdhdnjlb”重定向请求。在找到扩展名和/或ID之后,您可以访问chrome://扩展并禁用或删除修改请求的扩展。

#2


7  

In my case, the reason for the 307 redirect was more prosaic. Out of habit of using protocol-relative URLs, I've removed the protocol from the URL in the embedding script of the Google Universal Analytics, changing https://www.google-analytics.com/analytics.js to //www.google-analytics.com/analytics.js.

在我的例子中,307重定向的原因更加平淡无奇。出于使用协议相关URL的习惯,我从谷歌Universal Analytics的嵌入脚本的URL中删除了协议,将https://www.google-analytics.com/analytics.js更改为// //www.google-analytics.com/analytics.js。

For example (don't try this at home):

例如(不要在家里尝试):

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script',' https: //www.google-analytics.com/analytics.js','ga');

(函数(我,年代,o,g,r,a,m){我[' GoogleAnalyticsObject ']= r;我[r]=[r]| |函数(){(我[r]。q =[r]。q | |[]).push(参数)},我[r]。a= 1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];

This is inadvisable since Google apparently serves the script and tracking requests only over https. So removing the protocol causes a redirect both when first embedding the script as well as in any(!) subsequent tracking request. In addition, as stated by Paul Irish in an update to his canonical post about protocol-relative URLs, this technique is no longer encouraged or indeed has merit:

这是不可取的,因为谷歌显然只在https上服务脚本和跟踪请求。因此,在第一次嵌入脚本时,删除协议会导致重定向,在后续的跟踪请求中也是如此。此外,正如Paul Irish在他的关于协议相关url的权威文章中所指出的,这种技术不再受到鼓励,或者确实有价值:

Now that SSL is encouraged for everyone and doesn’t have performance concerns, this technique is now an anti-pattern. If the asset you need is available on SSL, then always use the https:// asset.

现在每个人都支持SSL,并且不关心性能问题,因此这种技术现在是一种反模式。如果您需要的资产在SSL上可用,则始终使用https:// asset。