Javascript 307 在 Chrome 中加载 analytics.js 时重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27945501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:59:07  来源:igfitidea点击:

307 Redirect when loading analytics.js in Chrome

javascriptgoogle-chromegoogle-analytics

提问by Benj

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.

我正在构建一个网络应用程序并使用 Google 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 中检查网络流量时,我可以看到分析脚本按预期从 Google 加载(HTTP 200 响应):

enter image description here

在此处输入图片说明

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 中运行完全相同的页面时,我收到指向 about:blank 的 HTTP 307 响应,并且分析没有运行:

enter image description here

在此处输入图片说明

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 地址栏中,则会找到该脚本。任何想法这里发生了什么,或者如何解决它?

回答by Rob W

307 Internal Redirectwith Non-Authorative-Reason: Delegateindicates that the request was intercepted and modified (redirected) by a Chrome extension via the webRequestor declarative webRequestextension APIs.

307 Internal RedirectwithNon-Authorative-Reason: Delegate表示请求被 Chrome 扩展程序通过webRequest声明性 webRequest扩展程序 API拦截和修改(重定向)。

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

您可以通过以下方式找出哪个扩展触发了重定向:

  1. Visit chrome://net-internals/#events
  2. Trigger the request (google analytics, in your case).
  3. Go back to the chrome://net-internals/#eventstab and look for a URL_REQUEST matching your request (you can use the searchbox to filter the search).
  4. 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:
  1. 访问 chrome://net-internals/#events
  2. 触发请求(谷歌分析,在你的情况下)。
  3. 返回chrome://net-internals/#events选项卡并查找与您的请求匹配的 URL_REQUEST(您可以使用搜索框来过滤搜索)。
  4. 单击条目以在右侧显示日志。您将看到有关请求的扩展名、扩展 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://extensionsand disable or remove the extension that modified the request.

在此日志示例中,名称为“[Name of extension]”且扩展 ID 为“ebmlimjkpnhckbaejoagnjlgcdhdnjlb”的扩展重定向了请求。找到扩展名和/或 ID 后,您可以访问chrome://extensions并禁用或删除修改请求的扩展。

回答by Boaz - Reinstate Monica

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.jsto //www.google-analytics.com/analytics.js.

就我而言,307 重定向的原因更为平淡。出于使用协议相对 URL的习惯,我从 Google 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');

(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 =克; m.parentNode.insertBefore(A,M)})(窗口,文件, '脚本',” HTTPS://www.google-analytics.com/analytics .js','ga');

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:

这是不可取的,因为 Google 显然仅通过 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:// 资产。

回答by John D.

In my case, I have UBlock Origin activated on my browser. Once disconnected or site authorised, the internal redirections have stopped

就我而言,我在浏览器上激活了 UBlock Origin。一旦断开连接或站点授权,内部重定向已停止