Search code examples
androidgoogle-analyticsandroid-manifestbuild.gradleandroid-version

Google Analytics picking up wrong version number for my Android App


I am adding Google Analytics in my app. When I go to Real Time > Overview I see 1.0 under App Version. My question is where is Google Analytics getting this 1.0 number from?

enter image description here

This is how I am starting Analytics in the onCreate() of my Launcher Activity:

        analytics = GoogleAnalytics.getInstance(MainDrawerActivity.this);
        analytics.setLocalDispatchPeriod(1800);

        tracker = analytics.newTracker("UA-XXXXXX-X"); // Replace with actual tracker/property Id
        tracker.enableExceptionReporting(true);
        tracker.enableAdvertisingIdCollection(true);
        tracker.enableAutoActivityTracking(true);

My project has multiple gradle files. I am pasting them all here:

enter image description here

Here is my gradle file and also my Android Manifest: build.gradle: (for my Project: xxx...)

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.1.3'

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

allprojects {
repositories {
    jcenter()
}
}

build.gradle: (for my Module: app)

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "xxx.xxx.xxx"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 58
    versionName "2.0.13"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

compileOptions {
    encoding "UTF-8"
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
    abortOnError false
}
}

build.gradle for Module: circularImageView (this is a library project) apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
    minSdkVersion 11
    targetSdkVersion 19

    versionCode 60
    versionName "2.0.14"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
}

Beginning of my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxxx.xxxxxxxx"
android:installLocation="internalOnly"
android:versionCode="58"
android:versionName="2.0.13" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="21" />

Another point to note is that -- before the "1.0" version seen, I was in Eclipse but this is the first time I am on Android Studio but I used the Gradle Method to add Google Analytics to my account.


Solution

  • You can use global tracker which provided by Google Analytics v4. Before you starting to add global tracker within your apps, let's have this structure first:

    structure

    app_tracker.xml

    <resources>
            <!-- The apps Analytics Tracking Id -->
            <string name="ga_trackingId">UX-XXXXXXXXX-X</string>
            <!-- Percentage of events to include in reports -->
            <string name="ga_sampleFrequency">100.0</string>
            <!-- Enable automatic Activity measurement -->
            <bool name="ga_autoActivityTracking">true</bool>
            <!-- catch and report uncaught exceptions from the app -->
            <bool name="ga_reportUncaughtExceptions">true</bool>
            <!-- How long a session exists before giving up -->
            <integer name="ga_sessionTimeout">-1</integer>
            <!-- If ga_autoActivityTracking is enabled, an alternate screen name can be specified to
            substitute for the full length canonical Activity name in screen view hit. In order to
            specify an alternate screen name use an <screenName> element, with the name attribute
            specifying the canonical name, and the value the alias to use instead. -->
            <screenName name="com.mypackage.example.MainActivity">Home Screen</screenName>
        </resources>
    

    ecommerce_tracker.xml

        <resources>
        <integer name="ga_sessionTimeout">60</integer>
        <!--  The following value should be replaced with correct property id. -->
        <string name="ga_trackingId">UX-XXXXXXXXX-X</string>
    </resources>
    

    global_tracker.xml

    <!-- enter your app's name -->
    <string name="ga_appName">My App Name</string>
    <!-- enter the current app's versionName -->
    <string name="ga_appVersion">2.0.13</string>
    
    <!-- the Local LogLevel for Analytics -->
    <string name="ga_logLevel">verbose</string>
    <!-- how often the dispatcher should fire -->
    <integer name="ga_dispatchPeriod">30</integer>
    <!-- Treat events as test events and don't send to google -->
    <bool name="ga_dryRun">false</bool>
    <!-- The screen names that will appear in reports -->
    <screenName name="com.mypackage.example.MainActivity">Home Screen</screenName>
    

    Then, create a new Java Class named App and extend it with Application:

    import android.app.Application;
    import com.google.android.gms.analytics.GoogleAnalytics;
    import com.google.android.gms.analytics.Tracker;
    import java.util.HashMap;
    
    /**
     * Custom implementation of android.app.Application.&nbsp;The android:name attribute in the
     * AndroidManifest.xml application element should be the name of your class (".MyApp"). Android will
     * always create an instance of the application class and call onCreate before creating any other
     * Activity, Service or BroadcastReceiver.
     */
    public class App extends Application {
        // The following line should be changed to include the correct property id.
        private static final String PROPERTY_ID = "UX-XXXXXXXXX-X";
    
        //Logging TAG
        private static final String TAG = "App";
        public static int GENERAL_TRACKER = 0;
    
        public enum TrackerName {
            APP_TRACKER, // Tracker used only in this app.
            GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
            ECOMMERCE_TRACKER,
        }
    
        HashMap<TrackerName, Tracker> mTrackers = new HashMap<>();
        public App() {
            super();
        }
        public synchronized Tracker getTracker(TrackerName trackerId) {
    
            if (!mTrackers.containsKey(trackerId)) {
                GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
                Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : analytics.newTracker(R.xml.ecommerce_tracker);
                mTrackers.put(trackerId, t);
            }
            return mTrackers.get(trackerId);
        }
    }
    

    Alter your manifest slighlty:

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
    
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <!-- Google Analytics Version v4 needs this value for easy tracking -->
        <meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
            android:resource="@xml/global_tracker" />
    
        <!-- Optionally, register AnalyticsReceiver and AnalyticsService to support background
         dispatching on non-Google Play devices -->
        <receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
            </intent-filter>
        </receiver>
        <service android:name="com.google.android.gms.analytics.AnalyticsService"
            android:enabled="true"
            android:exported="false"/>
    
        <!-- Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
             installation campaign reporting -->
        <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>
        <service android:name="com.google.android.gms.analytics.CampaignTrackingService"
            android:enabled="true"/>
    
        <!-- ... -->
    </application>
    

    And finally, hit the tracker within MainActivity class:

    private Tracker tracker;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tracker = ((App) getApplication()).getTracker(App.TrackerName.APP_TRACKER);
        tracker.setScreenName("Home Screen");
        tracker.send(new HitBuilders.EventBuilder().build());
        ...
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
    }
    
    @Override
    protected void onStop() {
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
        super.onStop();
    }
    

    Voila! Google Analytics are activated! For more information, see:

    Advanced Configuration - Android SDK v4

    Google Analytics SDK v4 for Android - Getting Started

    TAG:

    • Google Analytics Tutorial for Android
    • Configuring Google Analytics
    • Google Analytics v4
    • Google Analytics example