简体   繁体   English

使用Google Analytics(分析)跟踪外部.js文件-可以吗?

[英]Tracking an external .js file with Google Analytics - Possible?

I'm developing a web widget & it only uses an external .js file. 我正在开发一个Web小部件,它仅使用一个外部.js文件。 I need to track & get stats of my widget. 我需要跟踪并获取我的小部件的统计信息。 (eg. the number of times it is loaded, the urls of the websites where it is used, etc.) (例如,加载次数,使用该网站的网址等)

Is it possible to track & get stats of an external .js file using Google Analytics? 是否可以使用Google Analytics(分析)跟踪并获取外部.js文件的统计信息?

I wonder if I put the GA code into the external .js file weather it'll show the stats of the website where the widget is embedded instead of the stats of the external .js file. 我想知道是否将GA代码放入外部.js文件中,它将显示嵌入小部件的网站的统计信息,而不是外部.js文件的统计信息。 If Google Analytics wont help for this can anyone suggest a method to track my Javascript widget? 如果Google Analytics(分析)对此无济于事,谁能建议一种跟踪我的Javascript小部件的方法? Thanks! 谢谢!

You can use some server-side solution. 您可以使用一些服务器端解决方案。 For example, you could serve the JS file with a CGI script that records some stats. 例如,您可以使用记录某些统计信息的CGI脚本为JS文件提供服务。 Another possibility is some HTTP server plugin (eg Apache module). 另一种可能性是某些HTTP服务器插件(例如Apache模块)。

If you have siteA.com/script.js with GA code in it, and it is being included on siteB.com/somePage.html , GA will report stats on siteB.com/somePage.html IOW GA will report for the page it is put on. 如果您具有包含GA代码的siteA.com/script.js ,并且该网站已包含在siteB.com/somePage.html ,则GA会在siteB.com/somePage.html上报告统计信息siteB.com/somePage.html GA会为其报告该页面被戴上。

One thing I advise you to do is to give the GA object for your widget a non-standard namespace. 我建议您做的一件事是为窗口小部件的GA对象提供非标准名称空间。 A lot of people put GA code on their site and most people don't ever give it its own namespace, so you will likely end up clashing with someone's own instance of GA if you don't. 很多人将GA代码放在他们的网站上,大多数人从来没有给它自己的名称空间,因此如果您不这样做,您很可能最终会与某人自己的GA实例发生冲突。

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

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