React-Native SDK Quickstart
Introduction
td-react-native-sdk is React Native module that uses native iOS and Android SDK underneath to provide Treasure Data Mobile SDK features for React Native apps. You can see more detailed documentation in repositories for Treasure Data iOS SDK and Treasure Data Android SDK.
Getting started
Install the td-react-native-sdk
module in your project.
$ npm install td-react-native-sdk --save
Note
If you use a react-native version earlier than 0.60.0, you have to link the module yourself: $ react-native link td-react-native-sdk
Initializing the SDK
First, import the module and set up the initial object with the configuration details.
import TreasureData from 'td-react-native-sdk';
TreasureData.setup({
apiEndpoint: 'https://us01.records.in.treasuredata.com', // Or other supported endpoints
encryptionKey: 'xxxxx',
apiKey: 'xxxxx', /// You should use write only api key
defaultDatabase: 'default_database',
defaultTable: 'default_table_name',
cdpEndpoint: 'https://cdp.in.treasuredata.com' // Or other cdp endpoints
})
Warning
Treasure Data strongly recommends that you use a write-only API key for ingest and import operations and whenever using Treasure Data SDKs
Click here to see how to get a write-only API Key.
- Login to the Treasure Data Console and go to the API Key page .
- If you dont already have a write-only API key, then create one. From the top right, select Actions > Create API Key .
- Name the new API Key.
- From the Type drop-down menu, select Write-only .
- Select Save .
- Copy your new Write-Only API Key and use it authenticate the APIs in your project.
For a full list of baseURLs that can be used for apiEndpoint
see the Treasure Data API baseURLs.
Adding an Event
You can add custom events to a specific database and table. If database param is not specified, defaultDatabase
configuration in TreasureData.setup({...})
will be used instead.
Specify the database and table to which you want to import the events. The total length of database and table must be shorter than 129 characters.
const customEvent = {event: 'Custom event', data: new Date().getSeconds()};
TreasureData.addEvent(customEvent, 'table', 'database');
// or
TreasureData.addEvent(customEvent, 'table');
Or if you need to know when addEvent
is successful or has failed, use addEventWithCallback
instead. You can pass null
or undefined
as database param and defaultDatabase
configuration in TreasureData.setup({...})
will be used instead.
const customEvent = {event: 'Custom event', data: new Date().getSeconds()};
TreasureData.addEventWithCallback(customEvent, 'table', 'database', () => {
console.log('Add Event Successfully');
}, (errorCode, errorMessage) => {
console.log('Add Event Failed', errorCode, errorMessage);
});
Uploading Events to Treasure Data
You can upload all buffered events to Treasure Data at anytime with uploadEvent
function
TreasureData.uploadEvents();
Or if you need to know when uploadEvents
is successful or has failed, use uploadEventsWithCallback
instead.
TreasureData.uploadEventsWithCallback(() => {
console.log('Upload events successfully')
}, (errorCode, errorMessage) => {
console.log('Failed to upload events', errorCode, errorMessage);
});
Further Reading
You now have all the basics covered. Here are some extra resources that we think you may need. Check out the API Reference for more details on adding events and uploading events with callbacks. If you prefer the source code, check out the github link below.
- React Native API Reference - Deep dive into all available functionality.
- SDK Source Code - Source code for the React Native SDK.
- Android SDK - Underlying module with more functionality than this module.
- iOS SDK - Underlying module with more functionality than this module.