在Firefox扩展中使用Google Analytics [英] Using Google Analytics in Firefox Extension

查看:261
本文介绍了在Firefox扩展中使用Google Analytics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的Firefox插件使用Google Analytics事件跟踪。

 < script src =http://www.google -analytics.com/ga.js\"></script> 

另外还有:

pre > < script>
var _gaq = _gaq || [];
_gaq.push(['_ setAccount','UA-XXXXXXXX-X']);
< / script>

使用以下代码推送事件:

  _gaq.push(['_ trackEvent','example','clickedit']); 

我在firefox错误控制台中看不到任何错误,并且该分析页面中没有该事件。



有什么想法?请问firefox不允许这样吗?

谢谢

解决方案

建议您查看Universal Analytics中的新测量协议:

https://developers.google.com/analytics/devguides/collection/protocol/v1/



这允许您使用XHR POST直接发送GA事件。



这将与Firefox扩展共存。



代码如下所示:

  var xhr = Cc [@ mozilla.org/xmlextras/xmlhttprequest ; 1\" ]的createInstance(Ci.nsIXMLHttpRequest); 
var url =http://www.google-analytics.com/collect;
var params =v = 1;
params + =& tid =+GOOGLE ANALYTICS ID;
params + =& cid =+ UNIQUE IDENTIFIER
params + =& t =+event;
if(category){
params + =& ec =+ category;
}
if(action){
params + =& ea =+ action;

if(label){
params + =& el =+ label;

if(value){
params + =& ev =+ value;
}
params + =& z =+(1000000000 + Math.floor(Math.random()*(2147483647 - 1000000000)));

xhr.open(POST,url,true);
xhr.channel.loadFlags | = Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
xhr.send(params);

请注意,您必须在Google Analytics中新建一个属性,以便将其指定为Universal Google Analytics(分析)。

I would like to use Google Analytics Event tracking for my Firefox addon. I have included ga script like this in my popup.html.

<script src="http://www.google-analytics.com/ga.js"></script>

And also added:

<script >
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
</script>

I push events with the following code:

_gaq.push(['_trackEvent', 'example', 'clickedit']); 

I dont see any error in the firefox error console and also the event is not there in the analytics page.

Any idea? Does firefox doesn't allow this?

Thanks

解决方案

I would suggest you take a look at the new Measurement Protocol in Universal Analytics:

https://developers.google.com/analytics/devguides/collection/protocol/v1/

This allows you to use XHR POST to simply send GA events directly.

This will coexist much better with Firefox extensions.

The code would look something like this:

var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
var url = "http://www.google-analytics.com/collect";
var params = "v=1";
params += "&tid=" + "GOOGLE ANALYTICS ID";
params += "&cid=" + UNIQUE IDENTIFIER
params += "&t=" + "event";
if (category) {
  params += "&ec=" + category;
}
if (action) {
  params += "&ea=" + action;
}
if (label) {
  params += "&el=" + label;
}
if (value) {
  params += "&ev=" + value;
}
params += "&z=" + (1000000000 + Math.floor(Math.random() * (2147483647 - 1000000000)));

xhr.open("POST", url, true);
xhr.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;  
xhr.send(params);

Note that you'll have to create a new property in Google Analytics so you can specify it as Universal Analytics.

这篇关于在Firefox扩展中使用Google Analytics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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