Link to home
Start Free TrialLog in
Avatar of hrhs
hrhsFlag for United States of America

asked on

Including Google Analytics Code in 301 Redirect

I am using a 301 Redirect that looks like this....

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.org/filename.aspx");
exit();
?>


I want to add  Google Analytics code to report the traffic to our analytics...

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


I am having problems getting this code to work inside my PHP.  Can someone show me the what the combined should look like?

Thanks,
Dave
Avatar of nplib
nplib
Flag of Canada image

your header redirect is redirecting before the java can fire.

remove

header("Location: http://mydomain.org/filename.aspx");
exit();


and at the end of your javascript function put
window.location.href= 'http://mydomain.org/filename.aspx';
Just place the analytics code ahead of the php code.
if you put the javascript above header code you will get error that says something about output sent before header and it won't redirect.
The two concepts are incompatible.

The 301 redirect is in the header and the JavaScript is in the content.

The 301 tells any browser to move on before it even considers looking at the content.

You can't alter the header after you've written content as its already been sent, hence the error.

If you provide more information on what you want to do, then we may be able to provide an alternate solution.

If you have to do the 301 and want to track it happened you could either track based on the referrer or have your redirect URL include tracking information.
Avatar of hrhs

ASKER

Tiggerito,

I am looking to redirect properly so that I don't suffer from the engines for trying to trick.  It's really a technique I want to do to simplify a URL so I can keep track of the traffic from ads.

I don't want to just make the page URL the /pagename, but rather use the simplified URL to redirect to the true page name.  If I can log the use of the simplified URL, then can monitor the ROI from an ad and the actual (true) page traffic separately.

So, I was trying to redirect properly, but wanting to know when someone used the simple URL.

Does this make sense?

Thanks everyone for the advice.
One way to do this is to add tracking code in the redirect URL. This code will identify where the redirect came from.

I do this for QR Codes. My QRCode contains a short URL . e,g. min.to/QR1 This 301 redirects to a tracking URL using Googles campaign tracking:

http://www.google.com/support/analytics/bin/answer.py?answer=55578

Which integrates with Google Analytics.

Another recent idea I've heard is tracking via hash tags. e.g. the tracking url is domain.com/#from=QR1
I think the idea is that search engines will ignore the # info so you won't have indexing problems. For this to work your tracking system cannot ignore the # data.
Avatar of hrhs

ASKER

We run ads (billboards, radio, television, print)  that offer a URL like hrhonline.org/heart which then redirects to http://hrhonline.org/HealthServices/KeyServices/HeartCardiology.aspx

I am just looking to record the traffic to /heart in GA, then redirect the traffic legitimately without ticking off the engines for the redirect.

Tiggerito, I think the URL tool you've offered is for Adwords ad traffic.

Is there some legitimate proper redirect that the engines deem as proper other than 301 I should be using?
The URL tool is used by AdWords but is for Google Analytics. It's how Analytics can track Adverts.

You can invent your own personal campaigns with it.

On another note, if these are hand typed URLs it doesn't really matter what type of redirect you do. Those URLs should never be found by search engines, in fact you should block them from search engines to ensure your data gathering is not tainted. This means a 301 is not important as you're only doing a 301 to keep the search engines happy.

If that's the case then you could probably just do JavaScript based redirects which run after the analytics code has been fired.

There's probably better experts out there in how to track off-line adverts.

Personally, I would have those URLs block spiders and use 301s and Google's URL builder to track them via campaigns.

Avatar of hrhs

ASKER

I think you have a point Tiggerito.  I should be blocking them since the engines finding them will taint the math.  Good thought...  I was more worried about getting on some sort of black list because we actually used redirects.  I am not a PHP guy so I will try to see if I can get a file to work properly and forget about the 301.
Avatar of hrhs

ASKER

Can someone actually write the file the way it should look?  I have tried several combinations of the script before and even outside the php, I get errors.

There are the two things I need to wrap together.

<?php
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
window.location.href= 'http://hrhonline.org/HealthServices/Services/ImagingRadiology.aspx';
exit();
?>

Thanks...
What are the errors?

With the async Google code you can get race conditions. i.e. your location change may happen before the Analytics code gets loaded and run.

Google recommends the location change is done after a timeout:

http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55527
Avatar of hrhs

ASKER

I believe I found a solution.  I couldn't get it done with PHP.  Since I am hosting on a Windows platform, I started looking at a solution using an aspx page.  Here is what I got working working and is showing up in  Google Analytics.

<html>
<head>
<title>Radiology | Imaging | XRay | Hannibal, MO | Doctor</title>
<script type="text/javascript">
<!--
function delayer(){
window.location = "http://hrhonline.org/HealthServices/Services/ImagingRadiology.aspx"
}
//-->
</script>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-xx']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>
<body onLoad="setTimeout('delayer()', 1)">

</body>
</html>

I named the page default.aspx and oddly it's showing up in GA as index.html.

Thanks for everyone's help trying to get this working in PHP.

Dave
You can not send a 301 status code via Javascript. Since the HTTP status code of the page will remain 200 OK, most search engines will continue to think the location is valid. Any page rank of the old location will not be transferred to the new location.
ASKER CERTIFIED SOLUTION
Avatar of gwkg
gwkg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guy Hengel [angelIII / a3]
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.