homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

add GAnalytics code to php redirect script?
Possible to add analytics code to php redirect script?

 9:47 pm on Oct 5, 2012 (gmt 0)

Awhile back I made myself a simple php redirect script to make the changing of links across the site much easier to maintain. Is there any way at all possible to add Google Analytics code to track this page?

Since the headers are sent for the redirect it is causing a problem. I did find a post somewhere else about using obstart and obflush to echo the script code and while it did not cause any errors I was not able to verify in analytics.

Use the following link format:
<a href="goto.php?p=XXXXXX">XXXXXX</a>

$p = $_GET['p'];

switch ($p)

case "link1":
$link = "/link1.htm";

case "link1":
$link = "/link2.htm";

$link = "/default.htm";

header('Content-Type: text/html; charset=utf-8');
header("X-Robots-Tag: noindex, nofollow, noarchive", true);
header("Location: $link"); // URL



 11:35 pm on Oct 5, 2012 (gmt 0)

You can't run page-based analytics trackers on a redirect as the tracker requires a page to be loaded in a browser and there will never be a page executed for a redirect.

However, the redirects are tracked locally in your raw log file and get a report from there using some raw log file analytics.


 7:25 am on Oct 6, 2012 (gmt 0)

I posted a logger script here a few months ago that you add on the end of your redirect script and it writes a detailed log file of visitors that were redirected.

Your code would be a lot more compact if you used an array to hold the new and old URL pairs.


 5:43 pm on Oct 6, 2012 (gmt 0)


Thanks, I will look into that here in a few. I decided to take your advice and tidy up the current state of the script with an array. My only question is how do you handle the default link... the one where a valid $p in this case is not present? Just use an if in_array and else?

$p = $_GET['p'];
$link = array(




 7:11 pm on Oct 6, 2012 (gmt 0)

Yeah, if it's not in the array you need to do something.

I usually return 404 status using the PHP HEADER directive and a nice HTML page with useful on-site links.

I would not mass-redirect errors to a single page.


 5:25 am on Oct 7, 2012 (gmt 0)

I ended up doing this at the end of the file and it works great for anyone interested... thanks again...G1 will probably implement your stuff as well for tracking.

/*Send Headers*/

header('Content-Type: text/html; charset=utf-8');
header('X-Robots-Tag: noindex, nofollow, noarchive', true);

if (isset($link[$p]))
header('Location: '.$link[$p]); // Valid URL
header('Location: /default.htm'); // Invalid URL



 6:30 am on Oct 7, 2012 (gmt 0)


[edited by: g1smd at 6:31 am (utc) on Oct 7, 2012]


 6:30 am on Oct 7, 2012 (gmt 0)

Be aware your code generates a 302 redirect.

Are you sure that's right, or do you need 301?


 4:25 pm on Oct 7, 2012 (gmt 0)

It was my understanding a 302 was the preferred... the purpose of the script is to make it easier to change site wide sales links, trial links, etc. if need be.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved