Making the OSCommerce Google Analytics Module Work with BTS

Recently a client using a heavily modified OSCommerce installation needed the advanced Google Analytics ecommerce tracking installed on their site. This wasn't an out of the box install as their site used the Basic Template Structure mod ( from here out referred to as BTS ). I will detail below what I had to do different from the install instructions that came with the Google Analytics module to make it work with BTS. I thought this post might help others who might run across the same problem.

Step 1:
Download and unzip the OSCommerce Google Analytics Module. (The version I used was labeled as "osCommerce Google Analytics module v.1.2.2" )

Step 2:
Upload the analytics folder module to your site in the catalog/includes/modules/ directory.

Step 3:
Backup a copy of your template file (should be located at "catalog/templates/YOURTEMPLATENAME/main_page.tpl.php ).

Step 4:
Open up a copy of main_page.tpl.php.

Step 5:
Replace the </head> tag with this code which will detect whether you are on the checkout_success.php page when the page loads. Which should only happen after a successful sale on your site. Make sure to replace the "UA-xxxxxx-x" code with your analytics tracking id.

Here is the code:

<?php
	//Code for google analytics to see if we're on checkout_success.php page
	if(strstr($PHP_SELF, "checkout_success.php")){ 
	
		
	/* Modified Conditional code added thank to  rrodkey and bfcase */
		if ($request_type == 'SSL') {
			echo '
			<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript">
			</script>
			<script type="text/javascript">
			_uacct = "UA-xxxxxx-x";
			urchinTracker();
			</script>
			</head>
			<body onLoad="javascript:__utmSetTrans()">
			';
		} else {
			echo '
			<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
			</script>
			<script type="text/javascript">
			_uacct = "UA-xxxxxx-x";
			urchinTracker();
			</script>
			</head>
			<body onLoad="javascript:__utmSetTrans()">
			';
		}
		// osCoders.biz - Analistics - end
	}else{ 
	        echo '
		        </head>
		        <body>
		  ';
	} 
	?>

What this code does is check to make sure we're doing a successful checkout ( Since bts uses the same template for every page on the website. ). If it is the checkout_success.php page it loads the Google Analytics code -- otherwise it just loads a standard tag. It also checks whether we're over a secure connection or standard http connection so the end user doesn't get warnings when purchasing about possibly insecure connections.

Step 6:
Paste this code above the </body> tag.

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-xxxxxx-x";
urchinTracker();
</script>

<?php
	 //Code for google analytics to see if we're on checkout_success.php page
	 if(strstr($PHP_SELF, "checkout_success.php")){ 
		// osCoders.biz - Analystics - start 
			include(DIR_WS_MODULES . 'analytics/analytics.php'); 
		// osCoders.biz - Analistics - end
	 } 
?>

Step 7:
Save the file a make sure it works.

Your mileage may vary on getting this to work for your specific install. I hope this helps.