I have a script that uses PHP to automatically redirect a user to an outbound URL. I'm trying to incorporate Google Analytics into the script so that I'm tracking outbound links. It says to add a JavaScript attribute like this:

<a href="http://www.example.com" onClick="javascript:urchinTracker ('/outgoing/example_com');">

But the redirect doesn't use the <a href> tag, it looks like this:

if(headers_sent()) {
$outbound_url = "http://".$row['URL']; 
echo "<script type='text/javascript'>location.href='$outbound_url';</script>";
} else {
header ("Location: " . $outbound_url);
}

Can someone tell me how I can "merge" the two?

    Not going to happen. Javascript runs client side, header() redirects are controlled server side.

      Except from the other half of JavaScript, SSJS (Server-Side JavaScript)...

      http://en.wikipedia.org/wiki/Server-side_JavaScript

      I'm guessing you want to run the 'javascript:urchinTracker ('/outgoing/example_com');' JavaScript function, as well as redirecting the user? I can't think of a nice way to do this at the moment, but you could either replicate the urchinTracker () in PHP code, or even do something like this:

      <?php
      $outbound_url = "http://".$row['URL'];
      
      echo "<script type=\"text/javascript\">\n";
      echo "urchinTracker('/outgoing/example_com');\n";
      echo "</script>\n";
      
      header ("Location: " . $outbound_url); 
      ?>

      Might not work, but what i'm trying to achieve is first running the javascript, hopefully it doesn't mess with the headers, then redirecting...

        duckduckgoose wrote:

        I have a script that uses PHP to automatically redirect a user to an outbound URL. I'm trying to incorporate Google Analytics into the script so that I'm tracking outbound links. It says to add a JavaScript attribute like this:

        <a href="http://www.example.com" onClick="javascript:urchinTracker ('/outgoing/example_com');">

        But the redirect doesn't use the <a href> tag, it looks like this:

        if(headers_sent()) {
        $outbound_url = "http://".$row['URL']; 
        echo "<script type='text/javascript'>location.href='$outbound_url';</script>";
        } else {
        header ("Location: " . $outbound_url);
        }
        

        Can someone tell me how I can "merge" the two?

        I guess you could use meta tags to redirect the user... that way you'd be able to use your javascript.

        <meta http-equiv="Refresh" content="1; URL=<?=$outbound_url?>">

        Then you can make a call to the javascript seperately, I guess.
        shrug

          Write a Reply...