Android SDK for Treasure Data
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example/td-android-sdk-demo
src
.gitignore
.travis.yml
CHANGELOG.md
README.md
pom.xml

README.md

Treasure Data Android SDK

Android SDK for Treasure Data. With this SDK, you can import the events on your applications into Treasure Data easily.

Installation

You can install td-android-sdk into your Android project in the following ways.

Gradle

If you use gradle, add the following dependency to dependencies directive in the build.gradle

dependencies {
    compile 'com.treasuredata:td-android-sdk:0.1.18'
}

Maven

If you use maven, add the following directives to your pom.xml

  <dependency>
    <groupId>com.treasuredata</groupId>
    <artifactId>td-android-sdk</artifactId>
    <version>0.1.18</version>
  </dependency>

This SDK has an example Android application project. The pom.xml would be a good reference.

Jar file

Or put td-android-sdk-x.x.x-shaded.jar (get the latest here) into (YOUR_ANDROID_PROJECT)/libs.

Usage

Initialize the Library at onCreate() in your Application subclass

For efficient API calls, we highly recommend initializing a TreasureData shared instance at the onCreate() method of your Application subclass.

public class ExampleApp extends Application {

  @Override
  public void onCreate() {

    // Initialize Treasure Data Android SDK
    TreasureData.initializeEncryptionKey("RANDOM_STRING_TO_ENCRYPT_DATA");
    TreasureData.disableLogging();
    TreasureData.initializeSharedInstance(this, "YOUR_WRITE_ONLY_API_KEY");
    TreasureData.sharedInstance.setDefaultDatabase("your_application_name");
    TreasureData.sharedInstance.setDefaultTable("your_event_name");
    TreasureData.sharedInstance.enableAutoAppendUniqId();
    TreasureData.sharedInstance.enableAutoAppendModelInformation();
    TreasureData.sharedInstance.enableAutoAppendAppInformation();
    TreasureData.sharedInstance.enableAutoAppendLocaleInformation();
  }
}

We recommend to use a write-only API key for the SDK. To obtain one, please:

  1. Login to the Treasure Data Console at http://console.treasuredata.com;
  2. Visit your Profile page at http://console.treasuredata.com/users/current;
  3. Insert your password under the 'API Keys' panel;
  4. In the bottom part of the panel, under 'Write-Only API keys', either copy the API key or click on 'Generate New' and copy the new API key.

Then, you can use a shared instance from anywhere with the TreasureData.sharedInstance() method.

Use the shared instance

public class ExampleActivity extends Activity {

  public void onDataLoadSomethingFinished(long elapsedTime) {
    Map<String, Object> event = new HashMap<String, Object>();
    event.put("data_type", "something");
    event.put("elapsed_time", elapsedTime);
    TreasureData.sharedInstance().addEvent("events", event);
  }
}

Add an event to local buffer

To add an event to local buffer, you can call TreasureData#addEvent or TreasureData#addEventWithCallback API.

  View v = findViewById(R.id.button);
  v.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

      final Map event = new HashMap<String, Object>();
      event.put("id", v.getId());
      event.put("left", v.getLeft());
      event.put("right", v.getRight());
      event.put("top", v.getTop());
      event.put("bottom", v.getBottom());

      td.addEventWithCallback("testdb", "demotbl", event, new TDCallback() {
        @Override
        public void onSuccess() {
          Log.i("ExampleApp", "success!");
        }

        @Override
        public void onError(String errorCode, Exception e) {
          Log.w("ExampleApp", "errorCode: " + errorCode + ", detail: " + e.toString());
        }
      });
      
      // Or, simply...
      //    td.addEvent("testdb", "demotbl", event);

    }
  });

Specify the database and table to which you want to import the events.

Upload buffered events to Treasure Data

To upload events buffered events to Treasure Data, you can call TreasureData#uploadEvents or TreasureData#uploadEventsWithCallback API.

  findViewById(R.id.upload).setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

      // You can call this API to uplaod buffered events whenever you want.
      td.uploadEventsWithCallback(new TDCallback() {
        @Override
        public void onSuccess() {
          Log.i("ExampleApp", "success!");
        }

        @Override
        public void onError(String errorCode, Exception e) {
          Log.w("ExampleApp", "errorCode: " + errorCode + ", detail: " + e.toString());
        }
      });
      
      // Or, simply...
      //   td.uploadEvents();
            
      return false;
    }
  });

It depends on the characteristic of your application when to upload and how often to upload buffered events. But we recommend the followings at least as good timings to upload.

  • When the current screen is closing or moving to background
  • When closing the application

The sent events is going to be buffered for a few minutes before they get imported into Treasure Data storage.

Retry uploading and deduplication

This SDK imports events in exactly once style with the combination of these features.

  • This SDK keeps buffered events with adding unique keys and retries to upload them until confirming the events are uploaded and stored on server side (at least once)
  • The server side remembers the unique keys of all events within the past 1 hours by default and prevents duplicated imports (at most once)

As for the deduplication window is 1 hour by default, so it's important not to keep buffered events more than 1 hour to avoid duplicated events.

Start/End session

When you call TreasureData#startSession method, the SDK generates a session ID that's kept until TreasureData#endSession is called. The session id is outputs as a column name "td_session_id". Also, TreasureData#startSession and TreasureData#endSession method add an event that includes {"td_session_event":"start" or "end"}.

	@Override
	protected void onStart(Bundle savedInstanceState) {
			:
		TreasureData.sharedInstance().startSession("demotbl");
			:
	}

	@Override
	protected void onStop() {
			:
		TreasureData.sharedInstance().endSession("demotbl");
		TreasureData.sharedInstance().uploadEvents();
		// Outputs =>>
		//   [{"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
		//		"td_session_event":"start", "time":1418880000},
		//
		//    {"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
		//		"td_session_event":"end", "time":1418880123}
		//    ]
			:
	}
	

If you want to handle the following case, use a pair of class methods TreasureData.startSession and TreasureData.endSession for global session tracking

  • User opens the application and starts session tracking. Let's call this session session#0
  • User moves to home screen and destroys the Activity
  • User reopens the application and restarts session tracking within default 10 seconds. But you want to deal with this new session as the same session as session#0
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			:
		TreasureData.setSessionTimeoutMilli(30 * 1000);  // Default is 10 seconds
	}

	@Override
	protected void onStart() {
			:
		TreasureData.startSession(this);
			:
	}

	@Override
	protected void onStop() {
			:
		TreasureData.endSession(this);
		TreasureData.sharedInstance().uploadEvents();
			:
	}

In this case, you can get the current session ID using TreasureData.getSessionId

	@Override
	protected void onStart() {
			:
		TreasureData.startSession(this);
		Log.i(TAG, "onStart(): Session ID=" + TreasureData.getSessionId(this));
			:
	}

Track app lifecycle events automatically

App lifecycle event tracking is optional and not enable by default. You can track app lifecycle events automatically using : TreasureData#enableAppLifecycleEvent()

App lifecycle events include : Application Install, Application Open, Application Update. You can disable the individual core events as the following:

  • Disable Application Install: disableAppInstalledEvent()
  • Disable Application Open: disableAppOpenEvent()
  • Disable Application Update: disableAppUpdatedEvent()

Opt out

Depending on the countries where you sell your app (e.g. the EU), you may need to offer the ability for users to opt-out of tracking data inside your app.

  • To turn off auto tracking events (when application lifecycle event tracking is enable) : TreasureData#disableAppLifecycleEvent().
  • To turn off custom events (the events you are tracking by TreasureData#addEvent, TreasureData#addEventWithCallback ) : TreasureData#disableCustomEvent. To turn on it again : TreasureData#enableCustomEvent

You can query the state of tracking events by using : TreasureData#isAppLifecycleEventEnabled() and TreasureData#isCustomEventEnabled() The states have effects across device reboots, app updates, so you can simply call this once during your application.

About error codes

TreasureData#addEventWithCallback and TreasureData#uploadEventsWithCallback call back TDCallback#onError method with errorCode argument. This argument is useful to know the cause type of the error. There are the following error codes.

  • init_error : The initialization failed.
  • invalid_param : The parameter passed to the API was invalid
  • invalid_event : The event was invalid
  • data_conversion : Failed to convert the data to/from JSON
  • storage_error : Failed to read/write data in the storage
  • network_error : Failed to communicate with the server due to network problem
  • server_response : The server returned an error response

Additional configuration

Endpoint

The API endpoint (default: https://in.treasuredata.com) can be modified using TreasureData.initializeApiEndpoint. For example:

    TreasureData.initializeApiEndpoint("https://in.treasuredata.com");
    td = new TreasureData(this, "your_api_key");

Encryption key

If you've set an encryption key via TreasureData.initializeEncryptionKey, our SDK saves the event data as encrypted when called TreasureData#addEvent or TreasureData.addEventWithCallback.

    TreasureData.initializeEncryptionKey("hello world");
        :
    td.addEventWithCallback(...)

Default database

	TreasureData.sharedInstance().setDefaultDatabase("default_db");
		:
	TreasureData.sharedInstance().addEvent("demotbl", …);

Adding UUID of the device to each event automatically

UUID of the device will be added to each event automatically if you call TreasureData#enableAutoAppendUniqId(). This value won't change until the application is uninstalled.

	td.enableAutoAppendUniqId();
		:
	td.addEvent(...);

It outputs the value as a column name td_uuid.

You can reset the UUID and send forget_device_uuid event with old UUID using TreasureData#resetUniqId().

Adding an UUID to each event record automatically

UUID will be added to each event record automatically if you call enableAutoAppendRecordUUID. Each event has different UUID.

	td.enableAutoAppendRecordUUID();
	// If you want to customize the column name, pass it to the API
	// td.enableAutoAppendRecordUUID("my_record_uuid");
		:
	td.addEvent(...);

It outputs the value as a column name record_uuid by default.

Adding device model information to each event automatically

Device model information will be added to each event automatically if you call TreasureData#enableAutoAppendModelInformation.

	td.enableAutoAppendModelInformation();
		:
	td.addEvent(...);

It outputs the following column names and values:

  • td_board : android.os.Build#BOARD
  • td_brand : android.os.Build#BRAND
  • td_device : android.os.Build#DEVICE
  • td_display : android.os.Build#DISPLAY
  • td_model : android.os.Build#MODEL
  • td_os_ver : android.os.Build.VERSION#SDK_INT
  • td_os_type : "Android"

Adding application package version information to each event automatically

Application package version information will be added to each event automatically if you call TreasureData#enableAutoAppendAppInformation.

	td.enableAutoAppendAppInformation();
		:
	td.addEvent(...);

It outputs the following column names and values:

  • td_app_ver : android.content.pm.PackageInfo.versionName (from Context.getPackageManager().getPackageInfo())
  • td_app_ver_num : android.content.pm.PackageInfo.versionCode (from Context.getPackageManager().getPackageInfo())

Adding locale configuration information to each event automatically

Locale configuration information will be added to each event automatically if you call TreasureData#enableAutoAppendLocaleInformation.

	td.enableAutoAppendLocaleInformation();
		:
	td.addEvent(...);

It outputs the following column names and values:

  • td_locale_country : java.util.Locale.getCountry() (from Context.getResources().getConfiguration().locale)
  • td_locale_lang : java.util.Locale.getLanguage() (from Context.getResources().getConfiguration().locale)

Use server side upload timestamp

If you want to use server side upload timestamp not only client device time that is recorded when your application calls addEvent, use enableServerSideUploadTimestamp.

	// Use server side upload time as `time` column
	td.enableServerSideUploadTimestamp(true);
	
	// Add server side upload time as a customized column name
	td.enableServerSideUploadTimestamp("server_upload_time");

Enable/Disable debug log

	TreasureData.enableLogging();
	TreasureData.disableLogging();