Google analytics error


#1

Hi,

I’m having trouble using the google analytics cordova plugin. I followed exactly the procedure explained here:

However, when I do so, I get a “Uncaught ReferenceError: analytics is not defined”. Should I load any js file somewhere, besides just doing “cordova plugin add https://github.com/danwilson/google-analytics-plugin.git”?

Here is my app.js:

angular.module('myapp', ['ionic','ionic.service.core', 'ngCordova',  'ionic.service.push'])
.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        if(typeof window.analytics !== undefined) {
            analytics.startTrackerWithId("UA-XXX-X");
        } else {
            console.log("Google Analytics Unavailable");
        }
    });
})

Thanks in advance for your help!


#2

Try changing the if statement to if (typeof analytics !== 'undefined') (i.e. quote underfined).


#3

Have you added google analytics plugin . run cordova plugin add https://github.com/danwilson/google-analytics-plugin.git


#4

yes, I have already done all that and then some. If I get rid of the if/ells statement completely (ie the Google Analytics initialization executes everytime), and even set a timeout in a loop (maybe the plugin takes some time to initialize), I get the same error. And of course I ran Cordova plugin add … and the plugin is listed when I do Cordova plugin list.


#5

I am running into the same problem as well, do you happen to have a workaround for this?


#6

It’s old so I don’t remember.

Make sure you are not testing this in the browser, but really on the device itself. This plugin does not work in the browser.


#7

Yeah, manage to get it work yesterday running on the device itself. Thank you :slight_smile: