【问题标题】:GoogleAnalytics: Cannot read property 'trackView' or undefindedGoogle Analytics:无法读取属性“trackView”或未定义
【发布时间】:2018-10-26 21:56:05
【问题描述】:

开发 ionic 3.19 Web 应用程序并尝试将 GA 包含到我的项目中,但在运行 serve 命令时,我收到了未在承诺中发现的错误。在 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)
});
});

请帮忙!非常感谢!

【问题讨论】:

    标签: ionic3


    【解决方案1】:

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

    constructor(
       googleanalytics: GoogleAnalytics
       ...
    )
    

    【讨论】:

    • 您能否更具体地了解一下依赖注入?我对离子很陌生。你的意思是什么构造函数?在 app.component.ts?我需要创建新服务吗?谢谢!
    • 你可以在上面给出的构造函数中初始化它,而不是直接在课后初始化谷歌分析变量
    猜你喜欢
    • 2022-12-05
    • 2016-12-21
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多