2013-02-19 115 views
1

我怎么能把这个PHP变量作为一个自定义变量到这个JavaScript代码?谷歌Analytics自定义变量从PHP变量(动态数字)

我的每个访问者都有一个在我的登陆页上唯一生成的动态数字。 这是可用的PHP变量;

$点击次数

我已经下面的代码GA(JavaScript)的工作;

<script type="text/javascript"> 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']); 
    _gaq.push(['_trackPageview']); 

    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

</script> 

我的问题:如何将这个php变量作为自定义变量放入此javascript代码中?

回答

3
<script> 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']); 

    _gaq.push(['_setCustomVar', 1, 'clicknumber', <?= $clicknumber ?>, 2]); 

    _gaq.push(['_trackPageview']); 

    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

</script> 

的更多信息:https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables#setup

+1

也许没有必要为了呼应整个脚本标签。只需使用'<?= $ clicknumber?>''在其中打印数字。 – RayViljoen 2013-02-19 14:03:38

+0

将上面的脚本嵌入到我的页面后,我仍然看到流量进入,所以嵌入成功。 问题:为了看到这个额外的参数(clicknumber),我必须在GA的一侧做些什么? – Mutey 2013-02-28 11:58:31

+0

例如,您可以在仪表板上创建一个小部件。例如:我使用setCustomVar来跟踪SSL请求与非SSL请求,并且我在Analytics(分析)仪表板上创建了一个饼图。在可用参数列表中查找“自定义变量1”。 – fnkr 2013-02-28 16:16:27