Google Analytics Tracking with Android App Installs - android

I need help with some Google Analytics tracking with Android app installs. I've tried researching this but haven't understood it all clearly.
So far, I have this in my AndroidManifest.xml:
<receiver
android:name="<IHaveMyPackageNameHere>.InstallReferrerReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Also, I have the following in my InstallReceiver class:
public class InstallReceiver extends BroadcastReceiver {
private static final String TAG = "InstallReferrerReceiver";
#Override
public void onReceive(Context context, Intent intent) {
AnalyticsTrackerFactory.setApiKey("");
HashMap<String,String> values = new HashMap<String, String>();
try {
if (intent.hasExtra("referrer")) {
String referrers[] = intent.getStringExtra("referrer").split("&");
for (String referrerValue : referrers) {
String keyValue[] = referrerValue.split("=");
values.put(URLDecoder.decode(keyValue[0]), URLDecoder.decode(keyValue[1]));
}
}
} catch (Exception e) {
}
Log.d(TAG, "referrer: " + values);
AnalyticsTrackerFactory.getTracker(context).event("Installed", values); } }
I've found some of this code online but cannot figure out how to implement it correctly. Basically, I'm stuck at setting an API key and sending the values to Google Analytics.
Can anyone guide me into the right direction? Thanks.

Google Analytics provides referrer receiver implementation. You don't need to implement it yourself. The instruction for adding campaign attribution are in this dev guide https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

Related

Referral linking in Android like mCent application

I have to implement the referral linking functionality in my application like mCent application. For that I have done the following lines of code.
My application Manifest file. In the <application >..... </application> , I have done some entries for it.
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver
android:name=".receivers.InstallReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
And My BrodcastRecevier class is as follow , please check it.
public class InstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String rawReferrer = intent.getStringExtra("referrer");
if (rawReferrer != null) {
trackReferrerAttributes(rawReferrer, context);
}
}
private void trackReferrerAttributes(String rawReferrer, Context context) {
String referrer = "";
try {
referrer = URLDecoder.decode(rawReferrer, "UTF-8");
} catch (UnsupportedEncodingException e) {
return;
}
if (Strings.isNullOrEmpty(referrer)) {
return;
}
Uri uri = Uri.parse('?' + referrer); // appends ? for Uri to pickup query string
String memberCode;
try {
referringMember = uri.getQueryParameter("mcode");
} catch (UnsupportedOperationException e) {
return;
}
SharedPreferences.Editor editor = context.getSharedPreferences(
BuildConfig.PACKAGE_NAME, Context.MODE_PRIVATE).edit();
if (!Strings.isNullOrEmpty(memberCode)) {
editor.putString(Constants.REFERRER_CODE, memberCode);
}
String referralMedium = uri.getQueryParameter("tc");
if (!Strings.isNullOrEmpty(referralMedium)) {
editor.putString("referral_medium", referralMedium);
}
editor.apply();
}
}
But i am not receiving any referral from the above code...
I have created the refferal link like this
https://play.google.com/store/apps/details?id=tv.CaseGaurd&referrer=ravindrakushwaha
Is there is any error in my referral link above OR friends , what am i doing wrong in my BroadcastRecevier class or in Manifest file
From this documentation I found that the action filter is (in manifest):
<!-- Used for install referrer tracking-->
<receiver android:name="YOUR_RECEIVER"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Also be sure that your Receiver is in that real package ".receivers.InstallReceiver", but package com.example.app.receivers;... is your package really com.example.app?
(I considered you to be using the Google Play Store app)... also, about your downvotes, this is likely to bad wording on your question, or that you are not showing effort about your question, finally, note that this is a "free to use community forum", and that people are random...
Finally, put a breakpoint in the Receiver, send a broadcast (using adb for instance), and test that you are really not getting the broadcast.

Android referral removed in analytics v3?

I've spent the last two days trying to find a workaround for this.
I need to pre-config my app depending on the referral and since google play is broadcasting an Install Referrer intent when an app is installed, I created my own receiver for this task. The code for the manifest declaration and the Receiver is:
Manifest declaration:
<receiver
android:name="my.package.CustomReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
And the simplified code of the CustomReceiver:
public class CustomReceiver extends BroadcastReceiver {
private static final String CAMPAIGN_SOURCE_PARAM = "utm_source";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("debug", "waking receiver");
Uri uri = intent.getData();
getReferrerMapFromUri(uri);
new CampaignTrackingReceiver().onReceive(context, intent);
}
void getReferrerMapFromUri(Uri uri) {
MapBuilder paramMap = new MapBuilder();
// If no URI, return an empty Map.
if (uri == null) {
Log.d("debug", "intent null");
return;
}
if (uri.getQueryParameter(CAMPAIGN_SOURCE_PARAM) != null) {
// MapBuilder.setCampaignParamsFromUrl parses Google Analytics
// campaign
// ("UTM") parameters from a string URL into a Map that can be set
// on
// the Tracker.
paramMap.setCampaignParamsFromUrl(uri.toString());
Log.d("debug", paramMap.get(Fields.CAMPAIGN_SOURCE));
// If no source parameter, set authority to source and medium to
// "referral".
} else if (uri.getAuthority() != null) {
paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral");
paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority());
}
}
}
That's all. I am sending broadcast install intent with the adb shell command but it's not getting activated at all. I am using google analytics v3.
Thank you in advance!
I've tried this way. And it does work.( I am also using Google Analytics v3)
First, in manifest :
<!-- Used for Google Play Store Campaign Measurement-->;
<service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
<receiver android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver" android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Second, add an CustomReceiver extends BroadcastReceiver
( In my case, I just copy and paste all the codes from developers )
package com.example.testanalytics;
import com.google.analytics.tracking.android.CampaignTrackingReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class CustomReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Pass the intent to other receivers.
// When you're done, pass the intent to the Google Analytics receiver.
new CampaignTrackingReceiver().onReceive(context, intent);
}
}
last step: in my activity( which I want to send messages to Google Analytics )
create an Intent and call sendBroadcast( Intent ) as follow:
Intent it = new Intent("com.android.vending.INSTALL_REFERRER");
it.setPackage("com.example.testanalytics");
it.putExtra("referrer", "utm_source%3Dyahoo%26utm_medium%3Dbanner+cpc%26utm_term%3Debook%26utm_content%3Dmy_content%26utm_campaign%3Dmy_campaign_name_2");
sendBroadcast(it);
And just get it.
I hope this may help you.

Override Scringo Signup, Login, and Logout Completed Events

I have successfully integrated Scringo into my Android app.However, I would like to do something at runtime after a successful signup, login, and logout. How do I go about this. Please help. I've searched google but to no avail. I've also contacted the Scringo team but haven't heard from them. Thanks in advance
Disclosure: I work for Scringo ;-) And didn't receive your email... next time try support#scringo.com
In Android: Create a broadcast receiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.scringo.LoginBroadcast")) {
String userId = intent.getExtras().getString("userId");
String accountId = intent.getExtras().getString("accountId");
ScringoLogger.e("Got receiver: " + accountId + ", " + userId);
}
}
}
And register it in the manifest:
<receiver android:name="com.example.MyReceiver">
<intent-filter>
<action android:name="com.scringo.LoginBroadcast" />
<category android:name="com.example" />
</intent-filter>
</receiver>
Added this: http://docs.scringo.com/android-guides/popular/handling-login-status-changes/
In iOS it's even simpler, you have the "kNotificationUserSignInChanged" defined in Scringo.h to which you should add an observer and you'll receive a notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(statusChanged) name:kNotificationUserSignInChanged object:nil];
Hope that helps

Getting Google Campaign Tracking

Google Analytics is storing the information I pass in the URL with utm_source, utm_campaign, and utm_medium. Now I am wondering how I grab that information when the user opens the application? I see this in my logs:'
03-18 20:19:48.633: I/GAV2(32317): Thread[GAThread,5,main]: Campaign found: utm_source=source value tracking tara&utm_medium=medium value tracking tara&utm_campaign=campaign value tracking tara androidlitetrackingtara
I have this in my applications manifest:
<service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
<receiver
android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
I try to grab it from the intent, but the data is not there. Any ideas?
Make sure you are using the v3 SDK. Here is the instruction page:
https://developers.google.com/analytics/devguides/collection/android/v3/campaigns
If the INTENT_REFERRER doesn't work then try the Map example given later in the page.
Also, make sure you are giving enough time between downloading the app and the data showing up in GA. It can take up to ~24 hours for GA to show data.
Create your own broadcast receiver. Store data and pass it to google analytic later
public class InstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("InstallReceiver", "onReceive");
try {
// get referrer value
Bundle extras = intent.getExtras();
if (extras != null) {
GoogleAnalytics.getInstance(context).getLogger().setLogLevel(LogLevel.VERBOSE);
String referrerValue = extras.getString("referrer");
// Handle data. Save it
Log.d("InstallReceiver", "referrerValue=" + referrerValue);
String afterDecode = URLDecoder.decode(referrerValue, "UTF-8");
String[] temp = afterDecode.split("&");
String agencyId = temp[0].replace("utm_source=", "");
Utils.saveAgencyId(context.getApplicationContext(), agencyId);
// transfer intent to google receiver.
new CampaignTrackingReceiver().onReceive(context, intent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

BroadcastReceiver not catching the INSTALL_REFERRER broadcast

i created an app that has a BroadcastReceiver that catches a INSTALL_REFERRER broadcast.
When I'm installing the app with eclipse and creating a broadcast with adb I see that all work fine, the LogCat is displaying all that it should be.
But when I'm installing the app from the play store nothing is showing on the logcat.
If I understand correctly, the play store app should create a broadcast witch the app that is being installed supposed to catch, right?
Thats basicly what im doing:
public class SDK_Referrer extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("com.android.vending.INSTALL_REFERRER"))
{
String referrer = intent.getStringExtra("referrer");
if (!(referrer == null || referrer.length() == 0))
{
// extracting the relevant data to Map
Log.d("SAMPLE", "Generating Ymid from referrel");
Map<String, String> referralmap =
createHashMapFromQueryString(referrer);
Log.d("SAMPLE", "Ymid is: " + referralmap.get("ymid"));
}
}
}
}
i only want to send someting to a server when the app is being installed.
Thanks!
You need to add the receiver to your manifest, so your app knows you have something listening for the broadcast. Something like this:
<receiver android:name="com.company.cool.SDK_Referrer" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

Categories

Resources