Google Analytics(分析)为我的Android应用程序提取了错误的版本号

我在我的应用程序中添加了Google Analytics。 当我去Real Time > Overview我看到1.0 App Version 。 我的问题是Google Analytics从哪里获取这个1.0号码?

在这里输入图像说明

这就是我在启动器ActivityonCreate()中启动Google Analytics的方法:

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

我的项目有多个gradle文件。 我在这里粘贴他们:

在这里输入图像说明

这是我的gradle文件,也是我的Android Manifest: build.gradle :(对于我的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 :(对于我的模块:应用程序)

 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模块:circularImageView (这是一个库项目)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' } } } 

我的舱单开始

 <?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" /> 

还有一点需要注意的是 – 在“1.0”版之前,我在Eclipse中,但是这是我第一次在Android Studio上,但是我使用Gradle方法将Google Analytics添加到我的帐户。

Solutions Collecting From Web of "Google Analytics(分析)为我的Android应用程序提取了错误的版本号"

您可以使用Google Analytics(分析)v4提供的全球跟踪器。 在开始在您的应用程序中添加全局跟踪器之前,先让我们来看看这个结构:

结构体

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> 

然后,创build一个名为App的新Java类,并使用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); } } 

改变你的清单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> 

最后,打开MainActivity类中的跟踪器:

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

瞧! Google Analytics已激活! 有关更多信息,请参阅:

高级configuration – Android SDK v4

适用于Android的Google AnalyticsSDK v4 – 入门

标签:

  • 适用于Android的Google Analytics教程
  • configurationGoogle Analytics
  • Google Analytics(分析)v4
  • Google Analytics示例

它从传递给GoogleAnalytics.getInstance(context)的上下文获取版本,

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

根据PackageInfo的文档:

此包的版本名称,由标记的versionName属性指定。

此外,为了准确获取您的应用程序数据,您应该从扩展Application的类创buildTracker

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