3

I'm trying to delay outgoing links when clicked, so that googles event tracking have time to occur.

I wrote the following code, but I'm unsure of how to pass my variable to window.location. It just adds it as string "url" and not the link adress. What am I doing wrong?

$("a.private-product-order-button").click(function(e) {
   e.preventDefault();
   _gaq.push(['_trackEvent', 'Order buttons', 'Click', 'Service']);
   var url = $(this).attr("href");
   setTimeout(function() {
      $(window.location).attr('href', 'url');
      }, 200);
});
Johan Dahl
  • 1,492
  • 2
  • 18
  • 34

3 Answers3

2

No need to use jQuery to set a property of the location object (and no need to use jQuery to get href property of the anchor object):

$("a.private-product-order-button").click(function(e) {
    e.preventDefault();
    _gaq.push(['_trackEvent', 'Order buttons', 'Click', 'Service']);

    var url = this.href;
    setTimeout(function() {
        window.location.href = url;
    }, 200);
});
dfsq
  • 187,712
  • 24
  • 229
  • 250
1

Because you're adding string after all.

It should be:

$(window.location).attr('href', url);

not

$(window.location).attr('href', 'url');
kidwon
  • 4,270
  • 4
  • 26
  • 45
1

Use $(window.location).attr('href', url); without the quotes around url.

Jerry Joseph
  • 932
  • 7
  • 14