JackTheJack
JackTheJack

Reputation: 187

PHP Show Google Analytics

I use the fololowing code for all links going outisde my website:

if ($_GET["url"])
  {
    $urll = base64_decode($_GET["url"]);

    header("Location: ".$urll);

    exit();
  }

How can I show the Google Analytic's tracking code before the user is redirected so the page is tracked?

Could I just use a print statement?

Thanks.

Upvotes: 1

Views: 214

Answers (2)

drew010
drew010

Reputation: 69977

If you wanted to track the hit with GA, then you will have to do something other than a redirect using HTTP headers. With that, you can't guarantee that the page content will be parsed and executed. I think in most browsers any content would be ignored.

Instead you will need to serve a page (could be blank) and use a javascript and/or meta redirect and place the GA code in there so it gets called prior to the redirect.

Something like this should work:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="refresh" content="1;URL">
<title>Redirect</title>
</head>
<body>
Redirecting...
<script type="text/javascript">
   // put GA code here...

   window.location = "URL";
</script>
</body>
</html>

Upvotes: 2

Drahkar
Drahkar

Reputation: 1671

You can't display any characters before a HTTP Header. It will cause the PHP to throw errors. You should do all of your tracking collection prior to putting them onto the page that has the 'Location:' Header on it.

Upvotes: 0

Related Questions