记录一下之前往《程序语言答人》中添加Google Anlytics的过程。
1.添加插件
cordova plugin add https://github.com/danwilson/google-analytics-plugin.git
2.添加初始代码
$ionicPlatform.ready(function() {
if (typeof analytics !== 'undefined'){
analytics.startTrackerWithId('UA-xxxxxx-x');
analytics.trackView('Screen Title')
}
});
注意
: typeof analytics !== 'undefined'是很有必要的,对于国内这种大部分的人都用不了Google的App来说。
3.对应Controller添加trackView
如在levelSelectCtrl
中添加
if (typeof analytics !== 'undefined') {
analytics.trackView("LevelSelectCtrl");
}
4.run
接着我们就可以在Google Analytics看到结果了~~
详情可见: http://ngcordova.com/docs/plugins/googleAnalytics/
module.controller('MyCtrl', function($scope, $cordovaGoogleAnalytics) {
$cordovaGoogleAnalytics.debugMode();
$cordovaGoogleAnalytics.startTrackerWithId('UA-000000-01');
$cordovaGoogleAnalytics.setUserId('USER_ID');
$cordovaGoogleAnalytics.trackView('Home Screen');
$cordovaGoogleAnalytics.addCustomDimension('dimension1', 'Level 1');
$cordovaGoogleAnalytics.trackEvent('Videos', 'Video Load Time', 'Gone With the Wind', 100);
$cordovaGoogleAnalytics.addTransaction('1234', 'Acme Clothing', '11.99', '5', '1.29', 'EUR');
$cordovaGoogleAnalytics.addTransactionItem(
'1234', 'Fluffy Pink Bunnies', 'DD23444', 'Party Toys', '11.99', '1', 'GBP'
);
});
围观我的Github Idea墙, 也许,你会遇到心仪的项目