drmy1050 2014-06-26 09:31
浏览 48
已采纳

在特定页面的模板标题中包含Google实验代码

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!

  • 写回答

1条回答 默认 最新

  • dongmuyuan3046 2014-06-26 09:43
    关注

    Change your echo to:

    print file_get_contents(dirname(__FILE__).'/../exp.js');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败