Welcome to WebmasterWorld Guest from 54.196.214.35

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


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.


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


$p = $_GET['p'];

switch ($p)
{

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

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

/*Default*/
default:
$link = "/default.htm";
}


header('Content-Type: text/html; charset=utf-8');
header("X-Robots-Tag: noindex, nofollow, noarchive", true);
header("Location: $link"); // URL
exit();
?>
11:35 pm on Oct 5, 2012 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14624
votes: 88


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)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


@g1smd

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(

'first'=>'/link1.htm',
'second'=>'/link2.htm',

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


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
}
else
{
header('Location: /default.htm'); // Invalid URL
}

exit();
?>
6:30 am on Oct 7, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0




del

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

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


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.

Thoughts?