PageRenderTime 24ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://gitlab.com/vectorci/td-android-sdk
Markdown | 381 lines | 285 code | 96 blank | 0 comment | 0 complexity | 5cc43e09df0f1d46b9bed9c34a3c7605 MD5 | raw file
  1. TreasureData Android SDK
  2. ===============
  3. Android SDK for [TreasureData](http://www.treasuredata.com/). With this SDK, you can import the events on your applications into TreasureData easily.
  4. ## Installation
  5. You can install td-android-sdk into your Android project in the following ways.
  6. ### Gradle
  7. If you use gradle, add the following dependency to `dependencies` directive in the build.gradle
  8. ```
  9. dependencies {
  10. compile 'com.treasuredata:td-android-sdk:0.1.13'
  11. }
  12. ```
  13. ### Maven
  14. If you use maven, add the following directives to your pom.xml
  15. ```
  16. <dependency>
  17. <groupId>com.treasuredata</groupId>
  18. <artifactId>td-android-sdk</artifactId>
  19. <version>0.1.13</version>
  20. </dependency>
  21. ```
  22. This SDK has [an example Android application project](https://github.com/treasure-data/td-android-sdk/tree/master/example/td-android-sdk-demo). The pom.xml would be a good reference.
  23. ### Jar file
  24. Or put td-android-sdk-x.x.x-shaded.jar (get the latest [here](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.treasuredata%22%20AND%20a%3A%22td-android-sdk%22)) into (YOUR_ANDROID_PROJECT)/libs.
  25. ## Usage
  26. ### Instantiate TreasureData object with your API key
  27. ```
  28. public class ExampleActivity extends Activity {
  29. private TreasureData td;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. :
  35. td = new TreasureData(this, "your_api_key");
  36. ```
  37. or
  38. ```
  39. TreasureData.initializeDefaultApiKey("your_default_api_key");
  40. :
  41. TreasureData td = new TreasureData(this);
  42. ```
  43. We recommend to use a write-only API key for the SDK. To obtain one, please:
  44. 1. Login to the Treasure Data Console at http://console.treasuredata.com;
  45. 2. Visit your Profile page at http://console.treasuredata.com/users/current;
  46. 3. Insert your password under the 'API Keys' panel;
  47. 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.
  48. ### Use a shared instance
  49. Also, you can use a shared instance from anywhere with `TreasureData.sharedInstance` method after calling `TreasureData.initializeSharedInstance`.
  50. ```
  51. public class MainActivity extends Activity {
  52. :
  53. TreasureData.initializeDefaultApiKey("your_write_apikey");
  54. TreasureData.initializeEncryptionKey("hello world");
  55. :
  56. TreasureData.initializeSharedInstance(this);
  57. TreasureData.sharedInstance().setDefaultDatabase("testdb");
  58. :
  59. }
  60. public class OtherActivity extends Activity {
  61. :
  62. Map<String, Object> event = new HashMap<String, Object>();
  63. event.put("event_name", "data_load");
  64. event.put("elapsed_time", elapsed_time);
  65. TreasureData.sharedInstance().addEvent("demotbl", event);
  66. :
  67. ```
  68. ### Add events to local buffer
  69. ```
  70. View v = findViewById(R.id.button);
  71. v.setOnClickListener(new OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. final Map event = new HashMap<String, Object>();
  75. event.put("id", v.getId());
  76. event.put("left", v.getLeft());
  77. event.put("right", v.getRight());
  78. event.put("top", v.getTop());
  79. event.put("bottom", v.getBottom());
  80. td.addEventWithCallback("testdb", "demotbl", event, new TDCallback() {
  81. @Override
  82. public void onSuccess() {
  83. Log.i("ExampleApp", "success!");
  84. }
  85. @Override
  86. public void onError(String errorCode, Exception e) {
  87. Log.w("ExampleApp", "errorCode: " + errorCode + ", detail: " + e.toString());
  88. }
  89. });
  90. }
  91. });
  92. ```
  93. Or, simply call `TreasureData#addEvent` method instead of `TreasureData#addEventWithCallback`.
  94. ```
  95. final Map event = new HashMap<String, Object>();
  96. event.put("id", v.getId());
  97. event.put("left", v.getLeft());
  98. event.put("right", v.getRight());
  99. event.put("top", v.getTop());
  100. event.put("bottom", v.getBottom());
  101. td.addEvent("testdb", "demotbl", event);
  102. ```
  103. Specify the database and table to which you want to import the events.
  104. ### Upload buffered events to TreasureData
  105. ```
  106. findViewById(R.id.upload).setOnTouchListener(new OnTouchListener() {
  107. @Override
  108. public boolean onTouch(View view, MotionEvent motionEvent) {
  109. td.uploadEventsWithCallback(new TDCallback() {
  110. @Override
  111. public void onSuccess() {
  112. Log.i("ExampleApp", "success!");
  113. }
  114. @Override
  115. public void onError(String errorCode, Exception e) {
  116. Log.w("ExampleApp", "errorCode: " + errorCode + ", detail: " + e.toString());
  117. }
  118. });
  119. return false;
  120. }
  121. });
  122. ```
  123. Or, simply call `TreasureData#uploadEvents` method instead of `TreasureData#uploadEventsWithCallback`.
  124. ```
  125. td.uploadEvents();
  126. ```
  127. The sent events is going to be buffered for a few minutes before they get imported into TreasureData storage.
  128. ### Start/End session
  129. 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"}`.
  130. ```
  131. @Override
  132. protected void onStart(Bundle savedInstanceState) {
  133. :
  134. TreasureData.sharedInstance().startSession("demotbl");
  135. :
  136. }
  137. @Override
  138. protected void onStop() {
  139. :
  140. TreasureData.sharedInstance().endSession("demotbl");
  141. TreasureData.sharedInstance().uploadEvents();
  142. // Outputs =>>
  143. // [{"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
  144. // "td_session_event":"start", "time":1418880000},
  145. //
  146. // {"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
  147. // "td_session_event":"end", "time":1418880123}
  148. // ]
  149. :
  150. }
  151. ```
  152. If you want to handle the following case, use a pair of class methods `TreasureData.startSession` and `TreasureData.endSession` for global session tracking
  153. * User opens the application and starts session tracking. Let's call this session `session#0`
  154. * User moves to home screen and destroys the Activity
  155. * 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`
  156. ```
  157. @Override
  158. protected void onCreate(Bundle savedInstanceState) {
  159. super.onCreate(savedInstanceState);
  160. :
  161. TreasureData.setSessionTimeoutMilli(30 * 1000); // Default is 10 seconds
  162. }
  163. @Override
  164. protected void onStart() {
  165. :
  166. TreasureData.startSession(this);
  167. :
  168. }
  169. @Override
  170. protected void onStop() {
  171. :
  172. TreasureData.endSession(this);
  173. TreasureData.sharedInstance().uploadEvents();
  174. :
  175. }
  176. ```
  177. In this case, you can get the current session ID using `TreasureData.getSessionId`
  178. ```
  179. @Override
  180. protected void onStart() {
  181. :
  182. TreasureData.startSession(this);
  183. Log.i(TAG, "onStart(): Session ID=" + TreasureData.getSessionId(this));
  184. :
  185. }
  186. ```
  187. ### Detect if it's the first running
  188. You can detect if it's the first running or not easily using `TreasureData#isFirstRun` method and then clear the flag with `TreasureData#clearFirstRun`.
  189. ```
  190. if (TreasureData.sharedInstance().isFirstRun(this)) {
  191. Map<String, Object> event = new HashMap<String, Object>();
  192. event.put("first_run", true);
  193. event.put("app_name", "td-android-sdk-demo");
  194. TreasureData.sharedInstance().addEventWithCallback("demotbl", event, new TDCallback() {
  195. @Override
  196. public void onSuccess() {
  197. TreasureData.sharedInstance().clearFirstRun(MainActivity.this);
  198. TreasureData.sharedInstance().uploadEvents();
  199. }
  200. @Override
  201. public void onError(String errorCode, Exception e) {
  202. Log.w(TAG, "TreasureData.addEvent:onError errorCode=" + errorCode + ", ex=" + e);
  203. }
  204. });
  205. }
  206. ```
  207. ## About error codes
  208. `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.
  209. - `init_error` : The initialization failed.
  210. - `invalid_param` : The parameter passed to the API was invalid
  211. - `invalid_event` : The event was invalid
  212. - `data_conversion` : Failed to convert the data to/from JSON
  213. - `storage_error` : Failed to read/write data in the storage
  214. - `network_error` : Failed to communicate with the server due to network problem
  215. - `server_response` : The server returned an error response
  216. ## Additional configuration
  217. ### Endpoint
  218. The API endpoint (default: https://in.treasuredata.com) can be modified using `TreasureData.initializeApiEndpoint`. For example:
  219. ```
  220. TreasureData.initializeApiEndpoint("https://in.treasuredata.com");
  221. td = new TreasureData(this, "your_api_key");
  222. ```
  223. ### Encryption key
  224. 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`.
  225. ```
  226. TreasureData.initializeEncryptionKey("hello world");
  227. :
  228. td.addEventWithCallback(...)
  229. ```
  230. ### Default database
  231. ```
  232. TreasureData.sharedInstance().setDefaultDatabase("default_db");
  233. :
  234. TreasureData.sharedInstance().addEvent("demotbl", …);
  235. ```
  236. ### Adding UUID of the device to each event automatically
  237. 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.
  238. ```
  239. td.enableAutoAppendUniqId();
  240. :
  241. td.addEvent(...);
  242. ```
  243. It outputs the value as a column name `td_uuid`.
  244. ### Adding device model information to each event automatically
  245. Device model infromation will be added to each event automatically if you call `TreasureData#enableAutoAppendModelInformation`.
  246. ```
  247. td.enableAutoAppendModelInformation();
  248. :
  249. td.addEvent(...);
  250. ```
  251. It outputs the following column names and values:
  252. - `td_board` : android.os.Build#BOARD
  253. - `td_brand` : android.os.Build#BRAND
  254. - `td_device` : android.os.Build#DEVICE
  255. - `td_display` : android.os.Build#DISPLAY
  256. - `td_model` : android.os.Build#MODEL
  257. - `td_os_ver` : android.os.Build.VERSION#SDK_INT
  258. - `td_os_type` : "Android"
  259. ### Adding application package version information to each event automatically
  260. Application package version infromation will be added to each event automatically if you call `TreasureData#enableAutoAppendAppInformation`.
  261. ```
  262. td.enableAutoAppendAppInformation();
  263. :
  264. td.addEvent(...);
  265. ```
  266. It outputs the following column names and values:
  267. - `td_app_ver` : android.content.pm.PackageInfo.versionName (from Context.getPackageManager().getPackageInfo())
  268. - `td_app_ver_num` : android.content.pm.PackageInfo.versionCode (from Context.getPackageManager().getPackageInfo())
  269. ### Adding locale configuration information to each event automatically
  270. Locale configuration infromation will be added to each event automatically if you call `TreasureData#enableAutoAppendLocaleInformation`.
  271. ```
  272. td.enableAutoAppendLocaleInformation();
  273. :
  274. td.addEvent(...);
  275. ```
  276. It outputs the following column names and values:
  277. - `td_locale_country` : java.util.Locale.getCountry() (from Context.getResources().getConfiguration().locale)
  278. - `td_locale_lang` : java.util.Locale.getLanguage() (from Context.getResources().getConfiguration().locale)
  279. ### Enable/Disable debug log
  280. ```
  281. TreasureData.enableLogging();
  282. ```
  283. ```
  284. TreasureData.disableLogging();
  285. ```