I'm using google analytics with my ionic framework app and followed the instruction in https://blog.nraboy.com/2014/06/using-google-analytics-ionicframework/ , I use mobile app preference in google analytics. First I tested it in my device but I got no mobile app user in the analytics overview, instead i got users using windows and mac. And in my browser appears "Uncaught ReferenceError: analytics is not defined". I don't know what I've done wrong...
Here's my code :
.controller('AnalyticsController', function($scope) {
if(typeof analytics !== undefined) { analytics.trackView("Analytics Controller"); }
$scope.initEvent = function() {
if(typeof analytics !== undefined) { analytics.trackEvent("Category", "Action", "Label", 25); }
}
})
.run(function($ionicPlatform, $ionicPopup) {
$ionicPlatform.ready(function() {
if(typeof analytics !== undefined) {
analytics.startTrackerWithId("UA-xxxx");
} else {
console.log("Google Analytics Unavailable");
}
});
});
It means just that, analytics
has not been defined. The reason that your if
statements aren't catching it is because typeof undefined
returns the string "undefined"
not the primitive undefined
, so you should do:
.controller('AnalyticsController', function($scope) {
if(typeof analytics !== 'undefined') { analytics.trackView("Analytics Controller"); }
$scope.initEvent = function() {
if(typeof analytics !== 'undefined') { analytics.trackEvent("Category", "Action", "Label", 25); }
}
})
.run(function($ionicPlatform, $ionicPopup) {
$ionicPlatform.ready(function() {
if(typeof analytics !== 'undefined') {
analytics.startTrackerWithId("UA-xxxx");
} else {
console.log("Google Analytics Unavailable");
}
});
});
Ideally, you should set up a factory to create an injectable analytics service, and not reference things on the global scope.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.