Chrome 扩展程序的 background.html 中的 Google Analytics 跟踪 [英] Google Analytics tracking in Chrome Extension's background.html

查看:24
本文介绍了Chrome 扩展程序的 background.html 中的 Google Analytics 跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我意识到我无法直接在内容脚本中进行跟踪时.我开始使用后台 HTML 跟踪我的数据.通过使用 Google Analytics 跟踪内容脚本

When I realized that I cannot track directly in content script. I start to work with background HTML tracking my data. via Content Script tracking with Google Analytics

当我设置后台脚本时,我发现它不支持内联脚本.所以我把代码放在一个 js 文件中,并使用src=filename.js"来包含它.通过 chrome 扩展在浏览器操作中插入内容脚本

When I set up my background script, I figured out that it doesn't support inline script. So I put the code in a js file and use "src=filename.js" to include that. via chrome extension insert content script on browser action

但最后有个问题:我根本无法加载ga.js,因为它仍然违反规则.这是我得到的:

But finally there is a trouble: I cannot load ga.js at all because it still violates the rule. Here is what I got:

拒绝加载脚本https://ssl.google-analytics.com/ga.js'因为它违反了以下内容安全策略指令:"script-src 'self' chrome-extension-resource:".

Refused to load the script 'https://ssl.google-analytics.com/ga.js' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

我的扩展结构:

  1. background.html
  2. script.js
  3. tracker.js

<小时>

有关此问题的更多信息:


More information about this issue:

background.html:

<html>
<script src="tracker.js"></script>
<body></body>
</html>

tracker.js: (我隐藏我的 ID)

var _gaq = _gaq || [];
_gaq.push(['_setAccount', _gaID]);
_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);
})();

感谢您的任何帮助!

推荐答案

看起来不错.你在正确的轨道上.您只需要更新您的 manifest.json 文件以允许从 google 域下载脚本.

It looks good. You are on the right track. You just need to update your manifest.json file to allow scripts to be downloaded from the google domain.

假设您使用的是带有 manifest_version:2 的 manifest.json 文件.您应该将此行添加到清单

Assuming you are using manifest.json file with manifest_version:2. You should add this line to the manifest

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

更多信息:

https://developer.chrome.com/docs/extensions/mv2/tut_analytics/

这篇关于Chrome 扩展程序的 background.html 中的 Google Analytics 跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆