admin管理员组文章数量:1279112
I am trying, through javascript, to identify if Google Analytics or Universal Analytics is loaded.
Some clients still use the old Google Analytics and we want to roll out a javascript that collects data. So i then need to write code for both versions to make sure it gets tracked regardless if its the normal or universal version of analytics.
I am trying, through javascript, to identify if Google Analytics or Universal Analytics is loaded.
Some clients still use the old Google Analytics and we want to roll out a javascript that collects data. So i then need to write code for both versions to make sure it gets tracked regardless if its the normal or universal version of analytics.
Share Improve this question asked Aug 26, 2014 at 15:57 PatrickPatrick 5,59210 gold badges55 silver badges107 bronze badges 2- When you say old, you're talking about the version directly before the analytics.js stuff, right? How far back do you need to go? You don't need to go back to the Urchin days, do you? – Brad Commented Aug 26, 2014 at 16:02
- Nope, just Google analytics and Universal analytics. – Patrick Commented Aug 27, 2014 at 8:48
3 Answers
Reset to default 9Classic GA uses the "_gaq" object, and UA uses the "ga" object, so you could check for the existence of either of those
if (_gaq) {
// using classic GA; do whatever
}
or
if (ga) {
// using UA; do whatever
}
Hope this helps.
if (typeof window.ga === 'undefined') {
// analytics does not exist
}
From https://developer.mozilla/en-US/Firefox/Privacy/Tracking_Protection:
<a href="http://www.example." onclick="trackLink('http://www.example.', event);">Visit example.</a>
<script>
function trackLink(url,event) {
event.preventDefault();
//This is how you check that google analytics was loaded
if (window.ga && ga.loaded) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function() { document.location = url; }
});
} else {
document.location = url;
}
}
</script>
本文标签: javascriptCheck if Google Analytics or Universal Analytics is installedStack Overflow
版权声明:本文标题:javascript - Check if Google Analytics or Universal Analytics is installed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741210337a2358985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论