Since I last added Google Analytics to a website, Google have changed from the old urchin.js file to the shiny new ga.js file. This means that I really ought to start using ga.js now.
The University of Nottingham uses Sungard’s Luminis portal system which produces some rather nasty looking URLs. Web analytics for this is rather a handful – bad URLs, multiple features per page (each of which we’d like to track) and often tracking would be nice across sub-domains.
With that in mind, and Google’s Tracking Code Migration Guide close at hand, I’ve written jGoogleAnalytics.js to allow me to track:
- clicks events
- form submit events
- cross subdomain
- cross domain (e.g. for eCommerce payment gateways hosted externally)
- new organic search engines
- all the features of Jason Huck’s GA jQuery integration
Download jGoogleAnalytics.js
Usage:
// At it's simplest
$.jGoogleAnalytics( 'UA-XXXXXX-X');
// More advanced version
$.jGoogleAnalytics(
'UA-XXXXXX-X', // Your GA tracker code
{
anchorClick: false, // adds click tracking to *all* anchors
clickEvents: null, // e.g. {'.popup': '/popup/nasty'}
crossDomainSelector: false, // e.g. 'a.crossDomain'
domainName: false, // e.g. 'nottingham.ac.uk'
evalClickEvents: null, // e.g. {'#menu li a': "'/tabs/'+ $(this).text()"}
evalSubmitEvents: null, // e.g. {'#menu li a': "'/tabs/'+ $(this).text()"}
extensions: [
'pdf','doc','xls','csv','jpg','gif', 'mp3',
'swf','txt','ppt','zip','gz','dmg','xml'
], // download extensions to track
external: '/external/', // prefix to add to external links
mailto: '/mailto/', // prefix to add to email addresses
download: '/download/', // prefix to add to downloads
organicSearch: null, // e.g. {'search-engine': 'query-term', 'google.nottingham.ac.uk': 'q'}
pageViewsEnabled: true, // can be disabled e.g. if only tracking e.g. click events
sampleRate: null, // e.g. 50 - set the sample rate at 50%
submitEvents: {'#personUK': '/personsearch/uk'}
}
);
I’ve not added any eCommerce tracking to this release as we simply don’t have the need for it.
If you use this script, pplease leave a comment!
Comments
17 Comments so far. Leave a comment below.Hi,
Thanks for this, it looks like a good upgrade from Jason Huck’s code, I’m interested how I can track events that only happen on a single page (so shouldn’t be wired up throughout the site). e.g. Upselling, at the moment we do this onclick: javascript:urchinTracker(’/upsold/false/Some-Item’);
Is that possible with your code?
Tim
Great stuff!
Tim: If your upselling links had a parent container with a class name of “upsell”, e.g.
[ul class="upsell"]
[li][a href="http://example1.com"]Example 1[/a][/li]
[li][a href="http://example2.com"]Example 2[/a][/li]
[li][a href="http://example3.com"]Example 3[/a][/li]
[/ul]
…sorry for the square brackets
Your call to jGoogleAnalytics would be:
$.jGoogleAnalytics( 'UA-XXXXXX-X', evalClickEvents: {'.upsell a': "'/upsell/'+ $(this).text()"} );
This registers a click event for each anchor in the .upsell container, evaluates the link text and send it to GA.
The page impressions in GA for each link above would read:
/upsell/Example 1
/upsell/Example 2
/upsell/Example 3
Thanks for getting back to me, I’ve done something similar however as my upsell links are image buttons (so no text) I’ve done the following (the user has the choice in upgrading or not hence the true/false):
$.jGoogleAnalytics(
‘UA-123456-7′,
{
evalClickEvents: {
‘.upsellt’: “‘/upsell/true/’ + $(this).attr(’title’)”,
‘.upsellf’: “‘/upsell/false/’ + $(this).attr(’title’)”
}
}
);
And here’s the relevant HTML:
But I can’t see the upsell being logged anywhere in Google (but the page hits are).
Any ideas?
Tim
Ops sorry about the html (with square brackets):
[a href="/order/?li=241&vi=167&act=add" class="upsellt" title="Classic+Wax+100g"][img src="/img/bYes.png" /][/a]
Is there a way to use this to track submissions of only a particular form by using either the forms name or id?
Great plugin! Thanks! One problem though…
Using $.getScript will reload ga.js from google every time a page is requested. I fixed it by creating a function with the following code:
The essential part is to pass parameter cache: true. Now, jQuery will not force reloading the script every time.
Harri: Thanks. I’ll get it updated.
I just wanted to say thanks. We have this in testing over on our site and it has worked like a dream. I’ll let you know when we get a compressed version uploaded.
Erik: I’m glad it’s working for you – please let me know the sites you’ve got it on.
Sorry – I guess I could have compressed it, but when we build our javascript, we compress many files into one, so it’s of no use to me
Hube: You could track just the submit of a form with id of “myForm” using:
…this would only track the submit event for that form. It would not track any page views.
David, We too run Luminis 3.3.3 and would like to track all the clicks. where would I put your code to get the tracking set on.
Thnks
Bruce: My post Google Analytics and SungardHE Luminis hints at what we’ve done, but I need to spend a little time on a further post so that it’s a bit clearer. I’ve also updated the jQuery plugin since then and need to post about that too!
We’ve taken a razor to nested-tables.xsl, so it would take a reasonable amount of work to duplicate.
I know that there was a talk at Sungard Summit 2009 in Philadelphia that did what I’d call a first step in Google Analytics and Luminis. They basically added the normal GA code to the just before the </body> tag in nested-tables.xsl and produced a series of lookups for the different tabs and URLs. This is OK if you’ve got a very limited number of roles, but is unreasonable with many roles.
Hi David,
First thanks for the great JS!
I’m searching in the web jQuery JS’s which track Google Analytics data.
I used for first testing the JS provided by Jason Huck. Over his blog I found your JS and your blog.
When I look at the date of this post and the comments, most seem to be from 2008 (except the ones from David). In the JS file you provide the revision is noted as:
@version 1.0 2008-06-18
Now my concrete question(s):
- Is this the most actual file you provide?
Because I’ve seen the suggestion for example by Harri Kauhanen and I was wondering if you still use the same/origin file for yourself?
I also read from your post ‘I’ve also updated the jQuery plugin since then and need to post about that too!’ which implies that you use a newer version?
- Did you ever have problems with wrong page counts with IE6?
Compared to the original JS call as Google provides, the IE6 page hits are around 50% less than with the jQuery JS. I think the most likely reason of this problem is the jQuery initialization.
Best regards and thnx for any info
Christian
Christian,
In answer to your questions…
No, it’s not the latest version. I updated it in February 2009 and renamed it to qGoogleAnalytics (taking the q from qUnit) – to prevent confusion with the Java based GA utility.
Unfortunately, I never posted an update or let many people know. Sorry about that.
You can download qGoogleAnalytics here.
It includes a little University of Nottingham specific javascript, but is well commented so should be easily removed. I do hope to write an updated post about it and remove these bits. It’s reasonably well commented (in my opinion) as I generally compress all my JS using YUI Compressor.
In relation to the IE6 problem, I’ve not come across this. Sorry. We use it specifically in our intranet and our corporate browser is IE7.
Hi David,
First, happy new year!
Second, thanks for your reply!
About the IE6 issue:
It looks like it’s a problem of the domready call i used. With this code the stats seem to be better, but not completely accurate yet. But IE6 support is a mess anyways.
jQuery.noConflict();
(function($){
function callOnDomReady() {
… init tracker…
}
}
Question about licensing:
I can’t find any information about licensing of your file.
Is it free to use for anybody? (Same licensing as jQuery?)
Best regards,
Christian
Christian, I’ll update the license to same licensing as jQuery.
Trackbacks
2 pings so far.