Welcome to WebmasterWorld Guest from 107.21.159.218

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?

     

mihomes

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

10+ Year Member



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();
?>

incrediBILL

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

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



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.

g1smd

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

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



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.

mihomes

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

10+ Year Member



@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',

);

g1smd

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

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



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.

mihomes

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

10+ Year Member



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();
?>

g1smd

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

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





del

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

g1smd

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

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



Be aware your code generates a 302 redirect.

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

mihomes

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

10+ Year Member



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?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month