Reputation: 43
Developing a ionic 3.19 web app and trying to include GA into my project, I am getting the error uncaught in promise when running serve command. Added the import statement in app.module.ts like this:
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}
]
})
The pages wanted to track are quotes-list:
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”);
});
}
And quotes-detail:
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’);
}
Finally 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)
});
});
Some help, please! Thank you very much!
Upvotes: 0
Views: 40
Reputation: 385
You need to make dependency injection instead of directly instancing it in every typescript file
constructor(
googleanalytics: GoogleAnalytics
...
)
Upvotes: 1