尝试将 Google 分析添加到 Chrome 扩展

Trying to add Google Analytics to Chrome Extension

我浏览了几个 关于将 GA 添加到我的 chrome 扩展程序但还没有成功。

到目前为止这是我的分析代码

analytics.js

(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');
    
    ga('create', 'UA-XXXXXXX-Y', 'auto');
    ga('send', 'pageview');

当然,我已经添加了我的 GA ID.The 我不断收到的主要错误是

analytics.js:3 Refused to load the script 'https://www.google-analytics.com/analytics.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem:". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

参考 tutorial,我已将以下内容添加到我的 manifest.json manifest.json

  "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",

然而,即使将它添加到我的扩展程序中,我的开发控制台中仍然出现错误。

供参考,这是我的 index.html

中的脚本
<html lang="en">
  <head>
    <title>Polus</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="shortcut icon" type='image/png' href="images/polus_tab_icon.png"/>
    <link rel="stylesheet" type="text/css" media="screen" href="./css/calendar-style.css"/>

    <script type="text/javascript" src="./js/analytics.js"></script>

  </head>

如果可能请帮忙!

您在 manifest.json 中的内容政策声明无效,因为它指的是与 www.google-analytics.com 不同的域。此外,要使用 Universal Analytics 脚本 analytics.js,您还需要调整设置以禁用协议检查。看看这个教程https://davidsimpson.me/2014/05/27/add-googles-universal-analytics-tracking-chrome-extension/

顺便说一句 Google Analytics 4 (gta.js) 尚未实现 checkProtocolTask​​。因此,暂时使用 GA4 添加 Google 扩展是行不通的。查看 https://issuetracker.google.com/issues/174954288?pli=1 了解最新状态