Using Google Analytics in Firefox Extension

Deadly 提交于 2020-01-01 06:55:22

问题


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


回答1:


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.




回答2:


This is the bug that causes GA to break in the panel module: https://bugzilla.mozilla.org/show_bug.cgi?id=785914

Even if this was fixed, Mozilla won't allow the inclusion of ga.js due to the obfuscated source and security concerns. So you can't distribute your add-on via their addons.mozilla.org if you include ga.js.

To workaround all of this:

I created an iframe hosted on my server to proxy the analytics requests through. The code is on github here: https://github.com/yelloroadie/google_analytics_proxy

I hope this may be of some use to you.



来源:https://stackoverflow.com/questions/17389771/using-google-analytics-in-firefox-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!