I have an website with angular 2. How can I track the time that user has spend on pages.
You can use Object Date. Date is object for work with date and time.
The momentJs is lib for work with time also. I like a momentJs becouse it work with date - simple.
In Angular2 are interfaces OnInit and OnDestroy. You can add to ngOnInit
method dateStart
and Calculate eriod to ngOnDestroy
.
import { Component, OnInit, OnDestroy} from '@angular/core';
import * as moment from "moment";
...
export class ConverterComponent implements OnInit, OnDestroy {
....
ngOnInit() {
this.dateStart = moment(); //or New Date()
}
ngOnDestroy() {
let period = moment.utc(moment(this.dateStart).diff(moment())).format("HH:mm:ss");
}
....
}