Code has stopped running from inside .run, or at least some of it has

Here is the .run

angular.module('checklistApp', ['ionic','ionic.utils', 'checklist.controllers','ngCordova'])

.run(function($ionicPlatform, $cordovaAdMob, $window) {
  $ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
  StatusBar.styleDefault();
}
if (typeof analytics !== 'undefined' && $scope.analyticOnOff == true){
  alert("analytics function initiated");
  analytics.startTrackerWithId('mycode');
  analytics.trackView('app started');
}
 alert("in ionic run");

})
  }); 

This works fine in the browser but the alert does not run on my android phone using ionic run android.

The app seems fine otherwise. It was running the alerts yesterday and I was using them to try and find out how to get AdMob working.

But since yesterday I’ve had the following issues:

  • AdMob not working despite much effort and absolute sticking to instructions afaik
  • The CLI “ionic resources” failing yesterday and continuing to do so.
  • ionic serve this morning not working at all. I needed to reinstall ionic.

Is there something wrong with ionic on the whole at the moment? It seems like everything I try is collapsing and it’s very odd. I feel like I can’t make any more efforts or do any coding today as I can’t test anything when every other thing doesn’t work - ie I don’t know where the problem is, at all.

I’ve taken it apart bit by bit.

Removing the $scope.analyticOnOff bit seems to have fixed things. Most odd. Was working fine yesterday.

Ce la vie.

I’m sure you’ve moved on by this point, but if not…
You are getting an error because $scope is not defined.

Luckily, in the browser, neither is analytics, so the test for $scope.analyticOnOff is never executed.
In-app, however, analytics is defined, so $scope.analyticOnOff yields the ReferenceError: $scope is not defined error.

I think I’ve found this issue. It’s not related to Ionic or $scope actually (although you are right that I didn’t define it in that bit of code).

It’s Google Analytics - when you set it up you need to leave it 24hrs before it works fully. I’ve noticed this with other Google services too.