3

I've implemented this solution for page tracking in Google Analytics with Angular2 https://stackoverflow.com/a/39622860/4155124 and it works well.

How could I use

            ga('send', {
                hitType: 'event',
                eventCategory: 'Some Category',
                eventAction: 'Level Completed',
                eventLabel: 'Level 3'
            });

within a component?

This code works within the component but there is a "cannot find ga" error in the console.

Community
  • 1
  • 1
Pat M
  • 5,966
  • 7
  • 24
  • 34

1 Answers1

8

You can either download the real definition file (links below) or you can make your own definition file or simply type:

declare var ga: any;

at the top of your code like the OP did in the link you provided (but this way it won't autocomplete the ga methods).

Update:

Since typescript is moving from typings to @types the new repo is: https://www.npmjs.com/package/@types/google.analytics

you can install the d.ts files via: npm install --save @types/google.analytics

If you are using angular-cli here's the tutorial for making it work with 3rd party libraries: https://github.com/angular/angular-cli/wiki/stories-third-party-lib

eko
  • 39,722
  • 10
  • 72
  • 98