您的位置:首页 > Web前端 > JavaScript

Google Analytics in ADF using javascript

2013-12-06 17:46 399 查看
Avoid create the ga.js duplicate.

js file googletracker.js

function startTrace(){

(function() {

var scripts= document.getElementsByTagName('script');

var path= scripts[0].src.split("/")[3];

if(path != "ga.js"){

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);

}else{

s = document.getElementsByTagName('script')[0]; s.remove();

s = document.getElementsByTagName('script')[0];//get script[0] again

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';

s.parentNode.insertBefore(ga, s);

}

})();

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);

_gaq.push(['_trackPageview']);

alert('inside function: ' + document.title);

}

In the templat page add

<af:resource source="/googletracker.js" type="javascript"/>

<af:resource type="javascript">

startTrace();

</af:resource>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: