我无法理解 Google Analytics(分析)中的一件事。我需要在我的应用程序中使用某个功能,例如如果 A 用户将我的应用程序推荐给 B 用户,那么他可以获得一些奖励,但是我想跟踪 A 用户当 B 用户点击该链接时,该链接会提供ID,然后我可以在第一个活动的 B 用户应用中获得 A 用户ID。
分级文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.app"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.gms:play-services-analytics:10.2.4'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
}
分析类:
public class AnalyticsApplication extends Application {
private static GoogleAnalytics sAnalytics;
private static Tracker sTracker;
@Override
public void onCreate() {
super.onCreate();
sAnalytics = GoogleAnalytics.getInstance(this);
}
/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
if (sTracker == null) {
sTracker = sAnalytics.newTracker(R.xml.global_tracker);
}
return sTracker;
}
}
从此链接引用的所有类和代码上: https://developers.google.com/analytics/devguides/collection/android/v4/
现在我要跟踪用户ID。我该怎么做
答案 0 :(得分:1)
使用以下代码创建广播接收器:
public class CampaignBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String referrer= intent.getExtras().getString("referrer", ""); //your referrer
new com.google.android.gms.analytics.CampaignTrackingReceiver().onReceive(context, intent); //update the same to Google Analytics
}
}
在您的清单中
<service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
<receiver android:name="yourpackage.CampaignBroadCastReceiver">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
我还没有尝试过。但是根据Google Analytics(分析)广告系列跟踪中提供的文档。以下代码应在您的主要活动onCreate或onStart内起作用
// Get the intent that started this Activity.
Intent intent = this.getIntent();
Uri uri = intent.getData();
String campaign = uri.getQueryParameter("utm_source");
并将接收器添加到清单文件中,以从Play商店获取INSTALL_REFERRER广播
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
https://developers.google.com/analytics/devguides/collection/android/v3/campaigns