Check out my latest project: Full Frontal JavaScript Conference

Analytics for Bookmarklets & Injected Scripts

Lately I've been working on a number of different pure JavaScript projects ranging from widgets that are injected, bookmarklets and include scripts (red your site for example).

The one big thing that I've always wanted to see is analytics for usage, in particular referral tracking. So I've written code for that too :-)

Usage

I would recommend including the minified source for in your script/bookmarklet rather than loading in (another) external library (given the whole point of your code might be to inject a tiny bit of code). Then the call is simple:

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

Download

I've posted the project up on Google code - though plan (at some point...) to move it to github with the aim to let other developers improve the script (i.e. it doesn't track browser information as yet).

Download Google Anayltics JS

Notes

Currently the tracking is picking up the number of times it's downloaded and from where via the referral information. This was pretty much all I really find important in Google Anayltics, but if you want to add more information such as browser info, screen res, etc - let me know and I'll patch in the change.

10 Responses to “Analytics for Bookmarklets & Injected Scripts”

  1. [...] a nice, compact (~700 byte) way to track who’s using yours. Well done, Remy; check out his blog post for more [...]

  2. I'm not sure I'm getting this right. What is the info you get that isn't already gathered with the regular G.A. script?

  3. @effisk - it's not so much that we're capturing anymore information, moreover that you can now actually capture when your code is pulled in to the page via a bookmarklet or a dynamic include.

    You wouldn't use this code on your own site, but if you wrote a bookmarklet that you wanted to track it's usage and on what sites, you can use this code.

  4. That's interesting. I'm always looking for ways to make my site faster, and this could be one such tool - useful in reducing the total number of script downloads on the page.

    Thanks!

  5. Yup, it's a good idea.
    But Analytics sometimes could be slow to load.

  6. [...] a nice, compact (~700 byte) way to track who’s using yours. Well done, Remy; check out his blog post for more information. [...]

  7. Compression tips: Math.floor(x) is equivalent to the shorter (x)|0. 1000000000 is in ecmascript and javascript equivalent to 1e9. Your use of rand(min,max) seem to attempt to get a number x such that min <= x <= max, based on all of your 9999999999s. For that, you can use the much shorter form rand(i,1e10), as rand(i,9999999999) returns i <= x <= 9999999998 (as rand() returns 0 <= x < 1.0 -- and never 1.0). Thus:

    var i=1e9, utmn=rand(i,1e10), cookie=rand(1e7,1e8), random=rand(i,-(1<<31));

    Also, "new Date" and "new Image" are equivalent but shorter forms of the same plus a trailing "()", in your current code.

  8. Cool! I'm very interested in this and am adding it to one of my bookmarklets but I'm a little confused. What's the '/js/script.js' parameter in the function call?

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

    Thanks

  9. Yup, it's a good idea.
    But Analytics sometimes could be slow to load.

  10. I see that nobody has commented in some time, but wondering if anyone has an answer to Tom's question. What is the /js/script.js parameter, and why would you need this if you include the script in your bookmarklet? Thanks guys and looking forward to getting this going.

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight