Ng2GoogleAnalytics
Add Google Analytics to your Angular2+ or Ionic2+ projects
version 1.0.3 Tested on Angular 6+
For Angular 2 - 5 projects use version 0.2.0
Read my article: Enable Google Analytics in Angular2+ Projects
Features:
- Easy installation
- Angular 2+ and Ionic 2+ compatible
- Track traditional Ionic 2 Pages with no URL Change (use setPage(pageName) function )
- Track Custom Events with trackEvent function
- Add multiple tracker IDs
steps to integrate
Ng2GoogleAnalytics
in app.module.ts
Add imports: ... ... Ng2GoogleAnalyticsModule
initialize Ng2GoogleAnalytics
// add Ng2GoogleAnalyticsService in app.component.ts; // add in constructor { //initialize service with your GA Token ga; }
setPage(pageName)
function to track pages
call // add in constructor { } { thisga; }
trackEvent()
or trackEventV2()
function to track events
call thisga; // or call trackEvent2 which takes first param as object thisga; // or call like this thisga;
Additional Features
to disable GA while development
send second parameter of initialize function to false. It may be driven from environment.ts file. See https://angular.io/guide/build#configuring-application-environments
ga;
set additional tracker ID
Sometimes you need to set more than one tracking ID with your project.
This can be done with setAdditionalTraker
function.
See https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers
//call this function to set additional tracker thisga // if you want to send page to different tacking id thisga; //if you want to send event to different tacking id thisga;
set additional tracker ID
Set auto tracking of pages
This can be done with setAutoMode
function.
//call this function in app.component.ts //right after initialize function. ga;
Tested on Angular 7