Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

Thanks.

share|improve this question
1  
please do post your test cases. –  andrewk Jan 13 '11 at 22:38

3 Answers 3

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">
$(document).ready(function(){
    $('a:not(.popupwindow)').filter(function() {
        var theHref = this;
        if (theHref.hostname && theHref.hostname !== location.hostname) {
            $(theHref).not(".noAutoIcon").addClass("offSite");
            $(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]);
                };
            });
        };
    });
});
</script>

Originally from here, I think:

share|improve this answer

you can track events with google code.

put this code in your head

<script>
  function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);
  }
</script>

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)

share|improve this answer

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

<a href="/path/to/page" target="_blank" onClick="javascript: pageTracker._trackPageview('/path/to/page');">
share|improve this answer
    
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 '11 at 21:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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