9

I've added the folowing code into my JS to track a button click:

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

I've hit a breakpoint on it using teh Chrome dev tools and _gaq definitely resolves to the GA object and I can even step into the (minified) push event in the GA.js code. However, even though this fires with no errors, I dont see any GET or POST logged in Fiddler/firebug/Chrome, nor is anything logged on my analytics. Normal page analytics are working fine for me, with the followin running at the foot of the page:

<script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'XXXXXXXXX']);
        _gaq.push(['_setDomainName', '.Domain.com']);
        _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>

Anyone have any ideas why the above code isn't working?

LDJ
  • 6,896
  • 9
  • 52
  • 87
  • 2
    Hey, whatever happened with this? Did Rimbaud's answer really solve this problem or was it something else? If the former, can you accept Rimbaud's answer with the green checkbox to the left of the answer? If the latter, can you document the solution as an answer? I'm having this same exact problem. Thanks! – jamesmortensen May 04 '12 at 23:00

3 Answers3

2

A common cause are wrong parameter types (GA fails silently in this case).

For _trackEvent, parameters have to be:

  • Category = string
  • Action = string
  • Label (optional) = string
  • Value (optional) = integer

Don't use integers when a string is expected or vice versa.

smhg
  • 2,159
  • 19
  • 26
0

For me, it was a pretty silly mistake. I had my own IP filtered out in GA.

Figured that might help somebody!

Phil LaNasa
  • 2,907
  • 1
  • 25
  • 16
0

As I understand it, you have the trackevent in an external .js-file, and the call to the standard script at the bottom of the <body>-tag?

The apparent solution, is to move the script:

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'XXXXXXXXX']);
    _gaq.push(['_setDomainName', '.Domain.com']);
    _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>

Up in the <head>-tag, and the call to the external js-file below this snippet.

Like:

<html>
<head>
    <script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'XXXXXXXXX']);
            _gaq.push(['_setDomainName', '.Domain.com']);
            _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 type="text/javascript" src="ext.js"></script>
</head>
<body>

</body>

bskard
  • 1,040
  • 1
  • 9
  • 13
  • 1
    Moved the code into the header and the external JS file after it (in the header). Even though _gaq.push(['_trackEvent', 'category', 'action', 'label']); fires when clicking the link, I'm still not seeing any GET in fiddler and nothing in the GA – LDJ Jun 22 '11 at 07:20
  • Look for images tab in NET. GA track events are fired as images. – Moe Sweet Apr 03 '12 at 13:09