How can I get UTM attributes in android? - android

I want to track UTM attributes to track the sources that are bringing users to my app and store them in database but after spending more than two days I have not found anything useful on google.

You need to register a broadcast receiver which will automatically trigger on your app first open, you can use the below example to achieve this,
public class InstallTrackersReceiver extends BroadcastReceiver {
private static final String KEY_REFERRER = "referrer";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Install Referrer", "onReceive");
if (intent != null && !intent.getStringExtra(KEY_REFERRER).equalsIgnoreCase("")) {
Log.i("Referrer", intent.getStringExtra(KEY_REFERRER));
} else {
Log.e("Install Referrer", "not found");
}
}
}
In your manifest, register your receiver like below,
<receiver
android:name="InstallTrackersReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Updated
You can use the following adb command to test install referrer,
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n <your.package>/.<path.up.until.your.InstallTrackersReceiver> --es "referrer" "utm_source=test_source\&utm_medium=test_medium\&utm_term=test_term\&utm_content=test_content\&utm_campaign=test_name"

Related

My app does not listen ACTION_MY_PACKAGE_REPLACED

I noticed when App is updated, Intents all disappear, so I need this app to listen to App update and re-send the intents, like it does with BOOT_COMPLETE
AndroidManifest.xml
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver{
private AlarmManager alarmManager;
private AlarmDB pAlarms;
#Override
public void onReceive(Context context, Intent intent) {
int reqCode = intent.getIntExtra("requestCode", 0);
System.out.println("Received code:" + reqCode);
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
pAlarms= new AlarmDB();
pLoadAlarms(context);
System.out.println("Received Signal:" + intent.getAction());
if(Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())){
System.out.println("Replaceing Packing");
for(int i=0;i<pAlarms.getSize();i++){
if(pAlarms.get(i).enabled){
setAlarm(context,pAlarms.get(i));}
}
}
}
...
}
And I tested ACTION with adb shell
C:\Users\-----\AppData\Local\Android\sdk\platform-tools>adb shell am broadcast -a ACTION_MY_PACKAGE_REPLACED
Broadcasting: Intent { act=ACTION_MY_PACKAGE_REPLACED }
Broadcast completed: result=0
But on Logcat it does not print anything in the code, so I assume no signal was even received.
Still, if I change versionCode at gradle and update app through android studio, all the Broadcast signals disappear and MY_PACKAGE_REPLACED is not called/received.

Android INSTALL_RECEIVER Broadcast Listener gets null referrer

I am attempting to get the install referrer in an INSTALL_RECEIVER broadcast listener. I receive the event without any issues however the referrer string is always null. My manifest includes:
<service android:name="com.myname.myapp.InstallReceiver"/>
<receiver android:name="com.myname.myapp.InstallReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
my BroascastReceiver is:
public class InstallReceiver extends BroadcastReceiver {
private static String TAG = "INSTALL_RECEIVER";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received install event");
String referrer = "";
Bundle extras = intent.getExtras();
if(extras != null) {
referrer = extras.getString("referrer");
}
Log.d(TAG, "Referer is: " + referrer);
}
}
and to test I am using the following commands:
adb shell
am broadcast -a com.android.vending.INSTALL_REFERRER -n com.myname.myapp/com.myname.myapp.InstallReceiver --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"
can anyone see what I am doing wrong here? The receiver is triggered because I get the following output in LogCat:
Received install event
Referer is null
Thanks for your help.

BOOT_COMPLETE and ACTION_SHUTDOWN never call the BroadcastReceiver

I want to catch ACTION_SHUTDOWN and BOOT_COMPLETE using BroadcastReceiver.
But it turns out both signals never trigger the BroadcastReceiver (I didn't see any log on logcat).
Here is my source code.
I give the permission on Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and I try to register the BroadcastReceiver in both ways
protected void onCreate(Bundle savedInstanceState)
{
registerReceiver(BootReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
registerReceiver(ShutDownReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN));
}
<receiver android:name=".BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
and the source code for BootReceiver and ShutDownReceiver are as
private BroadcastReceiver BootReceiver = new BroadcastReceiver()
{
private String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_BOOT)){
//my stuff
Log.d("Power", "Boot Complete");
}
}
};
private BroadcastReceiver ShutDownReceiver = new BroadcastReceiver()
{
private String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SHUTDOWN)) {
//my stuff
Log.d("Power", "Shutdown Complete");
}
}
};
also, I unregister both BoradcastReceiver in onDestroy
public void onDestroy()
{
unregisterReceiver(BootReceiver);
unregisterReceiver(ShutDownReceiver);
super.onDestroy();
}
Does anyone know what's wrong with my code?
Or anything I miss? Thank you.
I found out why it didn't work.
Since I use a HTC device, the broadcast messages are different from others.
Shut down event broadcasts "com.htc.intent.action.QUICKBOOT_POWEROFF"
Restart(reboot) event broadcasts "android.intent.action.ACTION_SHUTDOWN"
Power on event broadcasts "com.htc.intent.action.QUICKBOOT_POWERON"
In other device, when shutting down the device, it might broadcast "android.intent.action.QUICKBOOT_POWEROFF".
Chances are that your application is not yet added to the "BOOT_COMPLETED" possible receivers list, starting from Android 3.1, in order to get the "BOOT_COMPLETED" action, your application must have been started explicitly by the user, either showing an Activity or another Component, until then your application will not receive the broadcast you are expecting, is important to know that if you "Force Close" the application, it will be missing the broadcasts again, So, try to open an activity and then reboot your device, you will get it...
Hope this Helps!
Regards!
Try this.
<receiver
android:name="packagename.GPSReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BOOT_COMPLETED must be registered in the manifest. You cannot register for it via registerReceiver(), because by the time you call registerReceiver(), the boot will have long since occurred.
AFAIK the shutdown broadcast works with registerReceiver(), though in your case it will only be around when your process is running.

How to get referrer URL for apps downloaded from outside the market

I've found some solutions to track referrer URL from the market, but my apps aren't in the market.
Is there a way to get the referrer URL for applications downloaded from private sites?
To get referrer, you need to register your receiver for that. After installation, a broadcast is fired which you need to catch by following code.
First take a look at Android Native Application Tracking Overview
1. Create a Receiver
public class ReferrerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");
Log.i("Home", "Referrer is: " + referrerString);
}
}
2. Register in Manifest file
<receiver android:name="your.package.name.ReferrerReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

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