admin管理员组

文章数量:1287697

I'm developing a Firefox extension and would like to track its use with google analytics, but I can't get it working.

I've tried manually calling a function from ga.js, but that didn't work for some reason. No error was produced, but neither was any data collected.

My last attempt was to have a website that just holds the tracking javascript and then load it within the extension in an iframe with the URL configured so it contains meaningful data. This way the analytics are getting connected when I visit said webpage with a browser, but not in an extension. I've tried putting some visible javascript on the site and have confirmed the site's javascript is executing. This method also works with other trackers, but I don't like their output and would prefer Google Analytics.

Any ideas what else I could try to acplish this?

I'm developing a Firefox extension and would like to track its use with google analytics, but I can't get it working.

I've tried manually calling a function from ga.js, but that didn't work for some reason. No error was produced, but neither was any data collected.

My last attempt was to have a website that just holds the tracking javascript and then load it within the extension in an iframe with the URL configured so it contains meaningful data. This way the analytics are getting connected when I visit said webpage with a browser, but not in an extension. I've tried putting some visible javascript on the site and have confirmed the site's javascript is executing. This method also works with other trackers, but I don't like their output and would prefer Google Analytics.

Any ideas what else I could try to acplish this?

Share Improve this question edited May 10, 2011 at 15:53 Wayne 60.4k15 gold badges135 silver badges128 bronze badges asked Nov 20, 2009 at 13:18 Swizec TellerSwizec Teller 2,3221 gold badge19 silver badges24 bronze badges 2
  • 1 If you are attempting to "emulate a browser" within the scope of your extension, this might not the right direction. In order to achieve anything close to real website, you have to be aware of all the code execution and event bindings Analytics creates (within any given page). It is not clear what exactly your extension does, thus it is hard to tell how you can get this done. – Tzury Bar Yochay Commented Nov 20, 2009 at 13:27
  • Well I don't really need all the special stuff, I construct a URL that contains all the information I need to track and then would like to fire up a page load event on analytics with that URL. Perhaps there's something about their API that I'm missing? Is there a way to fire custom events from any javascript code? – Swizec Teller Commented Nov 20, 2009 at 13:32
Add a ment  | 

5 Answers 5

Reset to default 5

The solution is to use Remy Sharp's mini library for tracking bookmarklets and extensions with Google Analytics. Works like a charm.

Usage is as simple as:

gaTrack('UA-123456', 'yoursite.', '/js/script.js');

Note that, since it doesn't use cookies, there's no differentiation between pageviews and visits, or for that matter, between visits and visitors. But, the rest of the functionality is fairly reliable.

Depending on what you want to track you may not need Google Analytics. Mozilla's addon.mozilla portal already provides prehensive tracking and usage statistics for addons.

To check if Mozilla provides what you need go to the Statistics Dashboard and choose the statistics for one of the publicly available addons.

Here is a small library to proxy the requests through an iframe hosted on another server: https://github./yelloroadie/google_analytics_proxy

This gets around the bug in the add-on sdk that causes ga.js to die (https://bugzilla.mozilla/show_bug.cgi?id=785914).

This method allows full use of google analytics, unlike the limited use found in the library by Remy Sharp.

I don't think this is possible. Firefox extensions don't allow you to load pages from other servers. So the only way I can think of is to have an invisible iframe load up the code. The pings to Google's servers need to be from a domain belonging to you. So I guess your own servers have to serve up pages every time a user loads the extension, which just kills your server and defeats the purpose of Google doing all the work!! Please post if you have found a way around it. Chrome extensions can be tracked easily!

For using analytics in the main/background script you might want to use this solution: https://stackoverflow./a/17430194/193017

Citing part of the answer:

I would suggest you take a look at the new Measurement Protocol in Universal Analytics:
https://developers.google./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:

本文标签: javascriptGoogle Analytics to track FireFox extension useStack Overflow