0
votes

I've tried to look around many threads here around GA, I feel like I've done what's required. My site consist of HTML5 and angular.js, the page is structured roughly as follow (through ng-include)

index.html
|-header
--|-navigation
|-content
|-footer

when a link from navigation bar is clicked, the content will change (partials page in angular), header, footer stays the same.

The last script tag in my < head > is as follow (inside index.html)

<script type="text/javascript">
        var globalLanguage = 'en';

        // GA tracking variable
        var _gaq = [['_setAccount', 'UA-XXXXXXXX-X'], ['_trackPageview']];
</script>

and at the bottom part of index.html, before the closing < / body > tag:

<script type="text/javascript">
    (function(d, t) {
        var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
        g.async = 1;
        g.src = '//www.google-analytics.com/ga.js';
        g.type = 'text/javascript';
        s.parentNode.insertBefore(g, s);
    }(document, 'script'));
</script>

All my angular controller function, call a common function as described in Tracking Google Analytics Page Views with Angular.js

function gaqPageView($scope, $location, $window) {
    console.log('triggering google analytics');
    $scope.$on('$viewContentLoaded', function(event) {
        console.log('event triggered, tracking: ' + $location.path());
        $window._gaq.push([ '_trackPageview', $location.path() ]);
    });
}

I do see the console log statements, there's no error in the console either. When I print out the content of _gaq, I do get an array that grows as I navigate around the page (which mean my _gaq.push call is working just fine).

However, in my the network call (in chrome dev tool), I don't see any _utm.gif call to Google Analytics. (Basics of Debugging Google Analytics Code: GA Chrome Debugger and other tools).

What am I missing here? seems like the google analytics is not firing off the event and reporting it?

Edit: I am pretty sure I am silly here, the _gaq variable itself is just a normal javascript array, so of course _gaq.push work just fine. But what am I missing to get Google Analytics to kick in and start sending the content of that _gaq?

1
_gaq should be replaced by an object when ga.js get's loaded -- if not, that implies a problem with the function at the end of the page that's supposed to insert it. Maybe add a console.log tag inside the function to verify it's running. You should also be able to see that the ga.js script has been inserted by looking at the page elements inside Chrome Dev tools.mike
I see the <script> to ga.js is added and the request to get ga.js from google retrieve the javascript. the _gaq object itself is just a plain array though.TS-

1 Answers

0
votes

Are you currently running your server on localhost, or an intranet name without a "." -- the tracking GIF request doesn't get made for localhost servers by default.

See Google Analytics GIF request not sent.


Another idea: Usually _gaq is defined as an array only if it's not already defined. If ga.js has already executed, you might be overwriting the _gaq object. It doesn't seem likely from your code organization, but...

Try replacing

var _gaq = [['_setAccount', 'UA-XXXXXXXX-X'], ['_trackPageview']];

with

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