# Treasure Data Unity SDK API Reference ## Overview ```cpp class TreasureData; ``` The `ITreasureData` interface wraps the native iOS and Android SDKs for use in Unity development. If you have any questions please reference the underlying SDK API Reference Documentation. ## Public Property ### property Instance ```cpp static ITreasureData Instance; ``` Treasure Data object instance. Make sure to call `InitializeWithAPIKey` before accessing. ### property DefaultDatabase ```cpp string DefaultDatabase; ``` The default database to send events to. Treasure Data recommends setting this during initialization. ### property DefaultTable ```cpp string DefaultTable; ``` The default Table to send events to. Treasure Data recommends setting this during initialization. ## Public Functions ### InitializeApiKey ```cpp static inline void InitializeApiKey( string apiKey ) ``` Set the write-only TD API key. Treasure Data recommends setting this during initialization. | Param | Description | | --- | --- | | apiKey | TD Write-only API Key | ### EnableLogging ```cpp static inline void EnableLogging() ``` Enable the debug log. ### DisableLogging ```cpp static inline void DisableLogging() ``` Disable the debug log. ### InitializeApiEndpoint ```cpp static inline void InitializeApiEndpoint( string apiEndpoint ) ``` Set the TD API Endpoint to use. The default is `records.in.treasuredata.com`. *Example* ```cpp TreasureData.InitializeApiEndpoint("https://us01.records.in.treasuredata.com"); ``` | Param | Description | | --- | --- | | apiEndpoint | API Endpoint to send data to. | ### InitializeEncryptionKey ```cpp static inline void InitializeEncryptionKey( string encryptionKey ) ``` Initialize the AES 128 encryption. The string must be less than 128 characters. This will encrypt event data passed to `AddEvent`. *Example* ```cpp TreasureData.InitializeEncryptionKey("hello world"); : TreasureData.Instance.AddEvent("testdb", "unitytbl", ev); ``` | Param | Description | | --- | --- | | encryptionKey | string with the encryption key to use for local device encryption. | ### InitializeDefaultApiKey ```cpp static inline void InitializeDefaultApiKey( string apiKey ) ``` Set the default API key to be used across all initialized TD objects automatically. Treasure Data recommends you use a write-only API key. | Param | Description | | --- | --- | | apiKey | TD write-only api key | ### InitializeDefaultDatabase ```cpp static inline void InitializeDefaultDatabase( string database ) ``` Set the default database to use for all initialized TD objects automatically. Make sure that this database already exists in your Treasure Data account. *Example* ```cpp TreasureData.InitializeDefaultDatabase("testdb"); : TreasureData.Instance.AddEvent("unitytbl", ev); ``` | Param | Description | | --- | --- | | database | Name of the database | ### StartGlobalSession ```cpp static inline void StartGlobalSession() ``` Start tracking a global session. ### EndGlobalSession ```cpp static inline void EndGlobalSession() ``` End tracking a global session. ### GetGlobalSessionId ```cpp static inline string GetGlobalSessionId() ``` Get the current global session ID. ### EnableCustomEvent ```cpp inline void EnableCustomEvent() ``` Enable custom event tracking. ### DisableCustomEvent ```cpp inline void DisableCustomEvent() ``` Disable custom event tracking. ### IsCustomEventEnabled ```cpp inline bool IsCustomEventEnabled() ``` Whether or not the custom event tracking is enabled. **Return**: true: enabled, false: disabled ### EnableAppLifecycleEvent ```cpp inline void EnableAppLifecycleEvent() ``` Enable app lifecycle tracking. This setting has no effect on custom tracking. ### DisableAppLifecycleEvent ```cpp inline void DisableAppLifecycleEvent() ``` Disable app lifecycle tracking. This setting has no effect on custom tracking ### IsAppLifecycleEventEnabled ```cpp inline bool IsAppLifecycleEventEnabled() ``` Determine whether or not application lifecycle tracking is enabled. **Return**: true : enabled, false : disabled ### EnableInAppPurchaseEvent ```cpp inline void EnableInAppPurchaseEvent( bool flag =true ) ``` Enable tracking In App Purchase event automatically. This is disabled by default. | Param | Description | | --- | --- | | flag | Boolean true / false | ### DisableInAppPurchaseEvent ```cpp inline void DisableInAppPurchaseEvent() ``` Enable tracking In App Purchase event automatically. This is disabled by default. ### IsInAppPurchaseEventEnabled ```cpp inline bool IsInAppPurchaseEventEnabled() ``` Determine whether or not the In App Purchase tracking is enabled. **Return**: true: enabled, false: disabled ### ResetUniqId ```cpp inline void ResetUniqId() ``` Reset UUID and send `forget_device_uuid` event with old UUID. ### AddEvent ```cpp inline bool AddEvent( string table, string key, object val ) ``` Add an event to local buffer. | Param | Description | | --- | --- | | table | the event's destination table | | key | key of the event object | | val | value of the event object | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string table, string key, object val, OnSuccess onSuccess, OnError onError ) ``` Add an event to local buffer with callback. | Param | Description | | --- | --- | | table | the event's destination table | | key | key of the event object | | val | value of the event object | | onSuccess | callback function for successful execution | | onError | callback function for failure | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string table, Dictionary< string, object > obj ) ``` Add an event to local buffer. Use DefaultDatabase as database. | Param | Description | | --- | --- | | table | the event's destination table | | obj | the event object | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string table, Dictionary< string, object > obj, OnSuccess onSuccess, OnError onError ) ``` Add an event to local buffer with callback. Use DefaultDatabase as database. | Param | Description | | --- | --- | | table | the event's destination table | | obj | the event object | | onSuccess | callback function for successful execution | | onError | callback function for failure | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string database, string table, string key, object val ) ``` Add an event to local buffer. | Param | Description | | --- | --- | | database | the event's destination database | | table | the event's destination table | | key | key of the event object | | val | value of the event object | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string database, string table, string key, object val, OnSuccess onSuccess, OnError onError ) ``` Add an event to local buffer with callback. | Param | Description | | --- | --- | | database | the event's destination database | | table | the event's destination table | | key | key of the event object | | val | value of the event object | | onSuccess | callback function for successful execution | | onError | callback function for failure | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string database, string table, Dictionary< string, object > events ) ``` Add events to local buffer. | Param | Description | | --- | --- | | database | the event's destination database | | table | the event's destination table | | events | event data | **Return**: bool success / failure boolean ### AddEvent ```cpp inline bool AddEvent( string database, string table, Dictionary< string, object > events, OnSuccess onSuccess, OnError onError ) ``` Add events to local buffer with callback. | Param | Description | | --- | --- | | database | the event's destination database | | table | the event's destination table | | events | event data | | onSuccess | callback function for successful execution | | onError | callback function for failure | **Return**: bool success / failure boolean ### UploadEvents ```cpp inline void UploadEvents() ``` Upload events from local buffer to Treasure Data. ### UploadEvents ```cpp inline void UploadEvents( OnSuccess onSuccess, OnError onError ) ``` Upload events from local buffer to Treasure Data. Triggers `onSuccess` and `onError` callbacks on success or failure. | Param | Description | | --- | --- | | onSuccess | Callback function to trigger on successful upload. | | onError | Callback function to trigger on failure to upload. | ### DisableAutoAppendUniqId ```cpp inline void DisableAutoAppendUniqId() ``` Disable the automatically appended `td_uuid` column. ### EnableAutoAppendUniqId ```cpp inline void EnableAutoAppendUniqId() ``` Automatically append `td_uuid` column for every event. The value is randomly generated and persists across application launches and events. Basically, it is used to represent a unique application installation instance. This is disabled by default. This value won't change until the application is uninstalled. *Example* ```cpp TreasureData.Instance.EnableAutoAppendUniqId(); : TreasureData.Instance.AddEvent("unitytbl", "name", "foobar"); // Outputs =>> // {"td_uuid_id":"cad88260-67b4-0242-1329-2650772a66b1", "name":"foobar", ... } ``` ### DisableAutoAppendModelInformation ```cpp inline void DisableAutoAppendModelInformation() ``` Disable automatic tracking of model information. ### EnableAutoAppendModelInformation ```cpp inline void EnableAutoAppendModelInformation() ``` Enable automatic tracking of device model information. *Example* ```cpp TreasureData.Instance.EnableAutoAppendModelInformation(); : TreasureData.Instance.AddEvent("unitytbl", "name", "foobar"); // Outputs =>> // {"td_device":"iPod touch", "name":"foobar", ... } ``` It outputs the following column names and values: * iOS * `td_device` : UIDevice.model * `td_model` : UIDevice.model * `td_os_ver` : UIDevice.model.systemVersion * `td_os_type` : "iOS" * Android * `td_board` : android.os.Build::BOARD * `td_brand` : android.os.Build::BRAND * `td_device` : android.os.Build::DEVICE * `td_display` : android.os.Build::DISPLAY * `td_model` : android.os.Build::MODEL * `td_os_ver` : android.os.Build.VERSION::SDK_INT * `td_os_type` : "Android" ### DisableAutoAppendAppInformation ```cpp inline void DisableAutoAppendAppInformation() ``` Disable automatic tracking of application information. ### EnableAutoAppendAppInformation ```cpp inline void EnableAutoAppendAppInformation() ``` Application package version information will be added to each event automatically *Example* ```cpp TreasureData.Instance.EnableAutoAppendAppInformation(); : TreasureData.Instance.AddEvent("unitytbl", "name", "foobar"); // Outputs =>> // {"td_app_ver":"1.2.3", "name":"foobar", ... } ``` It outputs the following column names and values: * iOS * `td_app_ver` : Core Foundation key `CFBundleShortVersionString` * `td_app_ver_num` : Core Foundation key `CFBundleVersion` * Android * `td_app_ver` : android.content.pm.PackageInfo.versionName (from Context.getPackageManager().getPackageInfo()) * `td_app_ver_num` : android.content.pm.PackageInfo.versionCode (from Context.getPackageManager().getPackageInfo()) ### DisableAutoAppendLocaleInformation ```cpp inline void DisableAutoAppendLocaleInformation() ``` Disable automatic tracking of locale information. ### EnableAutoAppendLocaleInformation ```cpp inline void EnableAutoAppendLocaleInformation() ``` Enable automatic tracking of locale information by adding it to each event. *Example* ```cpp TreasureData.Instance.EnableAutoAppendLocaleInformation(); : td.AddEvent("unitytbl", "name", "foobar"); // Outputs =>> // {"td_locale_lang":"en", "name":"foobar", ... } ``` It outputs the following column names and values: * iOS * `td_locale_country` : [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode] * `td_locale_lang` : [[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode] * Android * `td_locale_country` : java.util.Locale.getCountry() (from Context.getResources().getConfiguration().locale) * `td_locale_lang` : java.util.Locale.getLanguage() (from Context.getResources().getConfiguration().locale) ### EnableServerSideUploadTimestamp ```cpp inline void EnableServerSideUploadTimestamp() ``` Automatically append the server side upload time stamp when the event is received on server. This is disabled by default. The column name is set to `time` by default. *Example* : Use the server side upload time as `time` column. ```cpp td.EnableServerSideUploadTimestamp(); ``` ### EnableServerSideUploadTimestamp ```cpp inline void EnableServerSideUploadTimestamp( string v ) ``` Automatically append the server side upload time stamp when the event is received on server. Disabled by default. *Example* : Add server side upload time as a customized column name ```cpp td.EnableServerSideUploadTimestamp("server_upload_time"); ``` | Param | Description | | --- | --- | | columnName | Set the column to write the uploaded time value | ### DisableServerSideUploadTimestamp ```cpp inline void DisableServerSideUploadTimestamp() ``` Disable the uploading time column. ### EnableAutoAppendRecordUUID ```cpp inline void EnableAutoAppendRecordUUID() ``` Automatically appends a random and unique ID for each event, using `record_uuid` as the default column name. Each event has a different UUID. This is disabled by default. *Example* ```cpp TreasureData.Instance.EnableAutoAppendRecordUUID(); // If you want to customize the column name, pass it to the API // TreasureData.Instance.EnableAutoAppendRecordUUID("my_record_uuid"); : TreasureData.Instance.AddEvent(...); ``` ### EnableAutoAppendRecordUUID ```cpp inline void EnableAutoAppendRecordUUID( string v ) ``` Automatically append a random and unique ID for each event. Disabled by default. | Param | Description | | --- | --- | | columnName | The column to write the ID | ### DisableAutoAppendRecordUUID ```cpp inline void DisableAutoAppendRecordUUID() ``` Disable appending ID for each event. ### DisableRetryUploading ```cpp inline void DisableRetryUploading() ``` Disable automatic retry uploading. After it is disabled, the application only attempts to upload events once per upload event. ### EnableRetryUploading ```cpp inline void EnableRetryUploading() ``` Enable automatic retry uploading. Once enabled, the application continues trying to upload events until all events are uploaded successfully. ### ClearFirstRun ```cpp inline void ClearFirstRun() ``` Clear first run flag. ### GetSessionId ```cpp inline string GetSessionId() ``` Get current session ID. **Return**: string Session ID