I am trying to track Campaigns (emailing for example) with the Android SDK v4 but it's not working as we cannot see any data in our Google Analytics account.
We are trying to use the code below to send the data to Google Analytics:
URL we want to track:
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)
Code for tracking :
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();
We also tried this:
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());
We want to track a click on a specific link through URL SCHEME (protocol or HTTP URL).
Could you tell us if we did something wrong in my code?
SDK version : play-services:7.5.0 I am using Google Analytics v4.
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')
}
}
Project level 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()
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.