Updated auto tracking script for file downloads, outbound links and mailto links
Google Analytics Hacks June 8th, 2008Just to let you know that I have finally gotten round to updating the combined tracking script hack I had for urchin.js to the new ga.js. If you know what I am referring to and simply wish to go straight to the script, the new file is listed here: http://www.advanced-web-metrics.com/blog/ga-scripts/
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.
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 virutal locations for these in your Google Analytics reports are:
/ext/, /downloads/, /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:
<script src="http://www.google-analytics.com/urchin.js" type="text/JavaScript">
</script>
<script src="/trackExternal.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
_uacct = "UA-XXXXX-Y";
urchinTracker();
</script>
<body onLoad=”addLinkerEvents()”>
...your remaining web page content...
</body>
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.
---Related posts:



(19 votes, average: 4.00 out of 5)


July 1st, 2008 at 6:10 am
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
July 3rd, 2008 at 2:07 pm
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
July 11th, 2008 at 12:13 pm
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.
July 11th, 2008 at 2:15 pm
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.
July 15th, 2008 at 2:56 pm
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
July 15th, 2008 at 10:43 pm
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
and after var tmp line, add an alert statement:
alert(“tmp” +i+ ” = ” +tmp)
July 16th, 2008 at 10:44 pm
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.
July 17th, 2008 at 2:34 pm
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.
July 21st, 2008 at 12:16 pm
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!
August 11th, 2008 at 12:20 am
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
August 15th, 2008 at 8:18 am
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.
August 15th, 2008 at 1:47 pm
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!
August 17th, 2008 at 10:48 pm
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.
August 20th, 2008 at 12:34 pm
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
August 20th, 2008 at 11:46 pm
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”
August 21st, 2008 at 1:13 pm
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.
August 21st, 2008 at 10:59 pm
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
August 22nd, 2008 at 9:59 am
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?
August 25th, 2008 at 9:06 pm
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
August 27th, 2008 at 12:33 pm
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?
September 1st, 2008 at 7:16 pm
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.
September 1st, 2008 at 7:31 pm
*** 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/ ]
September 4th, 2008 at 11:37 am
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
September 4th, 2008 at 11:40 am
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)
September 10th, 2008 at 12:30 pm
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
September 15th, 2008 at 12:39 pm
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?
September 23rd, 2008 at 2:17 pm
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!
September 26th, 2008 at 4:04 pm
Eric: the script you refence in your html (outbound.js) is not my script.
September 26th, 2008 at 4:31 pm
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!
October 3rd, 2008 at 9:27 am
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.
October 7th, 2008 at 9:44 pm
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?
October 8th, 2008 at 9:53 am
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
October 13th, 2008 at 3:56 pm
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?
October 13th, 2008 at 4:15 pm
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/)
October 14th, 2008 at 12:26 am
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?
October 15th, 2008 at 3:05 pm
Will this be able to track off-site download links, when the user chooses to right-click and Save Target As?
October 16th, 2008 at 7:02 pm
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.
October 20th, 2008 at 11:49 am
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.
October 22nd, 2008 at 5:34 pm
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;”> )
October 22nd, 2008 at 7:32 pm
Matt: no the script is not intended to automatically track visitors spanning different domains.
October 22nd, 2008 at 8:51 pm
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?
October 25th, 2008 at 7:33 pm
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!
October 31st, 2008 at 6:32 pm
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.
October 31st, 2008 at 8:24 pm
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
October 31st, 2008 at 11:25 pm
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.
October 31st, 2008 at 11:31 pm
euh sorry about that it seems that the blog picks up the html code and filters it out
. 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()” .
November 3rd, 2008 at 12:44 pm
Hi Victor,
Hey! Thanks a whole lot! I’ll try it right away.
November 3rd, 2008 at 2:47 pm
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.
November 3rd, 2008 at 5:54 pm
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
November 3rd, 2008 at 7:49 pm
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
November 3rd, 2008 at 9:35 pm
It is in the code in the body tag on line 57
.
Anyway, I know how much of a pain IE is, and we could all use a little more support and fixes from Microsoft rather than advertising, but is there a work around for this problem with IE? Unfortunately, IE is still the standard on most sites, and cutting out that part of our audience would make the tracking shaky at best.
Again, an example of a coder coming up with something cool and IE breaking it by not following the rules
Thanks again for your time and attention
November 6th, 2008 at 2:20 pm
Hi Brian,
I’ve, unsuccessfully, been trying to implement this code onto our site. We have multiple profiles in our GA set-up, with a naming convention of firstTracker, secondTracker, etc and was wondering whether I will need to amend the 3 lines with ‘pageTracker’ mentioned to ‘firstTracker’ etc?
Steve
November 7th, 2008 at 1:26 pm
Hi Brian,
First of all, thanks for all your good work on the scripts! This resource is extremely helpful.
I have been trying to track downloads and had success with tracking files being downloads / viewed on my site.
However, what I’m writing in about is slightly different.
I have links from other sites (e.g. Wikipedia) which give users the option to download a .pdf on my site. They are mentioned on the Wikipedia site as url to ‘http://www.mysite.co.uk/xyz.pdf’
This makes it possible for the user to directly view the page. This is further complicated because my site is database driven and all the files are present in the repository.
Is there a way of tracking these Inbound downloads from my site?
Thanks a ton!
Chandra
November 9th, 2008 at 1:28 pm
Steve: Yes, “var pageTracker” is the declaration of a variable in the GATC. So if you have: “var newTracker” you need to change all references to reflect this.
Chandra: The way around this is to have all direct links to PDFs go to a landing page, first and then follow the click throughs. See Tracking Links to Direct Downloads in Chapter 9.
November 12th, 2008 at 11:56 am
Hi Brian,
I’ve been trying to get this script working for the last week or two and after cleaning up a couple of silly errors on my part, I still can’t get it work. Could you take a look for me? I could add onClick to every link but I have hundreds of downloadable files on this site and I don’t think I can face doing that.
Site is itute.com
Thanks,
June
November 16th, 2008 at 8:47 am
June: I found some javascript errors on your home page using Firebug – see the post Troubleshooting Tools for Web Analytics
These may well be preventing the script from working
November 23rd, 2008 at 10:10 am
Thanks for that Brian. You wouldn’t believe what the problem was! I copied and pasted your code from this page into my code but my code editor didn’t like the style of double quote marks.
After puzzling over what could be causing the Javascript error, I noticed the style of quote marks was different to those used elsewhere in the code. I fixed it up and the error disappeared… so I’ll have to wait another day or so to see if the clicks show up in analytics.
Thanks again for your help.
June
December 31st, 2008 at 8:17 pm
Does anyone have any idea why this wouldn’t work with ga.js on a Yahoo store?
February 11th, 2009 at 6:16 pm
Hi everyone!
Hi Brian ! Thank you for your book. I’ve read it all and it is a great tool for me.
I’m Antoine, a French Canadian guy, so I apologize for my bad English.
I’ve tried to install the script to my Web site but I’m very not sure if I did the setup properly. (I know sweet nothing about JavaScript).
Could you please tell me if I did the right thing?
1) I’ve firstly created a new folder on my server called GA-scripts where I’ve copy and pasted the New Combined tracking script that I found at http://www.advanced-web-metrics.com/scripts/addLinkerEvents-ga.js. I’ve named that document trackExternal.js
2) Since I am using Wordpress, I’ve changed my GATC in the footer.php with the following :
_uacct = “UA-*******-1″;
urchinTracker();
3) Finally, in the page.php, I’ve changed the tag by
Can you please tell me if I did the right thing? If not, could you please provide me with more detailed information with the set up process.
Thank you very much for your help.
Best regards, Antoine
February 11th, 2009 at 6:26 pm
Ok
As I can see, the script that I just wrote just don’t show up… Anyway, the main idea is that I took your trackExternal.js and I saved it to a folder called GA-scripts on my server and then I’ve changed my GATC and the body tag in my pages. Is it fine to do it that way.
Thanks again! Antoine
February 20th, 2009 at 11:35 am
Hello,
I have implemented all the code and everything seems to be OK. But, there is one thing that I don’t understand -in TOP CONTENT report there is also mentioned this: “/ext/undefined” What does it mean and is there a way to solve this issue?
Thanks in advance.
Dario
February 21st, 2009 at 6:55 pm
Dario: Not sure – do you have other onClick event handlers already on the page?
February 22nd, 2009 at 10:17 am
Brian,
Well, there are two onCLick events on the page: Google AdSense ads and print page option…do they count as “undefined” external URLs? Thanks
February 26th, 2009 at 6:54 pm
There are some script errors, but overall it’s awesome!!
March 2nd, 2009 at 8:54 am
Webdesign: Can you expand on what errors you are getting please
March 3rd, 2009 at 6:29 pm
I’m using this to track clicks to vendors (external clicks to other domains) from a product site. I am seeing clicks in top content, but these clicks don’t match up with the traffic that the vendors are seeing from the page. One day I’ll have 3 clicks from vendors.html to a specific vendor, but said vendor will have 15-20 referrals from mysite.com/vendors.html – Anyone have any idea why this might be?
March 4th, 2009 at 4:23 am
Thanks for this Brian.
I’m not sure if the tracking electronic downloads part of the combined script is entirely correct (or I may have misunderstood it). It splits on extTrack[0] always, but should it not loop through the array of extTrack and then split based on the match?
Thanks!
Shirley
March 13th, 2009 at 2:07 am
Hey Brian – Thanks for the code! I’ve been slaving away trying to manually track pdfs for awhile now and this will save me tons of time. (Keeping my fingers crossed!)
March 14th, 2009 at 6:31 pm
John: Could be lots of reasons for differences (have a look at this post if you have not read already: http://www.advanced-web-metrics.com/accuracy-whitepaper). One thing I would try, is to move the GATC to the top of page i.e. in the <head> section. This ensures the tracking code is loaded before anyone can click away…
Shirlz: Your understanding is correct. There is a reason for this – though I can’t remember exactly why. Let me think it through and get back to you.
Update: OK, so the logic for having only extTrack[0] for file downloads is that these should only be hosted on your primary domain. It doesn’t make sense to host ‘your’ downloads on a third party site – they would then be an outbound link and picked up as such.
Jeremy: Thanks for the feedback. Let me know if it solves your problem.
March 15th, 2009 at 1:41 am
I’m testing out your script and love the idea. I’m interested in applying it to a very large site. But I noticed something odd on mine. All of my traffic since installing is showing as /ext/ links. Sorry I should say if some one clicks on a link to another page even that page is showing up in GA as /ext/www.domainname.ca/web/internalpage.html But this page internalpage.html isn’t external lol it’s just another page on my site. All links are are root relative links. /web/pagefile.html
Any ideas what I might have done wrong? Thanks again ps I bought your book and really like it. I hope you put out something with Adwords and more SEM.
Cheers,
C
March 24th, 2009 at 7:24 am
Hi Brian,
I have successfully installed and tested this script and it is working well apart from one issue.
The gif is not being fired off when downloads are clicked on when Firefox is being used.
Do you have any suggestions?
Regards,
Brendan.
March 25th, 2009 at 1:34 am
Hi Brian,
I have an update on the Firefox issue as mentioned in my last post.
Even though LiveHTTPheaders, HttpFox and HttpWatch all indicated that the data was not being fired off to GA when a downloadable document was clicked on in Firefox, I have been reassured that the script does work because data is now showing up in my GA reports.
Brendan.
March 30th, 2009 at 6:59 am
Hi Brian,
You may have addressed this issue before, but would it be possible to provide this tracking script using the event method in addition to the pageview method? Maybe some sort of true/false setting could be included.
An example of this sort of script can be found at:
http://blog.immeria.net/2009/01/google-analytics-script-to-track.html
Kind regards,
Brendan Halloran.
March 30th, 2009 at 12:12 pm
Brendan: Yes I am aware of Stephane’s script and I am sure it works well. I am also working on a switch method so you can choose which tracking method to use. Allow a month for this…
March 30th, 2009 at 10:08 pm
Hi Brian,
I have tested Stephane’s script and it does work well, as does yours. The best of both of your scripts would be a fantastic outcome – the ability to use pageviews or events, as well as be able to track clicks on mailto links in addition to outbound links and downloads.
Regards,
Brendan Halloran.
March 31st, 2009 at 8:41 pm
Hi again Brian!
I’ve installed your script on my server and I noticed that I just could not see the Events Tracking Reports in Google Analytics. I soon realized that I just didn’t have access to this functionality within my GA account, simply because I didn’t have a Beta version for my own personal Web site.
So here are some links that might be useful for others that happen to have the same problem that I had.
First, if you want to benefit from the Event Tracking method, you need to fill that request form from Google (http://spreadsheets.google.com/viewform?key=p7DycufUwNgmtunKEtgNGBg&hl=en).
Secondly, even though you don’t receive any news from Google, you don’t have to wait to see the Event Tracking reports. Here is an article from 3DIssue (http://www.3dissue.com/news/?tag=event-tracking) that shows you exactly what to do. You’ll see, it is pretty easy and very useful.
Thanks for sharing this information with your audience.
Best regards, Antoine Préfontaine
April 2nd, 2009 at 10:56 pm
Hi Brian,
Another question for you! Do you know if it’s possible to modify the script to allow the data to be sent back to multiple GA accounts/profiles?
Brendan.
April 3rd, 2009 at 6:57 am
And yet more feedback from me Brian!
I have discovered through testing that there is a way to ensure that the sub-domains of a website are not tracked as external links. You simply add “.” in front of the root domain – an example follows:
var extTrack = [".domain.com"];
and this will recognise http://www.domain.com, sub.domain.com, etc as internal links.
Brendan.
April 3rd, 2009 at 5:37 pm
Hello,
I’m trying to install this script on the website for the company I work for, but when I try to use the “addLinkerEvents()” command on the body OnLoad IE reports an error. (Object Expected) I’ve essentally copy/pasted your code(With the exception of the tracking code for GA). I’ve tried it in the header, and the footer, and I’m not very good with Javascript. Any help or suggestions would be appreciated. Thanks!
April 6th, 2009 at 3:20 am
Hi Brian,
What does “Only links written to the page (already in the DOM) will be tagged” mean? Can you give me an example?
Thanks,
Brendan.
April 14th, 2009 at 12:07 am
Hi Brian,
I have discovered that when you click on a “javascript:close();” button, the following shows up in the cookie string when you inspect it using HttpWatch “utmp=/outbound/undefined”.
Any tips on how to get around this issue? For now, I have created a filter that discards all pageviews with the name “/outbound/undefined”, but it would be good to stop it at the source.
April 14th, 2009 at 1:33 pm
Hi Brian
My tracking code goes to multiple accounts so pageTracker has been changed to firstTracker, secondTracker etc.
Will the script work if I change all the pageTracker references to secondTracker and just track downloads in one of the accounts, or will the script need modifying further than that?
Cheers
May 22nd, 2009 at 10:51 am
Hi Brian,
thanks for your scripts and your book (I’m reading it right now).
I’ve tried to implement the tracking srcipt for ga.js, and although I don’t get any script errors with firebug, I’m not getting any results in GA.
It’s been implemented in one of our websites: http://www.unctad.info/en/Public-Symposium-Website/
but I’m not 100% about the order of the ga-related scripts… Unless I have to wait a bit longer for the results to appear…
what do you think?
a
June 19th, 2009 at 5:21 pm
Trying to implement the outbound tracking as follows:
_uacct = “UA-xxxxxxx-x”;
urchinTracker();
(all this in the header)
and
But no errors on load – nor data tracked in GA
J
July 14th, 2009 at 10:13 am
Hello Christopher, Brian and all,
I am having the same problem as Christopher above (posting no. 70). He was never replied to and I haven’t found an answer to our problem anywhere else, so I was wondering whether Brian or anyone else would know what the /ext/ reporting on internal page views is caused by. I tried changing the HTML on my pages around a few times (not the javascript) but no setup bore any fruit.
Thanks
J
July 18th, 2009 at 12:08 pm
Jakub: Do you have the latest script? There was a bug I fixed a while ago which from memory sound similar to this. Ensure you get the latest file form the Hacks & Download section and subscribe to the script updates. Let me know if that solves the problem.
August 11th, 2009 at 7:54 pm
Hello all – thanks to everyone for providing such a great community resource. I’m about to add this script for a site with the GATC in the header. When using the ga.js version of GATC should the script src=”/trackExternal.js” type=”text/JavaScript” go after everything, or in between the two scripts like below? (it the software takes out the code than I’m referring to placing it in between the initial call to GATC and script that contains my UA number).
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”));
try {
var pageTracker = _gat._getTracker(“UA-xxxxxxxxx”);
pageTracker._trackPageview();
} catch(err) {}
Thanks in advance for any feedback!
August 11th, 2009 at 8:00 pm
i guess it did strip that out – should the /trackExternal line go after EVERYTHING or after the first script and before
try {
August 11th, 2009 at 8:42 pm
Rick: The script should go after everything. Thanks for the feedback. Spread the word
August 13th, 2009 at 12:59 pm
Hello Brian,
Many thanks for your reply and sorry for the long delay in responding. Holidays and all…
I have double checked I’m using the latest addLinkerEvents-ga.js and I am afraid I am. For the site above I am still getting that site’s own pages reported as /ext/ ones. I can send you a screenshot of my GA Top content report, if you’d like to see it.
Thanks
J
October 21st, 2009 at 8:31 pm
I have this script on a page with a product review script, and the two are conflicted somehow.
On this test page( http://www.avaline.com/ test-color-selector ), the “write your own review” link under the “Review” tab is supposed to expands= to show the review form, but upon hitting it a second time, it won’t run this part of the script which should make the form disappear again:
else {
document.getElementById( ‘review_form’ ).style.display = ‘none’;
reviewsLink.innerHTML = ‘[+]Write your own review’;
}
The full if-statement is attached to the onClick event of the element with an id of “write_review,” and when I click, in the bottom of the FF browser I can see “Waiting for http://www.google-analytics.com” for a split second. When I remove the addLinkerEvents-ga.js script, the full if-statement works, and allows the review form to collapse and expand as it should.
Any ideas as to what in particular is causing the conflict?
November 17th, 2009 at 4:35 pm
Hey Brian,
I’m moving from the urchin to ga version of tracking and am testing your script.
Two comments/questions:
1) Curious as to why this version doesn’t use setAttribute.
2) The commented out alerts for testing will never see the splitResult variable since it’s local to the onClick function that’s being written for each link. Am I missing something?
November 17th, 2009 at 5:06 pm
Actually, forget it. I was being dumb. Just dropped the alert into the function.
January 6th, 2010 at 9:25 am
Hi Brian,
Thanks for a brilliant solution.
I do have some problems however getting it to work.
I have added the script on our site http://global.atradius.com/, right after the ga.js code.
I also modyfied the body tag (added onLoad code), but I am unsure if this is necessary. No downloads are found in analytics.
Did we do something wrong here?
Thanks in advance,
Rene
January 20th, 2010 at 10:16 am
Rene: Make sure your GATC is for the latest ga.js and the script you are using is the latest from the site. In the post I describe the placement of the call to the /trackExternal.js file. This is critical.
I do need to update this post to reflect ga.js (not the legacy urchin.js), but you should be able to figure this out…
January 26th, 2010 at 4:13 pm
Hi !
Many thanks for maintaining this useful script.
I’m looking for a way to track external accesses to my PDF’s URLs, I mean when someone browses directly for the URL without loading any web page where the onClick event is located.
Is it possible? Any hints?
Many thanks !!!!!
January 29th, 2010 at 9:05 am
dgilperez: The “hack” to do this is describe in Ch9 of the book – Tracking Links to Direct Downloads. It has been updated for the second edition, but essentially the method remains the same.
January 29th, 2010 at 3:48 pm
Hi Brian !
Thanks for your answer. I have to say I have very little control on the web page I want to track their links. It’s a completely static one and have limited control over the code. That means I cannot add much JS nor any dynamic language.
If this can still be done, I’ll check your book out, although I’m quite in a hurry and worried about the time it’ll take to arrive at Spain.
Regards !
February 8th, 2010 at 2:21 pm
Regarding the direct downloads trick, great work Brian !!!
I’m enjoying your book.
Regards.