danwilson / google-analytics-plugin

Cordova (PhoneGap) Plugin to connect to the native Google's Universal Analytics SDK 3.0
MIT License
694 stars 500 forks source link

Ionic implementation not firing #551

Open adigourdi opened 5 years ago

adigourdi commented 5 years ago

I'm trying to track app start (and other views) but nothing shows up on Google Analytics, and no errors are reported on the console

This is the code I'm using on app.component.ts

...
import { GoogleAnalytics } from '@ionic-native/google-analytics';
...
const GA_TRACKING_ID = 'UA-XXXXXXXXX-X';
const GA_TRACKING_DISPATCH = 3; // default 30, doesn't work either
...
export class MyApp implements OnInit {
  ...
  constructor(
    ...
    private ga: GoogleAnalytics,
  ) {}

  ngOnInit() {
    this.platform.ready().then(() => {
      console.log('platform ready');
      ...
      if (this.platform.is('cordova')) {
        console.log('platform is cordova');
        ...
        this.initGoogleAnalytics();
      }
    });
  }

  private initGoogleAnalytics() {
    console.log('registering tracker');

    this.ga
      .startTrackerWithId(GA_TRACKING_ID, GA_TRACKING_DISPATCH)
      .then(() => {
        console.log('Google analytics is ready now');

        // the component is ready and you can call any method here

        this.ga.debugMode(); // no debugging info shows up on console
        this.ga.setAllowIDFACollection(false); // either true or false doesn't work

        this.ga.trackView('start')
          .then(a => console.log('tracked start', a))
          .catch(e => console.log('tracking start failed', e))
        ;
      })
      .catch(e => console.log('Error starting GoogleAnalytics', e));
  }
}

The track start command should fire normally, with a dispatch of 3s, it should show up on analytics right away

Log result on console, no errors occured

platform ready
platform is cordova
registering tracker
Google analytics is ready now
tracked start Track Screen: start

For the record, Google Analytics Admin no longer allows registering mobile app tracking without firebase, so I created it as a website, and created a mobile view

I set the domain as https://mobile.website.com and tried setting the compaign url in trackView as https://mobile.website.com/start, and still nothing

I checked the tracking ID, and it's correct

Info

ionic cli             : 4.5.0
ionic                 : 3.9.2
@ionic/app-scripts    : 3.2.1

cordova (Cordova CLI) : 8.0.0
Cordova Platforms     : android 7.1.3, browser 5.0.4

Android SDK Tools     : 26.1.1
selected-pixel-jameson commented 5 years ago

Are you using version 1.8.6? I'm also having this issue and have found that if I revert back to version 1.8.3 it works fine. Someone posted this issue https://github.com/danwilson/google-analytics-plugin/issues/536, but when I follow the steps in there my Android app just crashes when I launch it.

I'd be perfectly fine just sitting at version 1.8.3, however if I ever remove the android platform and re-add it then it automatically updates the plugin version and I'm going to run into this issue again. At this point I've been fighting with this for days. I'm probably just going to document this issue in my App's readme file and try to maintain version 1.8.3.

selected-pixel-jameson commented 5 years ago

Seems like this 1.8.6 issue has been going on for awhile. No response. No updates so it looks like our best bet is probably to just downgrade and make sure Ionic doesn't automatically update it to 1.8.6. Rack another up one for Ionic Native. 👎

adigourdi commented 5 years ago

I believe fixing the version with "1.8.3" (in package.json and config.xml) instead of "^1.8.3" or "~1.8.3" should do it

I will downgrade soon and get back to you with any updates

selected-pixel-jameson commented 5 years ago

Ya that fixes it. But if you ever remove a platform using ionic cordova platform rm android`` and then re-add it usingionic cordova platform add androidit updates thepackage.jsonandconfig.xml files so that they are again using^1.8.6```. I'm not sure how to prevent that from happening.

ryanhalley commented 5 years ago

Providing your config.xml, package.json and package-lock.json are all using 1.8.3, don't ever use 'ionic cordova platform add android', as like you say, it ignores the lock file and reapplies the ^ to package.json - use 'cordova platform add android' instead, and it will work correctly. Unless I'm mistaken, this has been the case for many moons, but no-one has commented on the ionic prefixed command being bugged.

selected-pixel-jameson commented 5 years ago

@ryanhalley Thank you! I will try this.

sajanmonga1771 commented 5 years ago

thanks @selected-pixel-jameson. it working fine now.

selected-pixel-jameson commented 5 years ago

I did something that adjusted my package file and boom. Issue is happening again. Would be great if this got fixed. Glad I test my builds before every release to make sure the analytics are working.