Why is Google Analytics not tracking any events?

谁说我不能喝 提交于 2020-01-04 01:58:09

问题


I've implemented pretty much the standard examples:

        <script>

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-mycode']);
            _gaq.push(['_trackPageview']);

            (function() {
            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);
            })();

        </script>

        <script>

            function recordOutboundLink(link, category, action) {
                try {
                    var myTracker=_gat._getTrackerByName();
                    _gaq.push(['myTracker._trackEvent', category ,  action ]);
                    setTimeout('document.location = "' + link.href + '"', 100)
                }catch(err){}
            }

        </script>

and the links have this onclick event:

<a id="latestDownload" href="https://example.com" onClick="recordOutboundLink(this, 'newDownloads', 'latest');return false;">Download latest version</a>

No events have been tracked for the past 3 days, which just sound wrong to me. I've tested the page with the GA debug plugin for chrome, which shows events are send.

Have I made some mistake here?

The Google GA debug addon shows (literally, not obfuscated):

Account ID : UA-XXXXX-X

&utmac=UA-XXXXX-X

Do I need to push the '_setAccount' again?


回答1:


tl;dr... leave out the _getTrackerByName() call, just use

_gaq.push(['myTracker._trackEvent', category ,  action ]);

Longer explanation: Async tracking allows pushing commands to multiple trackers (see Tracking Basics) using a syntax like

_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['b._setAccount', 'UA-XXXXX-2']);
_gaq.push(['b._trackPageview']);

The _gaq.push(['myTracker._trackEvent', category , action ]); code assumes you've already initialized myTracker like the b tracker above.

Since myTracker has never had an accountId set, it shows the UA-XXXXX-X accountId while debugging.

The analytics code on Specialized Tracking/Outbound Links is wrong, or would only work if the setup code named myTracker.




回答2:


myTracker is a variable, so you cannot really refer to it inside a string. Following should work:

_gaq.push(['_trackEvent', category ,  action ]);



回答3:


The setTimeout thing seems a bit risky to me - it assumes that the Google Analytics call has been made within 100 ms.

I prefer this:

function trackOutboundLink(url) {
    _gaq.push(['_trackEvent', 'outbound', 'click']);

    _gaq.push(function() {
        window.location = url;
    });
}

This queues up the redirect until after the Google Analytics async call has completed.

To hook up:

<a href="#" onclick="trackOutboundLink('your-url');return false;">Link</a>


来源:https://stackoverflow.com/questions/10203819/why-is-google-analytics-not-tracking-any-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!