开发者

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

开发者 https://www.devze.com 2023-02-22 10:03 出处:网络
I\'m developing a web widget & it only uses an external .开发者_运维问答js file. I need to track & get stats of my widget. (eg. the number of times it is loaded, the urls of the websites where

I'm developing a web widget & it only uses an external .开发者_运维问答js file. 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?

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. If Google Analytics wont help for this can anyone suggest a method to track my Javascript widget? Thanks!


You can use some server-side solution. For example, you could serve the JS file with a CGI script that records some stats. Another possibility is some HTTP server plugin (e.g. Apache module).


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.

One thing I advise you to do is to give the GA object for your widget a non-standard namespace. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号