问题
I'm using gtag.js to track analytics on my Ionic application.
This works with iOS, but not with Android.
In index.html, I've add the script :
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-XX', {
'send_page_view': false,
'app_name': 'MyApp'
});
</script>
I've made a provider for analytics :
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { AppVersion } from "@ionic-native/app-version";
import { Platform } from "ionic-angular";
declare let gtag: Function;
/*
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 platform: Platform,
public http: HttpClient,
private appVersion: AppVersion
) {
console.log("Hello AnalyticsProvider Provider");
}
send(screen_name) {
if (this.platform.is("cordova")) {
console.log("Send Analytics");
this.appVersion.getPackageName().then((app_id) => {
this.appVersion.getVersionNumber().then((app_version) => {
gtag("event", "screen_view", {
app_id: app_id,
app_version: app_version,
screen_name: screen_name,
});
});
});
}
}
event(action, category, value) {
if (this.platform.is("cordova")) {
gtag("event", action, {
event_category: category,
event_label: value,
});
}
}
}
And in my Home screen, I call it like this :
ionViewDidEnter() {
this.analytics.send("Home");
}
This is really working good with iOS, but not on Android. I'v found this post which mention the opposite : Google Analytics not tracking views in IOS Ionic 3 Application with GTAG.js
来源:https://stackoverflow.com/questions/62306415/google-analytics-not-tracking-views-in-android-ionic-3-application-with-gtag-js