I've been struggling with adding a timestamp OnInnit to track the time a user visited a page. I want to include the timestamp in an array within my services, but I keep encountering errors and can't seem to figure it out on my own. Any assistance would be greatly appreciated.
tracker-data.ts
export class TrackerData {
entry?: number;
exit?: number;
clicks?: number;
url?: string;
}
tracker.service.ts
import { Injectable } from '@angular/core';
import { TrackerData } from '../modules/tracker-data';
@Injectable({
providedIn: 'root'
})
export class TrackerService {
trackerData: TrackerData;
websiteData: TrackerData[];
public entryTime: {entry: number} [] = [];
constructor() { }
}
tracker.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { TrackerService } from '../services/tracker.service';
import { TrackerData } from '../modules/tracker-data';
@Component({
selector: 'app-tracker',
templateUrl: './tracker.component.html',
styleUrls: ['./tracker.component.scss']
})
export class TrackerComponent implements OnInit, OnDestroy {
constructor(
public trackerService: TrackerService,
private websiteData: TrackerData[]
) { }
ngOnInit(): void {
const timeOfEntry = Date.now();
this.websiteData.push(timeOfEntry); // Type 'number' has no properties in common with type 'TrackerData'.
}
Although the code above may not be perfect, I am new to this and doing my best to learn.