Google Analytics not tracking views in Android Ionic 3 Application with gtag.js

荒凉一梦 提交于 2021-02-11 14:45:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!