Android SDK for TreasureData. With this SDK, you can import the events on your applications into TreasureData easily.
You can install td-android-sdk into your Android project in the following ways.
If you use gradle, add the following dependency to dependencies directive in the build.gradle
dependencies {
compile 'com.treasuredata:td-android-sdk:0.1.6'
}
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.6</version>
</dependency>
This SDK has an example Android application project. The pom.xml would be a good reference.
Or put td-android-sdk-x.x.x-shaded.jar (get the latest here) into (YOUR_ANDROID_PROJECT)/libs.
public class ExampleActivity extends Activity {
private TreasureData td;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
:
td = new TreasureData(this, "your_api_key");
or
TreasureData.initializeDefaultApiKey("your_default_api_key");
:
TreasureData td = new TreasureData(this);
We recommend to use a write-only API key for the SDK. To obtain one, please:
- Login to the Treasure Data Console at http://console.treasuredata.com;
- Visit your Profile page at http://console.treasuredata.com/users/current;
- Insert your password under the 'API Keys' panel;
- 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.
Also, you can use a shared instance from anywhere with TreasureData.sharedInstance method after calling TreasureData.initializeSharedInstance.
public class MainActivity extends Activity {
:
TreasureData.initializeDefaultApiKey("your_write_apikey");
TreasureData.initializeEncryptionKey("hello world");
:
TreasureData.initializeSharedInstance(this);
TreasureData.sharedInstance().setDefaultDatabase("testdb");
:
}
public class OtherActivity extends Activity {
:
TreasureData.sharedInstance().addEvent("demotbl", "elapsed_time", elapsed_time);
:
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 call TreasureData#addEvent method instead of TreasureData#addEventWithCallback.
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.addEvent("testdb", "demotbl", event);
Specify the database and table to which you want to import the events.
findViewById(R.id.upload).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
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());
}
});
return false;
}
});
Or, simply call TreasureData#uploadEvents method instead of TreasureData#uploadEventsWithCallback.
td.uploadEvents();
The sent events is going to be buffered for a few minutes before they get imported into TreasureData storage.
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"}.
protected void onCreate(Bundle savedInstanceState) {
:
TreasureData.sharedInstance().startSession("demotbl");
:
}
protected void onDestroy() {
:
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}
// ]
:
}
You can detect if it's the first running or not easily using TreasureData#isFirstRun method and then clear the flag with TreasureData#clearFirstRun.
if (TreasureData.sharedInstance().isFirstRun(this)) {
TreasureData.sharedInstance().addEventWithCallback("demotbl", "first_run", true, new TDCallback() {
@Override
public void onSuccess() {
TreasureData.sharedInstance().clearFirstRun(MainActivity.this);
TreasureData.sharedInstance().uploadEvents();
}
@Override
public void onError(String errorCode, Exception e) {
Log.w(TAG, "TreasureData.addEvent:onError errorCode=" + errorCode + ", ex=" + e);
}
});
}
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 invalidinvalid_event: The event was invaliddata_conversion: Failed to convert the data to/from JSONstorage_error: Failed to read/write data in the storagenetwork_error: Failed to communicate with the server due to network problemserver_response: The server returned an error response
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");
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(...)
TreasureData.sharedInstance().setDefaultDatabase("default_db");
:
TreasureData.sharedInstance().addEvent("demotbl", …);
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.
Device model infromation 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#BOARDtd_brand: android.os.Build#BRANDtd_device: android.os.Build#DEVICEtd_display: android.os.Build#DISPLAYtd_model: android.os.Build#MODELtd_os_ver: android.os.Build.VERSION#SDK_INTtd_os_type: "Android"
TreasureData.enableLogging();
TreasureData.disableLogging();