Tracking Install Referrer or Campaign Referrer for Android App in Titanium

Posted By : Arpit Sharma | 19-Dec-2012

Tracking install referrer

In this blog you will find how to create a Titanium Android Module for tracking Install Referral.

Install Referral is use to track the installation source of android application. First of all we have to create a android module. Install ndk from here.

After creating the android module, you have to follow the following steps :

  • Import android module into Eclipse( File > Import > Existing Projects into Workspace).
  • Open build.properties file and add android ndk path into it, example : android.ndk=/Users/user_name/android-ndk-r8b
  • Create a new class (ReferrerCatcher.java) in src > com.yourpackage folder.
  • Now, extends BroadcastReceiver in ReferrerCatcher class and implement onReceive function into it.

 

package com.oodles.referral;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ReferrerCatcher extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		
	}

}

  • You will get the install url through Intent object and store the require parameter using SharedPreferences.

 

package com.oodles.referral;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class ReferrerCatcher extends BroadcastReceiver {

	final String INSTALL_PREFERENCE = "installPrefrences";
	final String REFERRAL_URL = "InstallReferral";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.i("installReferralTracker", "installReferralTracker Broadcast Receiver info");
		
		//Getting install url from intent object 
		String uri = intent.toURI();
		
		if (uri != null && uri.length() > 0) {
			int index = uri.indexOf("utm_source=");
			if (index > -1) {
				
				//Getting require parameters from url
				uri = uri.substring(index, uri.length() - 4);
				Log.i("installReferralTracker", "Referral URI: " + uri);
				
				//Store paramaters using SharedPreferences
				SharedPreferences settings = context.getSharedPreferences(INSTALL_PREFERENCE, 0);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(REFERRAL_URL, uri);
				editor.commit();

				Log.i("installReferralTracker", "Cached Referral URI: " + uri);
			} else
				Log.i("installReferralTracker", "No Referral URL.");
		}
		Log.i("installReferralTracker", "End");
	}
}
  • Now open Your_Module_NameModule.java and declare SharedPreferences object and other variables in class, example

 

public class ReferralModule extends KrollModule {
	
final String REFERRAL_URL = "InstallReferral";
	
static SharedPreferences settings;
	
// Standard Debugging variables
private static final String LCAT = "ReferralModule";

  • Next step is to defined the SharedPreferences object in onAppCreate method.

 

public static void onAppCreate(TiApplication app) {
		Log.d(LCAT, "inside onAppCreate");
		// put module init code that needs to run when the application is created
		settings = app.getSharedPreferences("installPrefrences", 0);
	}
  • Create a method get_utm_source() which returns the store parameter string.

 

public String get_utm_source() {
		Log.d(LCAT, "get_utm_source called");
		
		String referralURL = settings.getString(REFERRAL_URL, null);
		
		if (referralURL != null && !referralURL.equals("")) {
			return referralURL;
		} else {
			return "referralURL is null";
		}
	}
  • Right click on build.xml and Run As "Ant Build", after build sucessfully go to the module directory then "dist" foldar now extart the zip file and place the module into titanium module foldar and add the module in project.
  • In Titanium, you can use the module by calling get_utm_source() function but before this you have to add couple of lines in tiapp.xml

 

<receiver android:exported="true" android:name="your.package.ReferrerCatcher">         
      <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
      </intent-filter>
</receiver>

 

var win = Ti.UI.createWindow({
	backgroundColor : "#fff"
});
//Getting the module object
var referral = require('com.oodles.referral');

//Call the function get_utm_source()
var utm_source = referral.get_utm_source();

alert(utm_source);

win.open();

This is how you can track the  Install Referrer or Campaign Referrer for Android App in Titanium, i hope this will help you.

 

Arpit Sharma
[email protected]

http://oodlestechnologies.com/

About Author

Author Image
Arpit Sharma

Arpit has been developing Android and iPhone applications for a while now and is expert in mobile application development . He excels in developing mobile applications with location based intelligence. Arpit loves Modelling as a hobby.

Request for Proposal

Name is required

Comment is required

Sending message..