Google Analytics is collecting the wrong version number of my Android application

I am adding Google Analytics to my application. When I go to Real Time > Overview , I see 1.0 under the App Version . My question is: where does Google Analytics get this 1.0 number from?

enter image description here

This is how I run Analytics in onCreate() 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 several gradle files. I insert them all here:

enter image description here

Here is my gradle file as well as 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: application)

 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: circleImageView (this is a library project) apply the 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' } } } 

The 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 prior to version “1.0”, I was in Eclipse, but this is the first time I'm in Android Studio, but I used the gradle Method to add Google Analytics to my account.

+7
android android-manifest build.gradle google-analytics android-version
source share
2 answers

You can use the global tracker provided by Google Analytics v4. Before you start adding a global tracker to your applications, first create this structure:

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 name --> <string name="ga_appName">My App Name</string> <!-- enter the current app 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 called App and continue 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); } } 

Change your slide manifest:

 <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, click the tracker in the 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 is activated! For more information see:

Advanced Setup - Android SDK v4

Google Analytics SDK v4 for Android - Getting Started

TAG:

  • Google Analytics Tutorial for Android
  • Google Analytics Setup
  • Google Analytics v4
  • Google Analytics Example
+2
source share

It gets the version from the context, which is passed to GoogleAnalytics.getInstance(context)

 PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); packageInfo.versionName; 

According to the documentation for PackageInfo :

The version name of this package, as indicated by the tagName attribute.

In addition, to accurately obtain your application data, you must create a Tracker from a class that extends Application

 public class MyApp extends Application { public Tracker tracker; ..... public Tracker getTracker() { if (tracker == null) { tracker = GoogleAnalytics.getInstance(this); ....... } return tracker; } } 
+4
source share

All Articles