@nwx/gtag
A simple GTAG module for Angular applications
How to install
npm i @nwx/gtag |OR| yarn add @nwx/gtag
How to use
import { AppCfg, TargetPlatform } from '@nwx/cfg';
import { LogLevels } from '@nwx/logger';
export const environment: AppCfg = {
appName: '@nwx/gtag',
target: TargetPlatform.web,
production: true,
log: {
level: LogLevels.debug
},
gtag: {
trackingId: 'UA-XXXXXX-Y',
routeChangeTracking: true
}
};
import { CfgModule } from '@nwx/cfg';
import { LoggerModule } from '@nwx/logger';
import { GtagModule } from '@nwx/gtag';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CfgModule.forRoot(environment),
LoggerModule,
GtagModule
],
bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { CfgService, DefaultCfg } from '@nwx/cfg';
import { LogService } from '@nwx/logger';
import { GtagService } from '@nwx/gtag';
@Component({
selector: 'app-root',
template: `<h1>Welcome to {{ title }}!</h1>`
})
export class AppComponent {
title = 'Neekware';
options = {};
constructor(public cfg: CfgService, public log: LogService, public gtag: GtagService) {
this.title = this.cfg.options.appName;
this.log.info('AppComponent loaded ...');
this.trackDetailedEvent();
this.trackEvent();
}
trackDetailedEvent() {
gtag.trackEvent('home-page', {
event_category: 'SEO',
event_label: 'Page loaded, anonymous user'
});
}
trackEvent() {
gtag.trackEvent('home-page-visit');
}
}
Advanced usage
import { AppCfg, TargetPlatform } from '@nwx/cfg';
import { LogLevels } from '@nwx/logger';
export const environment: AppCfg = {
appName: '@nwx/gtag',
gtag: {
trackingId: 'UA-XXXXXX-Y',
routeChangeTracking: false
}
};
gtag.trackPageView({
page_path: '/',
page_title: 'Home Page',
page_location: 'http://neekware.com'
});
gtag.trackPageView();
const routes: Routes = [
{ path: '', component: HomeComponent, { title: 'Home page direct' }},
{ path: 'home', component: HomeComponent, data: { title: 'Home page' } }
];
Running the tests
To run the tests against the current environment:
npm run ci:all
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes