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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?