I’m adding Google Analytics page timing and I want to log timing for initial construction of a page and subsequent entries into that page. So I have a mixin Component that my other Components extend:
import { Component } from '@angular/core';
const CATEGORY = 'Page Timing';
const LOAD = 'Load'; // When page is constructed only called once
const ENTER = 'Enter'; // When page is about to enter
@Component({
selector: 'page-google-analytics',
templateUrl: 'google-analytics.html'
})
export class GoogleAnalyticsPage {
protected name: string = '';
protected willEnterStart: number = 0;
protected didLoadStart: number = 0;
ionViewDidLoad() {
this.didLoadStart = new Date().getTime();
}
ionViewWillEnter() {
this.willEnterStart = new Date().getTime();
// avoiding DI of GoogleAnalytics in all derived classes...
if (window['ga']) {
window['ga'].trackView(this.name);
}
}
ionViewDidEnter() {
if (window['ga']) {
let now = new Date();
if (this.didLoadStart) {
console.log(this.name, LOAD, now.getTime() - this.didLoadStart);
window['ga'].trackTiming(CATEGORY, now.getTime() - this.didLoadStart, this.name, LOAD);
}
console.log(this.name, ENTER, now.getTime() - this.willEnterStart);
window['ga'].trackTiming(CATEGORY, now.getTime() - this.willEnterStart, this.name, ENTER);
this.didLoadStart = 0;
this.willEnterStart = 0;
}
}
On Tabs the first time I visit each Tab I see a ‘Load’ followed by an ‘Enter’ logged and subsequent visits to that page I only see ‘Enter’ logged. Which is what I expect.
But all other pages I visit I always see both ‘Load’ and ‘Enter’ logged every time I visit the same page (even with the same NavParams). I expected all pages to be cached once they are Entered(?)
Am I misunderstanding the lifecycle events?