OK, so this seems like a really simply problem, but I have yet to find a solution that accomplishes the following:

  1. Opens the link in a new window
  2. Tracks the event in GA (obviously)
  3. Doesn't trigger pop-up blockers (uses target="_blank" instead of window.open)

Most of the code I've seen, including Google's, doesn't take into account the case of opening in a new window - they just window location.href.

Even GAAddons (http://gaaddons.com/), which charges for commercial use, doesn't seem to work for me.

Perhaps, I'm missing something simple - I'd be relieved if so and would thank profusely whoever points it out to me!

If no one is able to provide an example, I'll post some of the test cases I've created to illustrate the problem.


  • 1
    please do post your test cases.
    – andrewk
    Jan 13, 2011 at 22:38

3 Answers 3


I might have misunderstood... does this not work?

<a href="/path/to/page" target="_blank" onClick="javascript: pageTracker._trackPageview('/path/to/page');">
  • This tracks a click to an (external?) link as a pageview, which may produce misleading metrics, if you want to accurately measure the website pageviews.
    – Osvaldo
    Sep 1, 2011 at 21:46

you can track events with google code.

put this code in your head

  function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);

then you could do as Hissohathair says above and put the onClick in every link

`<a href="/path/to/page" target="_blank" onClick="javascript: recordOutboundLink(this, 'entercategoryname', 'enternameofaction');">`

then in your google analytics goto "content > Event tracking"

you can also use javascript to add this automatically instead of adding to each link (I wrote a wordpress plugin that had this feature)


This is my favourite script for tracking outgoing links and opening in a new window. This is for the async version of GA.

It uses jQuery for convenience, and has a few 'not' classes that you can use as overrides.

<script type="text/javascript">
    $('a:not(.popupwindow)').filter(function() {
        var theHref = this;
        if (theHref.hostname && theHref.hostname !== location.hostname) {
            $(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
                var code=event.charCode || event.keyCode;
                if (!code || (code && code == 13)) {
                    var fixedLink = this.href;
                    fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
                    fixedLink = '/outgoing/' + fixedLink;
                    _gaq.push(['_trackPageview', fixedLink]);

Originally from here, I think:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.