新的Google Analytics代码如下所示:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_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>
Run Code Online (Sandbox Code Playgroud)
如何将全新的Google Analytics异步跟踪代码移动到外部JavaScript文件中?
我特别要问"var _gaq = _gaq || []; [...]"因为我知道可以移动其余部分,例如
的index.html
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_gaq.push(['_trackPageview']);
</script>
<script src="include.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
include.js
function includeGA()
{
var ga = document.createElement('script'); ga.type = 'text/javascript'; …Run Code Online (Sandbox Code Playgroud)