Search code examples
angularjsgoogle-analyticsservicenow

How to track ServiceNow Service Portal (AngularJS) page views with Google Analytics?


I'm seeking help in getting Google Analytics working with ServiceNow's Service Portal (cloud based service management platform).. The platform uses AngularJS. I have the following code which I have in a header that is on every page:

$rootScope.$on('$locationChangeSuccess', function(event, toState, toParams){        
    ga('send', {  
        hitType: 'pageview',  
        title: $window.document.title,  
        location: $location.absUrl()  
    });
})

When I set a breakpoint in my browser's debugger, I can see the breakpoint is hit and the code is being called. enter image description here

But, I'm not seeing anything logged in my Google Analytics real-time dashboard when the above code is called: enter image description here

I have tried replacing $locationChangeSuccess, $locationChangeStart, and $stateChangeSuccess

I have tried $location.absUrl() and $location.path().

I can't seem to get the above to work. I would prefer NOT to have to use a third party script like angular-google-analytics or autotrack if possible.


Solution

  • I finally remembered to look at the browser's console logs and saw the error that was causing my issues: Error: $window is not defined

    I needed to add $window variable in the Client Script function declaration:

    function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout, $window) {