In order to track the overall user clickstream, I'd like to fire a javascript event, if the user right-clicks, and select "Open in new Tab" (or middle-clicks in most browsers) on a link. Most of these links are linking outside of my site, and I'd like to interfere with overall browser experience (such as: status bar, etc) as little as possible. What options are there to solve this?
There are many ways that a user can create a new tab in a browser:
- Middle click
- Context menu
- Mouse gesture
- "New tab" button on the toolbar
- "File" > "New tab"
Unfortunately there is no way to handle all these and potentially more user actions that could create a new tab.
You could do a simple server redirect and log the hits that it gets
Or does it have to be js?
If you're looking at ways to see outbound link hits, you could try the following:
Use a script, example link.php?src=http://www.example.com that increments a counter per IP & User Agent combo when clicked. This however doesn't look very good in the status bar. It could also be saved by web crawlers.
Use unobtrusive JavaScript to attach event handlers on links that are external. You could determine if they are external if the hostname is present and doesn't match the one you are on. You could then use this event handler to save the href, prevent default of click event, to increment a number much like the first script and then send the window.location to the actual href. This of course fails without JavaScript enabled/supported.
Middle click capturing does work.
You
have to check the browsers version (ie6 doesn't open anything on middle click),
have to use mousedown and mouseup to check if these two events happen on the same element,
have to check which button was pressed. (jQuery "which" function, for example)
If mousedown and mouseup happen on the same element, a new window opens, so you'll know that your link was clicked.