简体   繁体   English

Google Analytics:外部.js文件

[英]Google Analytics: External .js file

Just to confirm... 只是为了确认...

file: google.js file:google.js

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try {
var pageTracker = _gat._getTracker("UA-11510668-1");
pageTracker._trackPageview();
} catch(err) {}

And then linking the file like: 然后链接文件,如:

<script src="google.js" language="javascript" type="text/javascript"></script>

That works, right? 那很有效,对吗?

Add that script to your page, then if you're using FireFox w/ FireBug (or other debugger) or Chrome, you can Inspect Element to see what's been written to the page. 将该脚本添加到您的页面,然后如果您使用带FireBox(或其他调试器)或Chrome的FireFox,您可以检查元素以查看已写入页面的内容。 If you see... 如果你看到...

<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>

...then you should be good to go! ...那你应该好好去!

My testing shows that this works. 我的测试显示这是有效的。

The sample page provided runs a document.write() in an external script, and then also runs document.write() to print out yet another script tag, to make sure that that functionality works, also. 提供的示例页面运行document.write()一个外部脚本,然后也跑document.write()打印出另一个脚本标签,以确保该功能的工作原理,也。 I got expected output (two lines of text) on all browsers I tested, including Internet Explorer (even 6), Firefox, Safari, and Chrome. 我在所有测试的浏览器上得到了预期的输出(两行文本),包括Internet Explorer(甚至6),Firefox,Safari和Chrome。

I think they just say to do it inline because they think it's easier for newbies. 我认为他们只是说要内联,因为他们认为新手更容易。

You should be fine. 你应该没事。

I can't think of a reason it wouldn't. 我想不出它不会的原因。

Have you tried it and encountered any issues? 你试过它并遇到任何问题吗?

Edit: I just tried this and inspected it in firebug, as far as I can see, the second script tag is written correctly. 编辑:我刚试过这个并在firebug中检查过,据我所见,第二个脚本标记写得正确。

This should work. 这应该工作。

I think you still need to execute that script somehow. 我认为你仍然需要以某种方式执行该脚本。 If you simply include it then it won't be executed. 如果您只是包含它,那么它将不会被执行。 If you put it inside of a wrapper function: 如果你把它放在一个包装函数中:

function googleCode(){
// google code goes here
}

then you can use jQuery and execute it when a page is fully loaded 那么你可以使用jQuery并在页面完全加载时执行它

 $(document).ready(function() {
           googleCode();
        });

Just an idea. 只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM