0

I need to track a dynamic button that redirects to another url. I need to track everytime this happens and know exactly to what url the user is beeing redirected.

The button code:

 <a target="_blank" href="/out.php?url=<?php echo urlencode($this->product['from'])?>">
 <img src="http://xxx.com/data/images/buy.jpg" alt="buy"/>
 </a>

File "out.php"

 <?php
 $url = urldecode($_GET['url']);
  header("Location: ".$url);
  exit;?>

I want that for example if a user clicks on buy, it redirects to "out.php" and shows a message like "U are beeing redirected in a few seconds" and then sends to the url.

I need this in order to track via analytics how many times the user landed on that page and where they came from, getting some metrics about outbound clicks.

Anyone knows how to do it?

Thanks in advance!!

Chris Fadu Uba
  • 133
  • 1
  • 1
  • 10

2 Answers2

0

If you are using event tracker to determine which link was clicked, you can filter all the links you want to track and add an event call to the links:

var links = document.getElementsByTagName("a");

for(var i=0,l=links.length;i<l;i++){
   if (filter your links to only the external ones){
         links[i].onclick = function(){
             _gaq.push([your track event code here]);
         }
   }
}

Since your links have target="_blank"; there is no need to add a delay so the gaq call is complete. If they don't open in a new window here is what you can do:

links[i].onclick = function(){
    _gaq.push([your track event code here]);
    var url = this.href;
    setTimeout(function () {
        window.location.href = url;
    },500); // redirect in 500 milliseconds

    return false;
}
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • Thanks for replying Ibu! Every link opens in a new window. Where do I need to add this code? This is what I have now in the header: – Chris Fadu Uba Nov 20 '12 at 19:40
  • (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); – Chris Fadu Uba Nov 20 '12 at 19:41
  • you can add it at the end of your document, or an external file. as long as it runs when the document is loaded – Ibu Nov 20 '12 at 20:03
0

The simplest way to accomplish this is to use Google Analytics. It will let you track outbound clicks, redirects, etc.

Based on your current implementation, just save the variable $url to a database.

Another way is by using AJAX, listening for the javascript event window.onbeforeunload, have it call an PHP script (through AJAX), and save the URL to a database. This will keep the user from having to see the "You are now leaving" page.

Apollo Clark
  • 806
  • 9
  • 16
  • Thans apollo! The problem is that analytics doesn't count all the outbound links. I tried from different computers and doesn't track the outbound links. Some outbound clicks are beeing tracked, from another user, but not all. So I decided to implement an alternative because it's no reliable. I like your solution avoiding "you re now leaving", it was an idea to track that site. I know nothing about Ajax, is there a simplier way u can recommend me? Thanks!! – Chris Fadu Uba Nov 20 '12 at 19:32
  • Chris, make sure you're waiting at least 24 hours for Google Analytics to record your stats. To make absolutely sure that Outbound Links are being tracked, use the Google Analytics API Javascript option, details are here: [Outbound Links](http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920) – Apollo Clark Nov 26 '12 at 16:08