admin管理员组

文章数量:1394086

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/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="" 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?

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./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." 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?

Share Improve this question edited Nov 2, 2019 at 9:36 Lg102 asked Apr 18, 2012 at 6:15 Lg102Lg102 4,9183 gold badges40 silver badges63 bronze badges 1
  • could you give us a link to your site - this problem is really hard to debug only looking at your code. implementation seems correct. – Łukasz Rysiak Commented Apr 18, 2012 at 6:34
Add a ment  | 

3 Answers 3

Reset to default 4

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

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

Longer explanation: Async tracking allows pushing mands 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.

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

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

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 pleted.

To hook up:

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

本文标签: javascriptWhy is Google Analytics not tracking any eventsStack Overflow