Domanda

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!!

È stato utile?

Soluzione

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;
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top