angular-gtag icon indicating copy to clipboard operation
angular-gtag copied to clipboard

Cannot find name 'Gtag'

Open DonaH opened this issue 7 years ago • 6 comments

Encounter this >> ERROR in src/app/app.component.ts(11,29): error TS2304: Cannot find name 'Gtag'.

Here's my code in app.component.ts

import { Component } from '@angular/core';
import { GtagModule } from 'angular-gtag';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  constructor(gtag: Gtag) {}
}

mmm... what's missing?

DonaH avatar Sep 20 '18 15:09 DonaH

I imported import { Gtag } from 'angular-gtag';

import { Component } from '@angular/core';

import { Gtag } from 'angular-gtag';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  gtag: any;

  constructor(gtag: Gtag) {}
}

But still getting

Google Analytics pageview error ReferenceError: gtag is not defined

tanalam2411 avatar Sep 24 '18 13:09 tanalam2411

I am getting the same problem

deepak41 avatar Nov 12 '18 13:11 deepak41

¿any solution?

eflorespalma avatar Nov 27 '18 22:11 eflorespalma

Same error on build on cloud ng build --prod: Could not resolve angular-gtag relative to [object Object]. error TS2307: Cannot find module 'angular-gtag'.

if I build on localhost I don't get this error.

lapongua avatar Dec 04 '18 10:12 lapongua

Same error on build on cloud ng build --prod: Could not resolve angular-gtag relative to [object Object]. error TS2307: Cannot find module 'angular-gtag'.

if I build on localhost I don't get this error.

Solution: npm install on cloud

lapongua avatar Dec 04 '18 11:12 lapongua

I was getting this same error because the gtag() function doesn't get defined when you're running locally. Assuming you don't want to use google analytics locally, you can add the below 'else' block to your script tag:

  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
  <script>
    var host = window.location.hostname;
    if(host != "localhost") {
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXXX-X', { 'send_page_view': false });
    } else {
      // don't log google analytics for localhost
      function gtag() {}
    }
  </script>

tltjr avatar Sep 09 '19 22:09 tltjr