-1

I need the following function to fire every time a user lands on my site (regardless of the URL) in order to track traffic. The site is in Angular2 (v7). I placed the following function early on in the angular execution stack (in app.component.ts's constructor), however it only executes about once every few minutes at maximum, regardless of page reloads and navigation from new tabs. All other functions in the constructor work perfectly fine every time. Please help me fire this function every time:

constructor(){

this.currentDateTest = Date.now().toString();
this.afs.collection(`Traffic`).doc(this.currentDateTest).ref.set({
    date: this.currentDateTest,
    landingAddress: this.landingAddress,
    referrer: document.referrer,
    usersMeta: 'test'
  });

  otherFunctions()

}
FiringBlanks
  • 1,998
  • 4
  • 32
  • 48
  • Please check the document https://firebase.google.com/docs/functions/schedule-functions and example below: [1]:https://stackoverflow.com/questions/41684517/call-a-function-every-time-a-page-is-visited-ionic-2 [2]:https://stackoverflow.com/questions/55723837/how-to-call-an-angular-7-function-after-every-on-load-page – Vaidehi Jamankar Sep 27 '22 at 12:46
  • This is not scheduling a server-side cloud function. I need this function to run client-side. – FiringBlanks Sep 29 '22 at 06:33

1 Answers1

1

Try it inside ngOnInit

import { Component, OnInit } from '@angular/core';
 export class AppComponent implements OnInit {

    constructor() {}

    ngOnInit(): void {
      this.currentDateTest = Date.now().toString();
      this.afs.collection(`Traffic`).doc(this.currentDateTest).ref.set({
        date: this.currentDateTest,
        landingAddress: this.landingAddress,
        referrer: document.referrer,
        usersMeta: 'test'
      });

     otherFunctions();
   }

}
Lionel Lakson
  • 19
  • 1
  • 3