我在我的应用中添加了Google Analytics。当我转到Real Time > Overview
时,我会在1.0
下看到App Version
。我的问题是Google Analytics从哪里获得 1.0
号码?
这就是我在启动器onCreate()
Activity
中启动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 :(对于我的项目: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 for Module:circularImageView (这是一个库项目) 申请插件:' 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" />
另一点值得注意的是 - 在&#34; 1.0&#34;之前看到版本,我在Eclipse中,但这是我第一次使用Android Studio,但我使用the Gradle Method将Google Analytics添加到我的帐户。
答案 0 :(得分:4)
它从传递给GoogleAnalytics.getInstance(context)
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
packageInfo.versionName;
根据PackageInfo
的文档:
此程序包的版本名称,由标记的versionName属性指定。
另外,为了准确获取您的应用数据,您应该从扩展Tracker
Application
public class MyApp extends Application {
public Tracker tracker;
.....
public Tracker getTracker() {
if (tracker == null) {
tracker = GoogleAnalytics.getInstance(this);
.......
}
return tracker;
}
}
答案 1 :(得分:2)
您可以使用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>
然后,创建一个名为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. 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已激活!有关更多信息,请参阅:
Advanced Configuration - Android SDK v4
Google Analytics SDK v4 for Android - Getting Started
标签: