0

Followed the Google Analytics tutorial link for Android App, Everything works well with no error in App but Nothing is displayed on the google analytics webpage.

If I make a new project,the code works well and shows correct data on google analytics webpage. But doesn't when i add the code to a existing project.

Used Multilevel Inheritance for the application android:name tag. As told here

Existing Project has Google play services : maps and plus . So I have made all google play services to use the same version 8.4.0 .

logcat correctly displays the I/MainActivity: Tag name:MainActivity screen name: Main Activity page.

Project Gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:2.0.0-alpha6'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

App Gradle File

buildscript {
    repositories {

        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {
    useLibrary 'org.apache.http.legacy'
    compileSdkVersion 23
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "com.miamia.miamia"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.1"

    }
    repositories {
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.facebook.android:facebook-android-sdk:4.5.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.google.android.gms:play-services-plus:8.4.0'
    compile  'com.google.android.gms:play-services-maps:8.4.0'
    compile files('libs/butterknife-7.0.1.jar')
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile project(':library')
    compile 'com.android.support:design:23.0.1'
    compile 'com.soundcloud.android:android-crop:1.0.1@aar'
    compile project(':WheelViewLib')
    compile 'com.ms-square:expandableTextView:0.1.4'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
    compile 'com.github.amlcurran.showcaseview:library:5.0.0'
    compile 'com.google.android.gms:play-services-analytics:8.4.0'


}
apply plugin: 'com.google.gms.google-services'

Manifest File

.
.
.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:name=".analytics.AnalyticsApplication"
.
.
.    

AnalyticsApplication File

package com.miamia.miamia.analytics;

import com.miamia.miamia.R;
import com.miamia.miamia.app.Appcontroller;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
/**
 * Created by shailesh on 2/24/2016.
 */
public class AnalyticsApplication extends Appcontroller{
    private Tracker mTracker;


    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;
    }
}

Main Activity File

....
protected void onCreate(Bundle savedInstanceState) {
.
.
.
 sendDataToAnalytics();
        //
    }

    public void sendDataToAnalytics(){
        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        Tracker mTracker = application.getDefaultTracker();
        String name="Main Activity page.";
        Log.i(TAG, "Tag name:"+TAG+" screen name: " + name);
        mTracker.setScreenName("Image~" + name);
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
...

Appcontroller File

public class Appcontroller extends Application {
   .
   .
   .
}
Community
  • 1
  • 1
Shailesh Lobo
  • 43
  • 3
  • 10

2 Answers2

0

You need to extend the Application class to instantiate Google Analytics

Make the following change:

public class AnalyticsApplication extends Application{
private Tracker mTracker;


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;
}

}

Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
  • I have used Multi-level Inheritance. i.e AnalyticsApplication extends Appcontroller and Appcontroller extends Application. – Shailesh Lobo Feb 24 '16 at 11:29
  • as told [here](http://stackoverflow.com/questions/32395482/how-to-add-two-androidname-attributes-to-application-tag-in-manifest-file) – Shailesh Lobo Feb 24 '16 at 12:10
0

Got it. Made a new Property. Thanks all.

Shailesh Lobo
  • 43
  • 3
  • 10