簡體   English   中英

嘗試將 Google Analytics 添加到 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。我不斷收到的主要錯誤是

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.

參考教程,我在我的清單中添加了以下內容。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 Extensions 是行不通的。 最新狀態見https://issuetracker.google.com/issues/174954288?pli=1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM