Google Website Optimizer - How do I add the google code?
staino83 - March 28, 2008 - 23:31
Hi
Im trying to run Google Website Optimizer http://services.google.com/websiteoptimizer/ with my drupal website
I dont know where to insert the code for the 2 pages I want do AB testing on. Is there a module or can I just paste the code into a page somewhere?
The code needs to be inserted at the start of the page and at the end of the page source code.
Thanks
Sam

PHP snippet w/ drupal_add_js
If you're running the test on node pages and it's okay to set the input format to PHP code, you can insert a snippet in the body field to call the drupal_add_js function.
If you're not familiar with how PHP snippets work, have a look at the handbook page before using this approach:
http://drupal.org/handbook/customization/php-snippets
This snippet lets you put the javascript into a variable and then passes it to Drupal's drupal_add_js function to be included in the header or footer of the page:
<?php
$gwo_control = "
---this is supposed to be your control javascript from Google---
";
drupal_add_js($gwo_control , 'inline', 'header', FALSE);
$gwo_tracking = "
---the tracking piece of javascript from Google---
";
drupal_add_js($gwo_tracking , 'inline', 'footer', FALSE);
?>
IMPORTANT: The javascript provided by Google needs a few adjustments to work correctly in a PHP snippet.
First, you'll need to remove the first <script> and the last </noscript> tags from Google's code (otherwise you'll get double tags when drupal_add_js automatically wraps the code in <script></noscript> tags.) Any script tags in the middle of the Google code should be left in place.
Second, you'll need to look through the Google code and escape any double quotes (") by inserting a backslash before every occurrence (e.g., change src="blah blah" to be src=\"blah blah\"). If you miss any, the code won't be correctly saved into the variable and you'll probably get an error when the page loads. Single quotes (') do not need to be escaped.
Here's an example of the snippet with some Google code:
<?php
$gwo_control = "
function utmx_section(){}function utmx(){}
(function(){var k='9999999999',d=document,l=d.location,c=d.cookie;function f(n){
if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return c.substring(i+n.
length+1,j<0?c.length:j)}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
d.write('<sc'+'ript src=\"'+
'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'
+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'\" type=\"text/javascript\" charset=\"utf-8\"></sc'+'ript>')})();
";
drupal_add_js($gwo_control , 'inline', 'header', FALSE);
$gwo_tracking = "
if(typeof(urchinTracker)!='function')document.write('<sc'+'ript src=\"'+
'http'+(document.location.protocol=='https:'?'s://ssl':'://www')+
'.google-analytics.com/urchin.js'+'\"></sc'+'ript>')
</script>
<script>
_uacct = 'UA-9999999-9';
urchinTracker(\"/9999999999/test\");
";
drupal_add_js($gwo_tracking , 'inline', 'footer', FALSE);
?>
Obviously you'll need to use your code from Google, as it includes your account and test info. Also, your "B" page doesn't need control code, so you'll want to omit those lines from the snippet you include on that page.
JM
I've just about finished
I've just about finished writing a small module to handle Google Website Optimizer tests - I'll submit it to CVS shortly.
http://www.schoonzie.com/