angular-gtag
angular-gtag copied to clipboard
Cannot find name 'Gtag'
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?
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
I am getting the same problem
¿any solution?
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.
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
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>