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.
Install the td-react-native-sdk module in your project.
npm install td-react-native-sdk --saveIf you use a react-native version earlier than 0.60.0, you have to link the module yourself:
react-native link td-react-native-sdkFirst, 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', // change here
apiKey: 'YOUR_API_KEY', /// You should use write only api key
defaultDatabase: 'default_database',
defaultTable: 'default_table_name',
cdpEndpoint: 'https://cdp.in.treasuredata.com' // Or other cdp endpoints
})YOUR_API_KEY should be your actual apikey string. You can retrieve your API key from your profiles in TD Console. Using a write-only API key is recommended.
For a full list of baseURLs that can be used for apiEndpoint see the Treasure Data API baseURLs.
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);
});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);
});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.