Crownhill’s e-commerce website uses Google Analytics to provide some of our tracking metrics (it’s free, so it gives us instant ROI ;)). One major problem I had was that, when the order process moved from HTTP to HTTPS to protect our customer’s credit card details, browsers would throw up a “Mixed content” warning; something which would most likely confuse and, ultimately, scare customers away.

After a bit of digging around in Google Groups I devised a solution to the problem using a dash of PHP. I’ve provided the code below:


<?php if ($_SERVER['HTTPS'] == "off") { // if the server isn't in HTTPS mode, echo this ?>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
<?php } else { // otherwise echo the HTTPS URL for the javascript ?>
<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript">
<?php } ?>

</script>
<script type="text/javascript">
_uacct = "UA-xxxxxx"; /* Make sure you change this to reflect your GA account number! */
urchinTracker();
</script>


To explain a little. Basically, all I'm doing is checking to see what mode Apache is in, then adjusting the JavaScript source accordingly; to ensure that the browser doesn't present any Mixed-Content warnings to the customer - we certainly don't want to scare them off! The rest of the Javascript code is exactly the same as Google Analytics gives you; all I've done is change the link to the .js file.

Grab the code in a .txt file

Written by Phil Wiffen

Phil is an IT Professional working in Cambridge, England. He generally blogs about useful solutions that he comes across in his work/play.

Leave a Reply

Your email address will not be published. Required fields are marked *