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.
$ npm install td-react-native-sdk --save
If you use react-native less than 0.60.0, you will have to link the module yourself:
$ react-native link td-react-native-sdk
import TreasureData from 'td-react-native-sdk';
TreasureData.setup({
apiEndpoint: 'https://in.treasure-data.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
})
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);
});
Add and upload custom events are enabled by default. However you can disable and enable this feature at any time using:
TreasureData.enableCustomEvent();
To disable custom events
TreasureData.disableCustomEvent();
This feature is only available in Android. App lifecycle event tracking is optional and not enable by default. You can track app lifecycle events automatically using:
TreasureData.enableAppLifecycleEvent();
To disable tracking app lifecycle events:
TreasureData.disableAppLifecycleEvent();
To check if tracking app lifecycle events is enabled:
TreasureData.isAppLifecycleEventEnabled((enabled) => {
console.log('Tracking app lifecycle event is enabled?', enabled);
})
You don't need to check for platform when calling this feature's APIs, they will simply be no-op. In app purchase event tracking is optional and not enable by default. To track in app purchase events automatically, you only need to add a line of code:
TreasureData.enableInAppPurchaseEvent();
To disable tracking in app purchase events:
TreasureData.disableInAppPurchaseEvent();
To check if tracking in app purchase events is enabled:
TreasureData.isInAppPurchaseEventEnabled((enabled) => {
console.log('Tracking in app purchase event is enabled?', enabled);
})
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.
TreasureData.enableAutoAppendUniqId();
To disable adding UUID of device to each event automatically:
TreasureData.disableAutoAppendUniqId();
To reset UUID of device
TreasureData.resetUniqId();
UUID will be added to each event record automatically if you call enableAutoAppendRecordUUID
. Each event has different UUID.
TreasureData.enableAutoAppendRecordUUID();
To disable adding record UUID to each event automatically:
TreasureData.disableAutoAppendRecordUUID();
Advertising Id will be added to each event record automatically if you call enableAutoAppendAdvertisingIdentifier
.
In Android, you must install Google Play Service Ads (Gradle com.google.android.gms:play-services-ads
) as a dependency for this feature to work.
In iOS, you must link Ad Support framework in Link Binary With Libraries build phase for this feature to work.
User must also not turn on Limit Ad Tracking feature in their device, otherwise, Treasure Data will not attach Advertising Id to the record. Due to asynchronous nature of getting Advertising Id, after enableAutoAppendAdvertisingIdentifier
method called, it may take some time for Advertising Id to be available to be added to the record. However, Treasure Data does cache the Advertising Id in order to add to the next event without having to wait for the fetch Advertising Id task to complete.
TreasureData.enableAutoAppendAdvertisingIdentifier();
// Or specify custom column
TreasureData.enableAutoAppendAdvertisingIdentifier('custom_aaid_column');
To disable adding Advertising Id:
TreasureData.disableAutoAppendAdvertisingIdentifier();
To add device model information to each event automatically
TreasureData.enableAutoAppendModelInformation();
To disable:
TreasureData.disableAutoAppendModelInformation();
To add application version information to each event automatically
TreasureData.enableAutoAppendAppInformation();
To disable:
TreasureData.disableAutoAppendAppInformation();
To add locale configuration information to each event automatically
TreasureData.enableAutoAppendLocaleInformation();
To disable:
TreasureData.disableAutoAppendLocaleInformation();
Call startSession
to start tracking a session
TreasureData.startSession(sessionTable, sessionDatabase);
Call endSession
to end tracking current session
TreasureData.endSession(sessionTable, sessionDatabase);
This feature is not enabled on accounts by default, please contact support for more information. Important! You must set cdpEndpoint property of TreasureData's sharedInstance. Usage example:
TreasureData.fetchUserSegments(audienceTokens, keys, (jsonResponse) => {
console.log('Fetch User Segments', JSON.stringify(jsonResponse));
}, (errorCode, errorMessage) => {
console.log('Failed to upload events', 'Error: ' + errorCode + ' ' + errorMessage);
});
Set a default value if you want an event added to a table, a database, or any table or database to automatically set value for a key. If you have multiple default values set to the same key, newly added event will have the default value applied and override in following order:
- Default value targeting all tables and databases will be applied first.
- Default value targeting all tables in a database will then be applied.
- Default value targeting the table to which the event is added will then be applied.
- Default value targeting the table and database to which the event is added will then be applied.
- Finally, if the event has a value for the key, that value will override all default values.
To set default value:
TreasureData.setDefaultValue("value", "key"); // Targeting all databases and tables
TreasureData.setDefaultValue("value", "key", "database_name"); // Targeting all tables of database "database_name"
TreasureData.setDefaultValue("value", "key", null, "table_name"); // Targeting all tables with "table_name" of any database
TreasureData.setDefaultValue("value", "key", "database_name", "table_name"); // Targeting table "table_name" of database "database_name"
To get default value:
// Get default value for key targeting database "database_name" and table "table_name".
TreasureData.defaultValue("key", "database_name", "table_name", (defaultValue) => {
console.log('Default Value', defaultValue);
});
To remove default value:
// Only remove default values targeting database "database_name" and table "table_name".
TreasureData.removeDefaultValue("key", "database_name", "table_name");
To enable debug log
TreasureData.enableLogging();
To disable:
TreasureData.disableLogging();
To enable retry uploading
TreasureData.enableRetryUploading();
To disable:
TreasureData.disableRetryUploading();
See native SDKs repository for more information about supported devices and OS