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.

12 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.

  11. In response to Tom and stuntmanmike:

    In your Google Analytics reports, the requested page is recorded (for example “/” means your home page, some others could be “/gallery.php” or “/pages/contact.html”).

    In the case of a bookmarklet being used, the current site that it’s being used on is put into the “referral” variable, whereas the url for the script that was injected from your site is the “requested page”. This makes it easier to group the bookmarklet users.

    Maybe the reason why you were asking this was because your bookmarklet didn’t reference a script that was hosted externally on your own site, but was instead fully self-contained. In that case I guess you could just use whatever url that makes sense to you and is available (it must be unique, otherwise you wouldn’t be able to distinguish it from the other “real” page), Google Analytics doesn’t really care.

  12. Has this been updated to work with the more recent changes to Google Analytics?

Leave a Reply
Not required

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