Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Analytics in Firefox Extension

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

like image 703
Prabu Avatar asked Jun 08 '26 11:06

Prabu


1 Answers

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.

like image 69
Mike Kaply Avatar answered Jun 10 '26 17:06

Mike Kaply