# Treasure Data Android and iOS SDKs Cordova Plugin

The `td-cordova-sdk` module uses the native iOS and Android SDKs to provide Treasure Data Mobile SDK features to Cordova apps.

## Getting Started

Install the TD Cordova SDK by running the following command:


```shell
cordova plugin add td-cordova-sdk
```

## Usage

After installing the plugin, you can access the methods through `cordova.plugins.TreasureDataPlugin` namespace.

## Configuration


```javascript
TreasureDataPlugin.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
})
```

## Add an Event to Local Buffer

You can add custom events to a specific database and table. If a database parameter is not specified, the `defaultDatabase` configuration in `TreasureDataPlugin.setup({...})` will be used instead.

Specify the database and table where you want to import events. The total length of the database and table names must be shorter than 129 characters.


```javascript
const customEvent = {event: 'Custom event', data: new Date().getSeconds()};
TreasureDataPlugin.addEvent(customEvent, 'table', 'database');
// or
TreasureDataPlugin.addEvent(customEvent, 'table');
```

If you need to know whether `addEvent` was successful or failed, use `addEventWithCallback`. You can pass `null` or `undefined` as database parameters and `defaultDatabase` configuration in `TreasureDataPlugin.setup({...})` will be used instead.


```javascript
const customEvent = {event: 'Custom event', data: new Date().getSeconds()};
TreasureDataPlugin.addEventWithCallback(customEvent, 'table', 'database', () => {
  console.log('Add Event Successfully');
}, (errorCode, errorMessage) => {
  console.log('Add Event Failed', errorCode, errorMessage);
});
```

## Upload Buffered Events to TreasureData

You can upload all buffered events to Treasure Data at anytime using the `uploadEvent` function


```javascript
TreasureDataPlugin.uploadEvents();
```

If you need to know whether `uploadEvents` was successful or  failed, use `uploadEventsWithCallback` instead.


```javascript
TreasureDataPlugin.uploadEventsWithCallback(() => {
  console.log('Upload events successfully')
}, (errorCode, errorMessage) => {
  console.log('Failed to upload events', errorCode, errorMessage);
});
```

## Further Reading

Here are additional resources that may be helpful. If you prefer to look at the source code, select the GitHub links below.

- [Cordova SDK - API Reference](/products/customer-data-platform/integration-hub/streaming/mobile/cordova/api)
- [Android SDK](/products/customer-data-platform/integration-hub/streaming/mobile/android)
- [iOS SDK](/products/customer-data-platform/integration-hub/streaming/mobile/ios)
- [Cordova SDK Source Code](https://github.com/treasure-data/td-cordova-sdk)
- [Android SDK Source Code](https://github.com/treasure-data/td-android-sdk)
- [iOS SDK Source Code](https://github.com/treasure-data/td-ios-sdk)