A web marketing professional naturally thinks a lot about the incredible diversity of a site’s visitor demographics. Old and young, male and female, well educated and not, affluent and not… but how much thought has been given to the right-clickers? No, not the right-handed, the right-clickers. Right-clickers are those who right-click on a link to open a page in a new tab, to save a file in a specific location or to copy the link.
Sure, right-clickers are probably more technically advanced users representing a minority of a site’s web visitors, yet still, tracking right-clickers has been gnawing at me for a while. The summer “break” was just what I needed to bring focus to an issue potentially impacting many Web Analytics data collection scripts. These are scripts are used by JavaScript based systems like Google Analytics to track website activities not already included in basic page tracking, such as file downloads and outgoing link clicks.
The false onclick assumption: onclick really means “on left click”
Consider the many JavaScript files available to automatically add Google Analytics tracking code to links in page. Each of them currently uses the HTML event syntax onclick to trigger the Google Analytics tracking code. Completely understandable, onclick is specified by the Google Analytics documentation. The problem is that onclick really means on left click, at least as implemented in major browsers such as Internet Explorer, Firefox, Safari, Opera or Google’s Chrome. Tracking code attached to the onclick mouse event won’t trigger if a user clicks the right mouse button or even the center mouse wheel button – all the right clicks on external links or file downloads will be lost (although you’ll still find downloaded file information in your weblogs).
The onmouseup and oncontextmenu solutions
Fear not, the fix is rather simple. Just change onclick to onmouseup. onmouseup, part of a w3c standard for HTML events, triggers when a user releases a mouse button, regardless whether it is the left, center or right mouse button.
Do keep in mind that some users may right-click on a link for an external site or a download without actually choosing to view the site or download the file. Right click simply opens the context menu in the browser. A user could close the menu without making a choice. Thus, tracking right-click events will move tracking from under-counting to some over-counting of clicks, but overall data collection accuracy should improve.
There is also an unofficial oncontextmenu event which triggers in all major browsers (it now works in Opera, the last holdout) when the right mouse button is clicked. oncontextmenu could be used together with onclick to distinguish between left clicks and the potentially less certain right-clicks. That would however leave middle button clicks uncounted (there isn’t a separate event handler for middle wheel button clicks).
Keyboard clicks tracking and onkeyup
Not everyone uses a mouse, there are a few hearty souls out there who rely on keyboard strokes because they’re faster or easier to use than a mouse. Major browsers will trigger an onclick event when the enter key is pressed to select a link but will, not surprisingly, ignore the onmouseup event. There is an onkeyup event, but it triggers for multiple key strokes which may apply to a link, such as simply tabbing through links. That would pollute the web analytics data and cost calls to the web analytics server. In the case of Google Analytics, there is currently a limit of approximately 500 calls every session; SaaS / ASP Web Analytics tools such as Omniture, Coremetrics and WebTrends often charge based on usage.
Web Analytics Link Tracking Recommendation
To track all mouse clicks and enter key clicks on download and external links, add both onclick and onmouseup event handlers to tracking scripts but be careful to avoid them both executing on a left mouse click – double counting would occur. Sites tagging rich internet applications (RIA), like Ajax, Flash or Sliverlight may want to check their current tagging. The following summary table may help:
| Event Trigger(s) | Link Tracking |
|---|---|
| onclick | Left mouse click and enter key |
| onmouseup | Left, center and right mouse clicks |
| oncontextmenu | Right mouse clicks |
| onkeyup | Keys which touch a link, e.g. enter, tab… |
Table 1: Summary of link event triggers for Web Analytics tracking.
Try this at home: click here links to demonstrate onclick, onmouseup and oncontextmenu
Each of the above links has been tagged (or the pompous sounding instrumented if you prefer) with the indicated JavaScript event handler. Activating a link as suggested will trigger an alert message if supported by the browser.
Have fun clicking!
Similar Posts:
- Comparison of Google Analytics / Urchin Tracking Scripts
- Google Analytics Event Tracking – New Option to Avoid Impacting Bounce Rate
- Use events to track 404 page not found errors in Google Analytics
- Creating Search Engine Friendly Drop-down menus using CSS
- Web Analytics Embedded JavaScript Page Tracking Code: Place at the top or bottom of the page?
Registration is now open for the next SEO Course (June 13 and 14) and Google Analytics Course (June 20 and 21) in Milan. Don’t miss the opportunity!





Hi Sean,
The “onclick” issue is one that I am surprised the major analytics vendors have not considered or solved.
I have written some JS for my GA implementation that automatically tracks mouseup and keyup for file downloads, external links and mailto links.
I have also written a filter that excludes all keyup action apart from the Enter key.
You can check the JS at:
http://www.diird.vic.gov.au/__data/assets/js_file/0009/119592/gaAddons-diird.js
The script also tracks all clicks (“onclick” only) on HREFs as well as the “load” of all form submit buttons. This is for “Site Overlay” tracking purposes, as the default GA Site Overlay report is pretty underwhelming.
I’d be interested to hear if you have any feedback or comments…
Brendan.
Using onmouseup to track right-clicks (with the assumption that most people do this in order to open a link in a new tab) in addition to left-clicks is great. Basically, this method is just what I’m looking for. There is one problem though: As soon as the user right-clicks the link is followed in the current tab. Obviously the user right-clicked so that they could open the link in a new tab. Is there any way to fix this? Any help would be appreciated.
Thats for sharing this it was very helpful! I had been looking for a good way to track music downloads from our site as most people hit “right-click” and “save as…”. This should do the trick.