Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Analytics xml file

I am reading a new google tutorial for Android analytics and it's strange, first of all when I add this strings in my gradle:

classpath 'com.google.gms:google-services:1.3.0-beta1'
apply plugin: 'com.google.gms.google-services'

I can't sync my project (plugin not found error). I don't know is it important or not. I only can add compile 'com.google.android.gms:play-services-analytics:7.3.0'. And second, in tutorial there is a step where I should subclass Application:

package com.google.samples.quickstart.analytics;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;

/**
 * This is a subclass of {@link Application} used to provide shared objects for this app, such as
 * the {@link Tracker}.
 */
public class AnalyticsApplication extends Application {
  private Tracker mTracker;

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
      mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
  }
}

And I can't understand where to get R.xml.global_tracker file?? I thought that I will use json file, that I generated before. Have some1 faced this problem? It is funny, but for iOs tutorial was better.

Update

This approach has better documentation by the way, if some1 is interesting.

like image 244
Panich Maxim Avatar asked Aug 26 '15 17:08

Panich Maxim


People also ask

What is Analytics APK?

The official Google Analytics mobile app lets you monitor all of your Analytics properties so that you can keep track of your business while you're on the go. With this app, you can: 1) Check key metrics in built-in reports. 2) Monitor real-time data. 3) Compare date ranges and apply segments.

Can Google Analytics report on data from mobile apps?

To get the latest mobile app report features in Google Analytics, use Firebase in your Android and iOS apps. Once enabled in your app, Google Analytics will automatically collect and report on built-in events and user properties.


2 Answers

I agree with you. New documentation is not so helpful.

Here is my Application class and all you need is that, you don't need any other thing for basic integration. Even not need a xml. Use this tracker object where you want.

import android.app.Activity;
import android.content.Context;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import java.io.IOException;

public class Application extends android.app.Application {

    public static GoogleAnalytics analytics;
    public static Tracker tracker;

    @Override
    public void onCreate() {
        super.onCreate();

        analytics = GoogleAnalytics.getInstance(this);
        analytics.setLocalDispatchPeriod(1800);
        tracker = analytics.newTracker("UA-XXXXXX-X");
        tracker.enableExceptionReporting(true);
        tracker.enableAdvertisingIdCollection(true);
        tracker.enableAutoActivityTracking(true);
    }

}

Also keep 'com.google.android.gms:play-services-analytics:7.3.0' dependency in your "build.gradle".

Edit: i think my answer is not valid anymore. Don't force and just use json file :)

like image 74
asozcan Avatar answered Oct 16 '22 07:10

asozcan


With V4 of Google Analytics, Google has implemented various changes in the approach. They generate a google-services.json file which you download and add to your project; here are the definitive guides on the various steps https://developers.google.com/analytics/devguides/collection/android/v4/start and https://developers.google.com/analytics/devguides/collection/android/v4/

However, like the rest of the people here, when I used the sample code for the Application Object from Google there was a compilation error for R.xml.global_tracker. In Android Studio I decided to 'clean' the project and that did the trick. However the magic works, the xml object is now generated as part of R.java

For reference: I'm running Android Studio 2.2.3 on OSX. Presumably the fix should work on future instances and other OS's until Google change their practices again.

like image 40
JulianHarty Avatar answered Oct 16 '22 07:10

JulianHarty