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

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

问题描述

当我意识到我无法直接在内容脚本中追踪。我开始使用后台HTML跟踪我的数据。通过使用Google Analytics(分析)跟踪内容脚本



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



但最后还是有一个麻烦:我完全无法加载ga.js,因为它仍然违反了规则。这是我得到的:


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


我的扩展结构:


  1. background.html

  2. script.js

  3. tracker.js






有关此问题的更多信息:

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域名下载脚本。



假设您使用带有 manifest_version:2 。您应该将此行添加到清单中

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

更多信息:



http://developer.chrome.com/extensions/tut_analytics.html


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

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

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

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:".

My extension structure:

  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: (I hide my 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);
})();

Thanks for any kind of help!

解决方案

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.

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'",

More info:

http://developer.chrome.com/extensions/tut_analytics.html

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

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