Add to Google Add to Bloglines Add to Newsgator Add to Yahoo! Contact ROI Revolution Sign Up for the ROI Newsletter Unofficial Google Analytics Blog Feed Unofficial Google Analytics Blog
Unofficial Google Analytics Blog by ROI Revolution

Categories Search This Blog Blogroll Archive

| |

March 1, 2007

Secure vs. Nonsecure Pages in Google Analytics

By Shawn Purtell, Google Analytics Support Tech

padlock.jpg

During our recent training series, we received a lot of questions concerning how to add the Google Analytics tracking code to a site. Now there are lots of different things to think about before doing this, but a very important and basic one is the security level of your domain. If you have a shopping cart or some kind of secure lead collector, then there's a good chance that part of your site resides on a secure server, which you may recognize as https:// instead of the normal http://

Why is this important? Because the regular Google Analytics script can cause errors on secure servers, that's why! Now there are long ways to make sure you don't run into this problem, and there are easy ways. Well, unless you're trying to build character or torture yourself (like all of you who aren't using a separate include for your Google Analytics script), then the easy way is the way to go. Let me share the two easy ways with you...

Easy way #1: Write some code so that you don't ever have to think about it.

Eh? Basically, you have the code check for the https://. It then inserts the correct version of the urchin.js call, depending on whether the page is secure or not. If you aren't a coder, feel free to take this code and use it for your own site. Just make sure you enter in your own unique Google Analytics account ID. It's not really that complicated, so any webmaster worth having will be able to use it.

In JavaScript:
<script type="text/javascript">
if (window.location.protocol == 'https:') {
document.write('<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></sc'+'ript>');
}
else {
document.write('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></sc'+'ript>');
}
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXX-1";
urchinTracker();
</script>

In PHP:
if($_SERVER['HTTPS'])
{
$google_analytics_url = "https://ssl.google-analytics.com/urchin.js";
}
else
{
$google_analytics_url = "http://www.google-analytics.com/urchin.js";
}
echo "
<script src='$google_analytics_url' type='text/javascript'>
</script>
<script type='text/javascript'>
_uacct = 'XXXXXX-1';
urchinTracker();
</script>
";

You might argue that this doesn't really look so easy. However, this one code would be all you need on every single page, regardless of whether or not those pages are secure.

Easy way #2: Just use the secure version of the code at all times

Really? Yup. The secure version of the code will work on both secure and non-secure pages:

<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXX-X";
urchinTracker();
</script>

If you need to verify that this will work, visit this page to hear it straight from Google.

Keep in mind that although either of these solutions will fix any secure page tracking issues you may have been experiencing, it's not a cure-all for those of you that have the secure part of your site on a separate domain. You'll still need to use the methods outlined here.

Posted by Shawn Purtell at 8:39 AM









Filed under: ,

TrackBack

TrackBack URL for this entry:
/mt/mt-tb.cgi/202.

Comments

Thanks for the information on the "harder" way. I had never seen it done like that, as I just instituted the secure version on all pages like your second method.

Posted by: Jeff Lawrence at March 5, 2007 7:00 PM

No problem. You'd be surprised how often we encounter problems with this. I just thought I'd cover all the options, as both methods work just fine.

Posted by: Shawn Purtell at March 6, 2007 2:58 PM

Since the second way is so easy, can you please describe when it makes sense to use the first one? Will it solve Jeff Weidner's problem, http://tech.groups.yahoo.com/group/webanalytics/message/9739

Thanks. Robbin

Posted by: Robbin Steif at March 8, 2007 9:41 PM

Hi Robbin,

Really both of these methods require the same amount of effort for people who use separate includes for their Google Analytics script (which ideally would be everyone). One may be harder to understand for non-coders, which is why I recommend the second way. As for Jeff Weidner's problem, the security warning he was getting was a direct result of using the non-secure script on a secure server. As for Google 'looking' for event.company.org, my guess is that he has set his _udn variable incorrectly, as it should be set to 'none'. I wasn't able to look at the actual code, but I've seen it happen before.

Posted by: Shawn Purtell at March 9, 2007 11:08 AM

Post Your Comments

Post a comment

Contacting ROI Revolution

Contact Us

Address:
3109 Poplarwood Ct.
Suite 219
Raleigh, NC 27604


Authorized Google Analytics Consultant

Google Analytics Certified Service and Support Consultant


Free Webinar!

Join a Live FREE Google Analytics Training Webinar
Register now to learn how to
turn Google Analytics into
pure profit! You'll learn how to use many of the key reports, a ten-step system for properly configuring Google Analytics on your website, and how to apply what you learn to the attainment of your online goals. Register for our free Google Analytics training webinar today.


Subscribe to the
ROI Revolution Google
Analytics Newsletter today

Free monthly email publication that contains valuable, practical tips, secrets, and much more! Subscribe to our free Google Analytics newsletter today.


Looking to Purchase
Urchin 5 Software or Fee
Based Support?

We are a Urchin software certified service and support partner! Buy Urchin 5 software, profile packs, load balancing modules, campaign tracking, or fee-based support today.