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](http://redyoursite.com/) 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 :-)

[I’ve published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)

Usage[](#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[](#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).

Notes[](#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.

Published 27-Feb 2009 under #code & #google & #javascript. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/analytics-for-bookmarklets-injected-scripts.md)

Comments

Lock Thread

Login

Add Comment[M ↓   Markdown]()

[Upvotes]()[Newest]()[Oldest]()

FciscoG

0 points

7 years ago

Is this still relevant in 2016? what are you all doing for the UA code? Did you create a new Website Account in the GA Dashboard? What did you put when it asked you for a Website URL. My bookmarklet lives in github

?

Anonymous

0 points

9 years ago

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

?

Anonymous

0 points

13 years ago

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.

?

Anonymous

0 points

14 years ago

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.

?

Anonymous

0 points

14 years ago

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

?

Anonymous

0 points

14 years ago

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](http://yoursite.com)', '/js/script.js');

Thanks

?

Anonymous

0 points

15 years ago

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.

?

Anonymous

0 points

15 years ago

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

?

Anonymous

0 points

15 years ago

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!

?

Anonymous

0 points

15 years ago

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

?

Anonymous

0 points

15 years ago

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?

[Commento](https://commento.io)