gpt4 book ai didi

javascript - 将 Google Analytics 添加到 Chrome 扩展程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:35 26 4
gpt4 key购买 nike

为了将谷歌分析添加到 chrome 扩展中,official docs提供以下片段:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = 'https://ssl.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();

但是google也推荐to migrate from ga.js to analytics.js .

ga.js is a legacy library. If you are starting a new implementation, we recommend you use the latest version of this library, analytics.js. For existing implementations, learn how to migrate from ga.js to analytics.js.

在仔细关注 migration guide 之后并使用新脚本升级内容安全策略(从 https://ssl.google-analytics.com/ga.jshttps://www.google-analytics.com/analytics.js ),它根本不起作用,没有显示任何错误消息。

欢迎任何建议,

最佳答案

我的解决方案是基于官方文档:https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference

但稍作修改:

(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-Y', 'auto');

// Modifications:
ga('set', 'checkProtocolTask', null); // Disables file protocol checking.
ga('send', 'pageview', '/popup'); // Set page, avoiding rejection due to chrome-extension protocol

关于javascript - 将 Google Analytics 添加到 Chrome 扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com