Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find name 'Gtag' #5

Open
DonaH opened this issue Sep 20, 2018 · 6 comments
Open

Cannot find name 'Gtag' #5

DonaH opened this issue Sep 20, 2018 · 6 comments

Comments

@DonaH
Copy link

DonaH commented Sep 20, 2018

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?

@tanalam2411
Copy link

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

@deepak41
Copy link

I am getting the same problem

@eflorespalma
Copy link

¿any solution?

@lapongua
Copy link

lapongua commented Dec 4, 2018

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
Copy link

lapongua commented Dec 4, 2018

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

@tltjr
Copy link

tltjr commented Sep 9, 2019

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants