GoogleAnalytics:无法读取属性“ trackView”或未定义

时间:2018-10-24 20:44:04

标签: ionic3

开发一个Ionic 3.19 Web应用程序并尝试将GA包含到我的项目中,我在运行serve命令时遇到了Promise中未发现的错误。像这样在app.module.ts中添加了import语句:

import { GoogleAnalytics } from ‘@ionic-native/google-analytics’;

@NgModule({
declarations: [
MyApp,
QuotesListPage,
QuotesDetailPage

],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
QuotesListPage,
QuotesDetailPage
],
providers: [
GoogleAnalytics,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})

要跟踪的页面是引号列表:

export class QuotesListPage {

quotesList = [];
filteredQuotes = [];
isfiltered: boolean ;
googleanalytics: GoogleAnalytics;

constructor(private http:Http, public navCtrl: NavController, public navParams: NavParams, platform: Platform) {
this.isfiltered = false;
this.http.get(‘quotes.json’)
.map(res => res.json())
.subscribe(
data => {
this.quotesList = data.quotes;
},
err => console.log("error is "+err), // error
() => console.log('read quotes Complete '+ this.quotesList) // complete
);
platform.ready().then(() => {
this.googleanalytics.trackView(“Quotes List”);
});
}

引号详细信息:

import { GoogleAnalytics } from ‘@ionic-native/google-analytics’;

@IonicPage()
@Component({
selector: ‘page-quotes-detail’,
templateUrl: ‘quotes-detail.html’,
})
export class QuotesDetailPage {

quoteDetail: {quote:’’, author:’’};
googleanalytics: GoogleAnalytics;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.quoteDetail = navParams.get(‘quote’);
this.googleanalytics.trackView(“Quotes Detail”);
}

ionViewDidLoad() {
console.log(‘ionViewDidLoad QuotesDetailPage’);
}

最后一个app.component:

export class MyApp {
rootPage:any = QuotesListPage;
public googleanalytics: GoogleAnalytics;

constructor(platform: Platform) {
platform.ready().then(() => {
this.googleanalytics.debugMode();
this.googleanalytics.startTrackerWithId(“XX-XXXXXXXXX-X”);
this.googleanalytics.enableUncaughtExceptionReporting(true).then((_success) => {
console.log("Successful enabling of uncaught exception reporting "+_success)}).catch((_error) => {
console.log("error occured "+_error)
});
});

请提供一些帮助!非常感谢你!

1 个答案:

答案 0 :(得分:1)

您需要进行依赖注入,而不是在每个打字稿文件中直接实例化

constructor(
   googleanalytics: GoogleAnalytics
   ...
)