Google Analytics do Ionic projektu
Jak přidat Google Analytics do Ionic projektu
npm packages:
+ "@ionic-native/google-analytics": "^4.20.0",
src/app/app.module.ts:
export const analyticsKey: string = "UA-xxxxx-1";
app/app.components.ts:
this.analytics.init();
src/providers/analytics/analytics.ts:
import { GoogleAnalytics } from "@ionic-native/google-analytics";
import { Injectable } from "@angular/core";
import { Platform } from "ionic-angular";
import { App, ViewController } from "ionic-angular";
import { analyticsKey } from "../../app/app.module";
/*
Generated class for the AnalyticsProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class AnalyticsProvider {
constructor(private ga: GoogleAnalytics, private platform: Platform, private app: App) {
}
public init() {
console.log("Hello AnalyticsProvider Provider");
if (!this.platform.is("cordova")) {
console.log("Neni cordova");
this.app.viewDidEnter.subscribe(
(view: ViewController) => {
console.log("New view:" + view.id);
}
);
return;
}
this.ga.startTrackerWithId(analyticsKey)
.then(() => {
console.log("Google analytics is ready now");
// the component is ready and you can call any method here
// this.ga.debugMode();
this.ga.setAllowIDFACollection(false);
this.ga.trackView("IntroPage"); // Informace o prvni strance se ztrati, tak ji doplnim
this.app.viewDidEnter.subscribe(
(view: ViewController) => {
console.log("New view:" + view.id);
this.ga.trackView(view.id);
}
);
})
.catch(e => console.log("Error starting GoogleAnalytics", e));
}
}
Jak debugovat Google Analytics:
adb shell setprop log.tag.GAv4 DEBUG
adb logcat -s GAv4