我正在尝试使用Android SDK v4跟踪广告系列(例如通过电子邮件发送),但它无法正常工作,因为我们无法在Google Analytics帐户中看到任何数据。
我们正在尝试使用以下代码将数据发送到Google Analytics:
我们要跟踪的网址:
URL SCHEME : scheme://www.example.com/commandes?utm_source=Mail_Invitation_Vente_ET&utm_medium=email&utm_term=ALL&utm_content=ALL&utm_campaign=TEST_CAMPAIGN
OR
URLs WEB:
http://www.example.com/commandes?utm_source=Mail_Invitation_Vente_ET&utm_medium=email&utm_term=ALL&utm_content=ALL&utm_campaign=TEST_CAMPAIGN
http://examplepetstore.com/index.html?utm_source=email&utm_medium=email_marketing&utm_campaign=summer&utm_content=email_variation_1
(the documentation : https://developers.google.com/analytics/devguides/collection/android/v4/campaigns)
跟踪代码:
Tracker tracker = GoogleAnalytics.getInstance(this).newTracker("UA-XXXXX");
tracker.enableExceptionReporting(true);
tracker.enableAdvertisingIdCollection(true);
tracker.enableAutoActivityTracking(true);
tracker.setScreenName("SCREEN/ android");
tracker.send(new HitBuilders.ScreenViewBuilder().setCampaignParamsFromUrl(URL_TO_TRACK).build());
GoogleAnalytics.getInstance(context).dispatchLocalHits();
我们也试过这个:
HashMap<String, String> campaignMap = new HashMap<>(3);
campaignMap.put("utm_source", SOURCE_TO_TRACK);
campaignMap.put("utm_medium", MEDIUM_TO_TRACK);
campaignMap.put("utm_campaign", CAMPAIGN_TO_TRACK);
tracker.send(new HitBuilders.ScreenViewBuilder().setAll(campaignMap).build());
我们希望通过URL SCHEME(协议或HTTP URL)跟踪特定链接的点击。
你能告诉我们我的代码是否出错了吗?
SDK版本:play-services:7.5.0 我使用的是Google Analytics v4。
答案 0 :(得分:0)
public static GoogleAnalytics analytics;
public static Tracker tracker;
protected void onCreate(Bundle savedInstanceState) {
analytics = GoogleAnalytics.getInstance(this);
analytics.setLocalDispatchPeriod(1800);
tracker = analytics.newTracker("UA-XXXXX-0");
tracker.enableExceptionReporting(true);
tracker.enableExceptionReporting(true);
tracker.enableAutoActivityTracking(true);
String campaignData = "http://examplepetstore.com/index.html?utm_source=email&utm_medium=email_marketing&utm_campaign=summer&utm_content=email_variation_1 ";
tracker.setReferrer(campaignData);
tracker.send(new HitBuilders.ScreenViewBuilder()
.setCampaignParamsFromUrl(campaignData)
.build());
}
<强> AndroidMainfest.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxxxx"
android:versionCode="11"
android:versionName="1.0" >
<uses-sdk
android:maxSdkVersion="23"
android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
<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
android:permission="android.permission.INSTALL_PACKAGES"
-->
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
App level build.gradel
apply plugin: 'android'
apply plugin: 'com.google.gms.google-services'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':Volley')
compile project(':Volley')
compile 'com.google.android.gms:play-services-analytics:8.3.0'
//apply plugin: 'com.google.gms.google-services'
compile 'com.android.support:multidex:1.0.0'
}
android {
//compileSdkVersion 20
//buildToolsVersion "20.0.0"
compileSdkVersion 23
buildToolsVersion "23.0.0"
useLibrary 'org.apache.http.legacy'
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
defaultConfig {
applicationId "com.xxxxxxx"
multiDexEnabled true
minSdkVersion 14
targetSdkVersion 23
versionCode 11
versionName "1.0"
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
项目级别build.gradel
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
//mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.google.gms:google-services:1.5.0-beta2'
}
allprojects {
repositories {
jcenter()
}
}
}