react-native-td
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.
Installation
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'
Usage
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.
- iOS: https://docs.treasuredata.com/articles/ios-sdk
- Android: https://docs.treasuredata.com/articles/android-sdk
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:
- 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.
Add a event to local buffer
To add an event to local buffer, you can call addEvent
or addEventWithCallback
API.
TreasureData.addEventWithCallback(
{ event: "event_name" },
"database_name",
"record_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.
TreasureData.uploadEventsWithCallback(
() => {
console.log("onSuccess is called.");
},
(error, message) => {
console.log(`Error: ${error}`);
console.log(`Message: ${message}`);
}
);
Or simply:
TreasureData.uploadEvents();
Start/End session
TreasureData.startSession("table_name");
TreasureData.endSession("table_name");
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
TreasureData.startSession();
TreasureData.endSession();
In this case, you can get the current session ID using getSessionId
.
TreasureData.getSessionId().then(sessionId => {
console.log(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
.
TreasureData.isFirstRun()
.then(() => {
TreasureData.clearFirstRun();
TreasureData.uploadEvents();
})
.catch(error => {
console.log(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 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
Additional Configuration
Endpoint
TreasureData.initializeApiEndpoint("https://in.treasuredata.com");
TreasureData.initialize("your API key");
Encryption key
TreasureData.initializeEncryptionKey("hello world");
Default database
TreasureData.setDefaultDatabase("default_db");
Adding UUID of the device to each event automatically
TreasureData.enableAutoAppendUniqId();
Adding an UUID to each event record automatically
TreasureData.enableAutoAppendRecordUUID();
Adding device model information to each event automatically
TreasureData.enableAutoAppendModelInformation();
Adding application package version information to each event automatically
TreasureData.enableAutoAppendAppInformation();
Adding locale configuration information to each event automatically
TreasureData.enableAutoAppendLocaleInformation();
Use server side upload timestamp
TreasureData.enableServerSideUploadTimestamp();
// Add server side upload time as a customized column name
TreasureData.enableServerSideUploadTimestamp("server_upload_time");
Enable/Disable debug log
TreasureData.enableLogging();
TreasureData.disableLogging();