0

I have the Google Analytics tracking code in the index.html file as well as a page view tracker in the app.component.ts file. It tookHere is the code I have that is working.

app.component.ts

export class AppComponent {

  lastRoute = location.href;

  constructor(ga:any){
    setInterval(() => {
      if (location.href != this.lastRoute) {
         this.lastRoute = location.href;
         ga('send', 'pageview', this.lastRoute);
      }
    }, 1000);
  }
}

The problem I was first having is "ga is not a function." The suggested fix was putting declare var ga:Function; right above export class in app.component.ts. That did not work, but putting go:any in the constructor's input did. However, now I want to track whenever someone opens a modal. I cannot access the Google Analytic's function from any other components. I tried using ga:any again and I am getting error's such as "cannot resolve all parameters." I just need for the component to allow me to call the ga function just like app.component.ts does.

cbrawl
  • 875
  • 1
  • 9
  • 24
  • Using the constructor is the wrong way.. please post your index.html.. – slaesh Sep 16 '16 at 05:30
  • The index file simply has the Google Analytics tracking code script. I commented out the ga() in the main script so that there aren't duplicates when I send page views from elsewhere. – cbrawl Sep 16 '16 at 13:19
  • add the ga js script to the head. maybe it's a Timing Thing.. and use the `declare` way. other way would be to use a service. – slaesh Sep 16 '16 at 18:48
  • I think that I answered a similar question, but related to Google Analytics API here: http://stackoverflow.com/a/39771047/2308745 and related to Google Sing In here: http://stackoverflow.com/a/39770500/2308745 I hope this give you some light. – Gatsbimantico Sep 29 '16 at 13:06

0 Answers0