如何在Chrome扩展程序中使用Google Analytics(分析)? [英] How to use Google Analytics in Chrome Extension?
问题描述
This official tutorial seems outdated because ga.js is deprecated according to this.
此外,在本教程中它具有: _gaq.push(['_ setAccount','UA-XXXXXXXX-X']);
但 UA-XXX-X
是财产ID而不是帐户ID.
Besides, in the tutorial it has: _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
but UA-XXX-X
is property ID not account ID.
最新版本似乎是 analytics.js
我尝试了此解决方案,但是它不适用于我.我看到错误了
I tried this solution but it doesn't work for me. I saw error
拒绝加载脚本"https://www.google-analytics.com/analytics.js",因为它违反了以下内容安全政策指令:"script-src'self'https://www.googletagmanager.com".请注意,未明确设置"script-src-elem",因此将"script-src"用作备用.
Refused to load the script 'https://www.google-analytics.com/analytics.js' because it violates the following Content Security Policy directive: "script-src 'self' https://www.googletagmanager.com". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
我在 popup.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-XXXXX-X", "auto");
ga('set', 'checkProtocolTask', null); // Disables file protocol checking.
ga('send', 'pageview', '/popup'); // Set page, avoiding rejection due to chrome-extension protocol
在 manifest.json
中,我有
"content_security_policy": "script-src 'self' https://www.googletagmanager.com; object-src 'self'"
- 在Chrome扩展程序中使用Google Analytics(分析)的最新正确方法是什么?
- 我应在哪个网址中输入"Web URL"在Google Analytics(分析)中创建新媒体资源时输入字段?
推荐答案
一种解决方案是删除CSP应该提供的保护,但是您必须在GTM的便利性或通过CSP的安全性之间进行选择,您可以在此处找到有关此内容的文章: https://www.simoahava.com/analytics/google-tag-manager-content-security-policy/
A solution solution would be to remove the protection your CSP is supposed to offer, however you have to choose between the convenience of GTM or the security via a CSP, you can find an article about that here: https://www.simoahava.com/analytics/google-tag-manager-content-security-policy/
这篇关于如何在Chrome扩展程序中使用Google Analytics(分析)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!