I need to add Google Experiments code in two pages for an A/B testing.
The code has to be inside the header
section of the template, otherwise it won't work.
This is what I've tried on my template:
<?php
$titulo=substr($_SERVER['REQUEST_URI'], 0, 10);
if($titulo=='/experience'){
echo '<script type="text/javascript" src=".../exp.js"></script>';
}
?>
The if
statement is working OK, it is including the exp.js
script only to the pages I want.
This is exp.js
<!-- Google Analytics Content Experiment code -->
<script>function utmx_section(){}function utmx(){}(function(){var
k='15950497-1',d=document,l=d.location,c=d.cookie;
if(l.search.indexOf('utm_expid='+k)>0)return;
function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
indexOf(';',i);return escape(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/ga_exp.js?'+'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>')})();
</script><script>utmx('url','A/B');</script>
<!-- End of Google Analytics Content Experiment code -->
This is returning the error:
Uncaught SyntaxError: Unexpected token <
On line 2 of exp.js
I've also tried to echo the script directly on the template but it creates a syntax error with the quotes (I tried both simple and double).
Is there any way to fix this?
Thanks in advance for your help!