Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.


Repository files navigation

react-native-td CircleCI npm version

react-native-td is an unofficial React Native SDK for Treasure Data.

The module is a light-weight layer sitting on-top of the TreasureData SDK for both iOS and Android.


You can install react-native-td into your project in the following way.

npm install --save react-native-td
react-native link react-native-td

iOS configuration

If you haven't introduced cocoapods, install it and run pod init under ios/and make sure lines below are in Podfile.

+ pod 'React', :path => '../node_modules/react-native', :subspecs => [
+   'Core',
+   'DevSupport', # Include this to enable In-App Devmenu if RN >= 0.43
+   'RCTText',
+   'RCTNetwork',
+   'RCTWebSocket', # needed for debugging
+ ]
+ pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
+ pod 'RNTreasureData', :path => '../node_modules/react-native-td'


Basically react-native-td imitates the interface of TreasureData iOS/Android SDK as close as possible.

Hence to know more detail check official documents below.

Instantiate with your API key

import TreasureData from "react-native-td";

TreasureData.initialize("your API key");

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

  1. Login to the Treasure Data Console at;
  2. Visit your Profile page at;
  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.

Add a event to local buffer

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

  { event: "event_name" },
  () => {
    console.log("onSuccess is called.");
  (error, message) => {
    console.log(`Error: ${error}`);
    console.log(`Message: ${message}`);

Or simply:

TreasureData.addEvent("database_name", "table_name", { event: "event_name" });

Upload buffered events to Treasure Data

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

  () => {
    console.log("onSuccess is called.");
  (error, message) => {
    console.log(`Error: ${error}`);
    console.log(`Message: ${message}`);

Or simply:


Start/End session



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

  • User opens the application and starts session tracking using startSession
  • User moves to home screen and finishes the session using endSession
  • 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.
TreasureData.setSessionTimeoutMilli(30 * 1000); // Default is 10 seconds



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

TreasureData.getSessionId().then(sessionId => {

Detect if it's the first running

You can detect if it's the first running or not easily using isFirstRun method and then clear the flag with clearFirstRun.

  .then(() => {
  .catch(error => {

About Error code

addEventWithCallback and uploadEventsWithCallback methods call back onError 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


TreasureData.initialize("your API key");

Encryption key

TreasureData.initializeEncryptionKey("hello world");

Default database


Adding UUID of the device to each event automatically


Adding an UUID to each event record automatically


Adding device model information to each event automatically


Adding application package version information to each event automatically


Adding locale configuration information to each event automatically


Use server side upload timestamp

// Add server side upload time as a customized column name

Enable/Disable debug log