用于bookmarklet的Google Analytics

时间:2013-03-15 20:16:52

标签: javascript google-analytics bookmarklet google-analytics-api

我制作了一个书签,我想使用Google Analytics跟踪它的用法。我该怎么办?我找到了雷米夏普的一篇旧帖子,它做了我想做的事,但我不确定它是否过时了。 http://remysharp.com/2009/02/27/analytics-for-bookmarklets-injected-scripts/

2 个答案:

答案 0 :(得分:1)

您可能需要更新脚本以指向GA的最新请求网址格式,但原则仍然可以。

答案 1 :(得分:0)

我最终做了以下

(function() {
 //loading ga.js - not greatest idea, but can't rely on page having ga.js already
    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);

 //made a "cool_name" to be able to sort in GA and added domain URL/URI      
    var url = "/cool_name/" + location.host + location.pathname;

 //used "anotherGA" as a 'namespace' to not screw up the real GA for that domain
    _gaq.push(['anotherGA._setAccount', 'UA-XXXXXX-XX']);
    _gaq.push(['anotherGA._setDomainName']);//not sure if i need this
    _gaq.push(['anotherGA._trackPageview', url]);
})();

请参阅:https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gaq(位于页面底部)

说实话,我不知道这是反模式还是滥用API,但它似乎有效。