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,672
  • 3
  • 19
  • 35

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
  • 191,768
  • 25
  • 236
  • 258
1

Because you're adding string after all.

It should be:

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

not

$(window.location).attr('href', 'url');
kidwon
  • 4,448
  • 5
  • 28
  • 45
1

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

Jerry Joseph
  • 1,002
  • 8
  • 14