Hacker News new | ask | show | jobs
by philipwalton 4644 days ago
Great question.

I use Google Analytics to track where users are clicking, specifically, when users click on external links that aren't otherwise tracked by the GA tracking code.

The problem is that if you click on a link that opens a new page, the functionality in the click handler may or may not run depending on how long it takes to redirect the request. That means that if I'm tracking clicks, I very well may be losing a good deal of data.

The alternative is to hijack the click event, wait for say 100ms, and then load the new page via JavaScript, but that also break the open in a new tab functionality.

In other words, there are no good options to track external click events via Google Analytics and still give users complete control over where the links go.

The best option I could come up with is to always open external links in a new tab, because that way the click handling code always has time to run. If you're curious as to what I'm doing, here is the code: https://github.com/philipwalton/solved-by-flexbox/blob/maste...

Anyway, it has nothing to do with wanting users to stay on my site. But I can't speak for why other people do this.

4 comments

Thanks for the detailed explanation. My unsolicited two cents is that forcing this behavior on site visitors isn't worth whatever value you get from the analytics data.

Perhaps I'm in the minority here, but I find this behavior to be offensive enough that I immediately stop reading and move on. Trading user experience for analytics data is not a good trade.

I also find forcing a new window/tab annoying and it makes sense not to when you're targeting a technical audience. However, I've just as often heard complaints from less-technical users who don't reflexively cmd/ctrl-click on links and for whom opening new windows in tabs is a feature, so even though forcing a new window can be annoying it's often a decision made to improve UX.
That's what the Back button is for.

Less-technical users don't need to know how to cmd/ctrl-click on links because even less-technical users know how to use the Back button. If they don't want to use the Back button and instead want to open links in new tabs, there are multiple mechanisms available to them (whether they know about them or not).

The reverse is not true: if you don't want to open a link in a new tab, but that behavior is being forced on you, there is no way to avoid it.

Regardless of the motive, this behavior is not a UX improvement.

In fact - opening a new tab BREAKS the back button. I've seen my father baffled when a new tab opened and he can't work out how to get back. (his tab bar has 300 open tabs as he never closes them).

So - opening in a new tab is a disaster for non-technical users and an annoyance for technical ones.

There might be some middle-ground where it's a good idea but I wouldn't bank on it.

I can confirm that many users of my father's generation (including him) are completely baffled by tabs. My grandfather, however, seems to have no problem. I don't know if that means anything.
Here's what I want to know:

When you open a new tab in Dolphin browser for Android, pressing the back button deletes it. Why shouldn't this be the default behavior for desktop browsers?

If you're in the minority I'm in the same one. This kind of design is rude, and the author's explanation about how he decided to be rude to his readers so that it would be more convenient for him to spy on them (tracking, analytics) tells me everything I need to know about him.
> The alternative is to hijack the click event, wait for say 100ms, and then load the new page via JavaScript, but that also break the open in a new tab functionality.

How does it break the open in new tab functionality? You can detect if the user was Cmd/Ctrl-clicking, or using the middle mouse button. I've done this on many sites and it works fine.

That's a good point. I suppose I could try to detect if the click was accompanied by a CTRL/CMD keypress and open in a new tab accordingly.

Still, the 100ms delay isn't ideal, and the open in a new tab paradigm greatly simplifies things.

Your analytics aren't as important as my user experience. :)
Perhaps if you were my customer, but you're just consuming the free content I've spent a good deal of time putting together :)
That doesn't include the possibility that the user right-clicks and selects "Open Link in New Tab"
The best option I could come up with is to always open external links in a new tab, because that way the click handling code always has time to run.

Ugh. That's awful.

The alternative is to hijack the click event, wait for say 100ms, and then load the new page via JavaScript, but that also break the open in a new tab functionality.

Is this true? ISTM that if you just have:

    <!doctype html>
    <p>Here's a <a href="http://www.google.com">link</a>.</p>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $('a').not('[href*="mysite.com"]').click(function() {
        alert('do some analytics stuff');
        // only after that's done do we let the navigation proceed
      });
    </script>
Your analytics stuff still gets done, and open in new tab still works. (As written, the analytics doesn't run when opening in a new tab, but ISTM you can just capture some mouse events to work in that case as well.)
i think opening in a new tab for an external link is completely reasonable (i don't care about GA tracking, but if that was one of the reasons, its also a reasonable reason).

Unless your computer is too slow to handle many tabs, having more tabs is almost always better!

I agree and don't like to be forced to open external links in the same tab. I've actually written a tampermonkey script to override the default behavior of HN and SO for this reason.

But I hesitate to make this comment as this debate can approach the intensity of the "vim/emacs" debate in its religious fervor.

I don't think 'be forced' means what you think it means.

If a site defaults its links to "same tab" then I can easily override that behaviour and open them in new tabs if I choose (just by using a different mouse button or holding down a key when I click)

If a site defaults links to "new tab", can you tell me the simplest way I can override that behaviour?

I will help you, despite your snarkiness :)

Just use greasemonkey or tampermonkey and a script like this:

    var a = document.getElementsByTagName("a"); 
    for (i=0;i<a.length;i++) { 
        if (a[i].target="_blank") { 
            a[i].target="_self" 
        }                        
    }
It should work for the majority of cases. You should be able to tweak it to handle any exceptions.
Drag the link and drop it into the URL bar of your browser.