Question about analytics placement

SuperPhil

New member
Jun 24, 2009
67
0
0
www.imperatodesign.com
Hello, I'm trying to have a site redirected but still have a analytics report

current code
Code:
<?php header("Location: http://domain.com")?>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("");
pageTracker._trackPageview();
} catch(err) {}</script>

this currently isn't working, and i was wondering if you guys knew of a solution?
 


You must allow the page to load first if you are going to use a javascript analytics solution.


header("Refresh: 2; URL=http://example.com");

This will load the page, wait 2 seconds and then redirect the user.
 
You're currently doing a 301 redirect, which is accomplished by sending an HTTP header ("Location: blah"). HTTP headers must precede all body contents, and are interpreted first, so by the time your browser reads ".com", the <script> tag is already useless [will never be interpreted].

Instead, you need to load the body, evaluate the page contents (which entails loading the analytics), and then finally process your redirect.

Try:
HTML:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("");
pageTracker._trackPageview();
} catch(err) {}

window.onload(function doRedirect() { window.location.href="http://domain.com"; });
</script>
 
I would use a javascript redirect below the analytics code, so it has time to execute before the redirection. Having said that, I would use uplinkeds method. Plus as a bonus, the user wont have to wait 2 seconds, only the time it takes to load the page.