views:

942

answers:

4

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?

+1  A: 

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.

Andrew Hare
Thank you for your answer. I'm only interested in new-tab events originating from a link on my site, so most of these do not apply. Any way to achieve that?
Silver Dragon
Well that does change things a bit - I think where you might have some trouble is with mouse-gestures and folks with multi-button mice. Remember that folks may have many different ways to open a link in a new tab. Good luck :)
Andrew Hare
A: 

You could do a simple server redirect and log the hits that it gets
Or does it have to be js?

cobbal
+2  A: 

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.

alex
+1, These are the two ways most commonly used.
Paolo Bergantino
Thanks Paolo... I recommend these options from the comments on the OP.
alex
A: 

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.

Vili