Just to let you know that I have finally gotten round to updating the original combined tracking script hack I had for urchin.js to the new ga.js. Essentially the combined tracking script is a modification of the GATC so that it automatically tracks:
- outbound links – Automatically
- file downloads – Automatically
- mailto links – Automatically
Therefore, instead of having to manually modify such links by adding an onClick event handler to your ‘a’ tags, this script will do it all for you.
April 2011 – New async hack launched
This hack has been completely revise for the latest GA async code. Further info >>
Why is this necessary?
Outbound links (links to other websites from yours), file downloads (e.g. PDFs, exe, xls etc.) and mailto links (links to an email address) can be important indicators as to whether your site is engaging with visitors. If so, you will certainly want to track these and potentially define them as goals in your Google Analytics reports.
Normally you do this by creating a virtual pageview. However if you have hundreds/thousands of these, or even just a few dozen that constantly change, manually tagging them is an administrative nightmare.
This script uses the properties of the browser (Document Object Model, or DOM for short), to capture links that are non-standard pageviews and automatically create the event handler for you.
The result is that your outbound links, file downloads and mailto links are tracked for you and are always up to date. The default virtual locations for these in your Google Analytics reports are: /ext/, /downloads/, and /mailto/ respectively, though of course you can change these as you wish. Typically you will wish to view these in your Content > Top Content report as shown below:
IMPORTANT: The position of this code within your page is important. This must placed after your call to the GATC. Add addLinkerEvents() in an onLoad event handler and host the JavaScript in a separate file. As an example I show this below, assuming the javascript is hosted in a file called trackExternal.js, as follows:
[js]
…your remaining web page content…
[/js]
A note on performance: Each time your page loads, this script will go through all links referenced on the page to see if it is for a download. Clearly the more links on your page, the harder the script must work. As long as the number of links on each page number in the hundreds and not thousands, performance should not be a problem. Also, pages with a large number of links, it is possible that visitors will click on a download link before the script has modified it. The result is that click through will not be tracked by Google Analytics.
Related posts:
- Tracking links to direct downloads automatically
- Tracking banners and other outgoing links automatically
Are you using this or creating your own hacks for Google Analytics? Please post your comments and share your thoughts here.
Jason: I took a quick look at your page and could not see the call to addLinkerEvents() in the onLoad statement – or did you put this somewhere else?
wrt IE, Holger Temple did some testing on this and found that the script will not work in current versions of IE 🙁
His comment is here: http://www.advanced-web-metrics.com/blog/2007/08/15/tracking-banners-and-other-outgoing-links-automatically/#comment-7709
I have an issue that I have been struggling with, but before that I wanted to let Brian know how much his script and support have been appreciated.
The trouble that I am having seems to be coming from IE. On Firefox the script works like a champ and records the data correctly. But in IE I have not been able to get it to track correctly.
I do have the code in the tag as mentioned and I have the appropriate script call and can see the code in FF. I did some independent static testing on IE and the numbers never recorded.
I read through most of the comments on this post and can’t seem to find anyone else with this problem. Or maybe I am missing an update because the script and download I have says they were updated on July 15th 2008, and I know it has been updated since then.
Any help would be greatly appreciated.
Here is the link to the example
http://www.knittingdaily.com/blogs/patterns/archive/2008/10/03/a-little-something-special.aspx?download=true
Thanks Victor- you are entirely correct.
As this hack is an update of previous posts, I had wrongly assumed readers were following the thread and had missed out some of the detail for configuration. Of course no one reads old posts, so I have updated the article to make clearer…
Jacques et – My apologies for the confusion! I would love to know whether you think this post is more understandable now.
Hi Victor,
Hey! Thanks a whole lot! I’ll try it right away.
euh sorry about that it seems that the blog picks up the html code and filters it out :P. So i will use text only ;).
You should put the reference to the external script within your head area (see this page as example). Then replace your body tag with body onLoad=”addLinkerEvents()” .
Hi Jacques,
First put the JavaScirpt somewhere on the server. Then make a reverence to the location within your html within the area. If you put the JavaScript in a folder named “scripts” on your root than the reference should look like this.
Then replace the tag on your page with
This will load the function within the JavaScript code, so that Brians script will add the extra code to the outgoing links/downloads and mail addresses. If you install the Firebug plug-in from Firefox you should be able to use Firebug to see if the links are dynamically tagged with the extra Google code. Your GA implementation should measure the links within 24-48 hours. I hope this small tutorial will help you with implementing the tracking code.
Hi Brian,
This is great! However, I am trying to find how exactly you want us to implement it, but can’t find the info. So far, all my tests with it have not been conclusive
There was a minor bug in the script where by if you had a mailto tag in the page, the script would stop processing at that point. This has now been corrected by removing the break command from the final loop.
This effected both urchin.js and ga.js versions
Thanks to Victor Geerdink of userintelligence.com for spotting this.
A question about JavaScript links on the page resulting in “undefined” external link URLs as a result of the script. Is there a way to solve this issue? For example a JavaScript link that pops out a new window is recorded as “/external-link/undefined”
Thanks!
We’re trying it on http://www.navymutual.org and it doesn’t seem to be working. Have the script in an external js script and call it after the GATC. You can see an example of where it isn’t coming up on http://www.navymutual.org/ReservistDetails.asp, at the “Reserve Component SBP Calculator” link in the sidebar.
Any ideas on what I’m doing wrong?
Thanks –
Also, are we supposed to change what is in the email config – by default listed as gashbug@google.com?
Matt: no the script is not intended to automatically track visitors spanning different domains.
Does this script automatically tag links with the appropriate code if we’re tracking across multiple domains? (e.g. the onclick=”pageTracker._link(this.href); return false;”> )
Steve: no problem if you wish to use relative links for file downloads etc, the script will still work.
Hayden: You could the modify the script so that instead of performing a pattern match on the file extension (.doc, .xls etc), you look for the string “ImageID” and use this match to pull out the file name and place in a separate pageview.
Justin: the Javascript onClick event handler only works with the left mouse click. However I am sure the hack could be modified to handle right button mouse clicks. The code requires:
Audio Bible: You can find a list of Google Analytics Authorised Consultants (GAACs) on this site under Getting Help.
Brian, do you know of someone who can help me install this code correctly? I am willing to pay a reasonable price. I want to track Audio file downloads.
Let me know……..thanks.
Will this be able to track off-site download links, when the user chooses to right-click and Save Target As?
I have a small problem I could use some insight on please. a Content Management System I’m using stores all images and files with a string like GetImage.aspx?ImageID=aFG7843j and no extension on the end. This means the above script is not tracking it obviously. I’m wondering how I would modify the script to allow these file downloads with no extension to be tracked?
ALL: I have trouble shooted a number of sites posted here where the script was reported as not working.
The most common mistake I have found is sites using the latest ga.js GATC, but using the legacy version of addLinkerEvents.js (i.e. urchin.js version)
Please ensure you are using the right script for your GATC as listed on the Scipts & Download page (http://www.advanced-web-metrics.com/blog/ga-scripts/)
In using the ga.js script we are able to track emails, external links and downloads. We’ve noticed that external links from one tracked site are usually less than the what’s reported in the other’s sites traffic sources referral sources report.
For example SappSeaWalls.us shows just 5 external visits in the last 30 days to SappSeawalls.com But Analytics for SappSeawalls.com shows 12 visits in its All Traffic Sources report during the same time period.
I’m ordering your book to learn more, but can you shed any light on this apparent conflict?
Hi Brian,
We’ve just migrated over to ga.js and I’m having a slight issue with the link tracking script.
External links and mailtos are being tracked correctly but documents are not. Our content management system uses relative links to documents i.e. /documents/pdfs/some-pdf.pdf not http://www.site.com/documents/pdfs/some-pdf.pdf – could this be the cause of the script not teacking documents?
Steve
Didn’t see instructions about how this should be implemented. I added it to our site at navymutual.org but don’t see it in the source. We put it in sis.js which is linked in the head. Did we do this correctly?
Hi Brian,
Thanks for providing the tracking script. I have applied this to one of our development sites for testing. See http://dev.studylink.com.
The problem I have is that I am not seeing any results in Google Analytics. So I am assuming the script is not set up correctly.
I have tried your debugging suggestion, by adding the alert, and can confirm that I receive null where the onclick attribute should not be null.
Strangely, when I highlight a link (see the ‘test’ link at the bottom of the page), and select ‘View Selection Source’ in firefox, I do see the onclick attribute defined correctly in the html.
Any help would be much appreciated.
Hi Brian – Sorry for the confusion there. I was testing a few other scripts I found online. It has now been updated with your script, and isn’t working.
http://www.sarfraternity.org/learn-b.htm
To debug this I added an alert statement to your script here:
http://www.sarfraternity.org/learn-e.htm
This provides an alert for every object in the extDoc[] array which displays the name of the file, then shows what is set as the onclick action for that link. Interestingly, the onclick action is set correctly, it’s simply not being triggered. (I’m using Charles to see that the action is not triggering)
Thanks for your help!
Eric: the script you refence in your html (outbound.js) is not my script.
Hi Brian –
Thanks for posting this script, I’m looking forward to getting it to work. However, I’m having an issue installing it. Yesterday, I added the script to the of the page that I’d like “.doc” files to be tracked. After downloading several files through different ISPs and browsers, Google Analytics did not track any downloads. This morning I tried moving the script to the section of the html page. Is this where it should go?
If you’d like to check out where I am testing the script:
http://www.sarfraternity.org/learn-b.htm
There are 4 “.doc” files at the bottom.
Also, how do you recommend testing this? Are there any ways to confirm that your script is installed correctly besides adding it to a page, downloading some files from it, then waiting to see if Google Analytics picks it up?
Thanks!
Timo: Check your JavaScript for errors and conflicts – Firebug is an excellent Firefox plugin for this.
Charel: I don’t know what the reason is, but Firebug doesn’t show the modified “a” tags. For testing, I simply used an alert statement in the loop. Are you seeing data in the reports?
Hi Brian,
Thanks for the script, it’ll be most useful once I’ve installed it correctly.
I’ve found a couple of inconsistencies and to eliminate installation error as the cause, I’ve had a look at your site and it looks like they are here too. Eg, as far as I can see, the the script is installed on this page (http://www.advanced-web-metrics.com/blog/2008/06/08/updated-tracking-script-for-gajs/), but the link to Avinash Kaushik doesn’t have an onclick event and doesn’t show any tracking calls in Wasp or Firebug.
Similarly, testing the onclick events using IE and Fiddler, I don’t see any tracking calls at all (for the ones that work in Firefox and Chrome).
Might there be something wrong with the way I’m testing, or can you see inconsistencies too?
Many thanks
Charel
Damn. The blog-software killed part of my post.
After the last line of code it should have been:
< script type=”text/javascript” src=”/common/includes/foot/tracker.js”
(hope this works now)
I think I did everything the way it should be done but obviously I did not: Nothing shows up under
/external/
/directdownloads/
/mailto/
which is where they should show up since I modified the script accordingly.
However, I can see nothing in GA even though I’ve had the script on a few pages for a couple of days now. To be sure I checked for the original (/ext/, /downloads/, /mailto/) as well.
So I’m afraid I might be missing something really, really obvious.
Here is how I implemented it:
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
var pageTracker = _gat._getTracker(“UA-2851757-1”);
pageTracker._initData();
pageTracker._trackPageview();
Do you see any glaringly obvious problems? tracker.js is the tracking script.
Thanks,
Timo
*** custom_se-ga.js ***
I am going to setup a separate thread for discussing this script. Just want to do some testing here first…
[updated 15-Sep-2008: This is now back working 🙂 . See post: http://www.advanced-web-metrics.com/blog/2008/09/14/customising-the-list-of-search-engines-in-google-analytics/ ]
Jonas: The reference to the .js can be any path you wish but it must be placed below the GATC. This is because it is calling routines that are defined in the GATC.
Michael: Yes you can modify the code to only track downloads. Seek the help of a web devloper who understands JavaScript for this. Its not hard to do, but it does need to be exact.
Gabe,
Thanks for your advice.
I’ve been trying to get it to work on jonas.ax.
I moved the custom script, in this case addLinkerEvents-ga.js above the GA tracking code and did some testing and waiting. But still no luck. John (earlier in the comments) has the script after the GA code.
I’ve placed the script in a folder called /google-scripts/ rather than just /scripts/ (but the link to it in the GA code is correct). Perhaps this could be it? The GA code is also at the end of each page, perhaps moving it to the beginning would do it?
Hi Brian,
I’m assuming the combined code can be modified to track file download links only.
Can you indicate which parts of the js file should be removed to avoid tracking the outbound and email links.
Thanks,
Michael
Jonas:
I have implemented the same code recently onto our website, and encountered exactly the same problem as yourself. After doing some research I think that I understand what the issue may be.
Where did you implement the script? The custom script should be above the GA tracking code you have implemented. Therefore:
< script type=”text/javascript” src=”http://www.yoursite.com/scripts/custom_se-ga.js
var gaJsHost = ((“https:” == document.location.protocol) ? “https://
ssl.” : “http://www.”);
[removed](unescape(“< script src='” gaJsHost “google-
“));
var pageTracker = _gat._getTracker(“UA-0000-1”);
pageTracker._initData();
pageTracker._trackPageview();
Does that make sense?
I’ve been using the custom_se-ga.js on a few sites and haven’t had any issues. The data is coming through ok for me. For what its worth at least 🙂
Brian, on my site (jonas.ax) I have installed the script, but I see no data for:
/ext/, /downloads/, /mailto/
in the “Content Overview”/”Top Content” reports.
I’ve tested by clicking on links to external sites and clicking on the mailto links as well (from several different computers). I’ve also made sure to wait for the pages to load completely, but still no luck only ordinary page views are displayed in GA.
I removed the custom_se-ga.js.
Jonas – I have this script running on numerous sites, both the urchin.js and ga.js. What appears to be the problem?
BTW, I do not recommend using custom_se-ga.js at the moment. Something has indeed changed with the GATC – I am out of the direct loop with Google now, so I don’t know exactly what, but I am pretty sure it has nothing to do with _initData.
Essentially custom_se-ga.j is still in “testing”
I do not seem to get this script nor the custom_se-ga.js script to work. No external links are tracked and google traffic shows up just like before as “google / organic”. Can I not have it both ways? I also noted that the GA code has recently changed (just a bit):
http://www.morevisibility.com/analyticsblog/rest-in-peace-_initdata.html
Frederikke: Good point. I have clarified this in the post and added a screenshot. The data will show in numerous places – where ever there is a page URL listed. The most common place you will see this is in the Top Content report. HTH.
Martijn: Thanks for the feedback. Good to see you made it through to a GAAC. A long process I know 😉
Adam: The script captures everything after the “mailto:” in the a tag and puts into a virtual URL. So yes, you will see the subject and can filter on this.
Brian –
Great book, finished it last week and it has shaped the way I approach GA for our clients and sites I work on. I have also been having issues with this script and wondered if you can answer a question regarding it.
Will the script pick up mailto links that are formatted with script to insert custom subject lines and content into the email? For example – if the mailto link looks like this: sales@example.com?subject=question&body=example or will it only work if the mailto link is sales@example.com
Thanks!
Nice script.
We sometimes advice our customers to use a similar script to also measure downloads, mailto, clickouts. I also believe using this script is a must. In example knowing what links on your website gave you the biggest loss of traffic is a real interesting metric.
I believe in some cases it might be advisable to add the pageurl on which the click was made.
In example did a lot of visitors click my email address on the contact page or on the page to submit complains.
Measuring everything gives you the biggest insight and the biggest worries.
Hi Brian
I have had my web-design company implement the script to automatically track downloads in GA but where in GA do I go to view the results? I don’t really understand the part in this post where you say ‘The default virutal locations for these in your Google Analytics reports are:
/ext/, /downloads/, /mailto/
respectively, though of course you can change these as you wish.’
Thanks in advance for your help!
Regards,
Frederikke
John: agreed!
I will be adding more comments to the script itself and creating an install page just for this script.
Essentially the script has grown out of blog posts over time which makes it a little difficult to follow. Its now time for me to put this all together on a single page.
In terms of placement, I just prefer to put the GATC in the header as opposed to the footer – just my preference. I take the risk that the chances of Google’s servers preventing the rest of page loading is minimal!
Brian,
This is good stuff. I am running the script on a couple of pages (keepingchildrensafe.org and writemedown.org) and they are working fine. The new annotated version is helpful. You might get more users if you provided a brief guide on configuration. I worked it out from the html of your site, puzzled a bit over having the GA script in the header.
One question: I have included the sites with and without “www” in the section for excluding my own domain from tracking. Is that necessary?
Oh, and I have read large chunks of the book and am well impressed so far.
Today I uploaded a new combined tracking script (over writing the original) to fix a bug for tracking outbound links. Essentially the original code only worked for the first domain specified in the extTrack array and effects both ga.js and urchin.js. This is now fixed.
The script is at: http://www.advanced-web-metrics.com/blog/ga-scripts/
I have also added an email address form to this page so you can be notified of any further updates.
Danny: For trouble shooting I would try printing out all the onClick event handlers and checking the syntax. e.g.
within the loop:
for(var i=0; i
Hi Brian,
First of all, thanks for maintaining this great resource. I just purchased your book and am looking forward to reading it!
My developers and I are having a bit of issue with this code.
All of our sites are built XHTML strict so most of the anchors that would be tracked by this script use onclick=”window.open(this.href); return false” to open a new window, as “target” is not valid.
From our tests it seems that this breaks your tracking script. Do you have any solutions in place for this scenario?
We looked at this solution (http://archivist.incutio.com/viewlist/css-discuss/55345, but haven’t yet tested it.
Thanks!
Danny
Glenn: The script should work fine once installed correctly (it is running on this site).
What browser/OS are you using? Also, please send me the URL to the page in question and I’ll take a look.
Tomas: Unfortunatley not. I tried this earlier in the year trying to capture the search terms that result in click throughs from Technorati which follows a similar format.
Essentially the issue is that the search engine detection is all done on the landing page (via the GATC). At first I tried to manipulate the referring string in the DOM, but of course this is a security violation as far as the brower is concerned and so not allowed.
Hello Brian
Thanks for the new combined tracking script for ga.js
I have pasted your code as given below the ga.js code in my pages but they throw up xhtml validation errors whereas the google script does not? Is this something I have to live with or should I be using your script as a separate file with an onload call?
If so, what is the correct onload call code syntax and where should I place this? – I know nothing about js I’m afraid.
Thanks
I wonder if it is possible (or how) to hack custom search engine script, so you could measure also engines with rewriten urls – so they do not have example.com?search=something but example.com/search/something