0

I am using the code below to track outbound links in google analytics ( found it somewhere on this site ). I have 2 issues coming up:

  1. Sometimes, the e.currentTarget.host part of the output shows my own domain - instead of showing the domain where the click leads to. Any idea why my domain shows up on occasion ?
  2. Is it possible to modify this code to do the following (1) force link to open in new window and (2) track the outbound click event as shown.

    $(function() {
    $("a").on('click',function(e){
        var url = $(this).attr("href");
        if (e.currentTarget.host != window.location.host) {
            _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
            if (e.metaKey || e.ctrlKey) {
                var newtab = true;
            }
            if (!newtab) {
                e.preventDefault();
                setTimeout('document.location = "' + url + '"', 100);
            }
        }
    });
    

    });

user2022284
  • 413
  • 1
  • 9
  • 22

2 Answers2

0
  1. By using the code you have written, all the <a> elements of your site are affected. That should obviously include some links to your own site, except if your site has no menu, which I believe it obviously does.
  2. Maybe you can use jQuery to add a target="_blank" attribute to all your links, after loading the page.
mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
0
  1. The code compares the domain in the link with the window domain: e.currentTarget.host != window.location.host -- If your domain has a www prefix (like www.domain.com) but have some links without it (like domain.com/link.html), they'd be considered an external link.
  2. The original code uses a delay before setting document.location to allow time for the _trackEvent's tracking request. Since you want all off-site links to open in a new window, the delay can be eliminated.

The following code should work regardless of any www or sub-domain prefix:

jQuery(function($) {
  $('a[href^="http://"],a[href^="https://"]')
    .not('[href*="mydomain.com"]')
    .click(function(e) {
      var url = this.href;
      _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
      e.preventDefault();
      window.open(url);
    })
});
  • $('a[href^="http://"],a[href^="https://"]') -- select links that start with http:// or https://. This should be include all outbound links.
  • .not('[href*="mydomain.com"]') -- throw out any links containing mydomain.com, just in case there are any internal links that start with http://...
  • e.preventDefault(); -- prevent link from being followed normally.

Also, if you're using the current, asynchronous Google Analytics code, you can shorten the _trackEvent call to

_gaq.push(['_trackEvent', 'Booking', "Outbound Links", e.currentTarget.host, url, 0]);
mike
  • 7,137
  • 2
  • 23
  • 27
  • Mike - I think you found the issue. My site is mostly .mydoamin.com. I suspect there are a few stragglers with www.mydomain.com. Is it possible to configure the code to account for both scenarios ( or even any subdomain for that matter?) - This implies subdomains will also be seen as external links. Finally, I am assuming that with the link opening in a new window, there is no need to implement the setTimeout? – user2022284 Feb 28 '13 at 01:22
  • @user2022284 -- correct about not needing the setTimeout delay because of opening in a new window. – mike Feb 28 '13 at 05:02
  • What do you think about the comment on the link below which says its not so good to hardcode the domain name in this case? Wondering what you think about it: ( see last paragraph in blog post http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/) thxxx – user2022284 Feb 28 '13 at 12:54
  • the current code is opening the external link in another window but also in the same window! – user2022284 Feb 28 '13 at 13:07
  • To clarify, links with a target="_blank" attribute work fine. Other links without it ( which is a majority ) is what opens in the same window and also another window. – user2022284 Feb 28 '13 at 13:24
  • @user2022284 - good catch, `e.preventDefault();` was missing from the click function. Without it, the link would be opened normally and in a new window. – mike Feb 28 '13 at 15:18
  • @user2022284 -- using `var domainName = "domainname.com";` does help make the code more self-documenting. Using `window.location.hostname` takes more code because you'll have to strip off any www or subdomain prefixes. – mike Feb 28 '13 at 15:38
  • @mike- your modification is working great. Thx for the help on this. So, why is window.location.hostname better than hardcoding the domain? Is it a security issue? ( despite the code bloat ) – user2022284 Feb 28 '13 at 16:36