Visit our new documentation site! This documentation page is no longer updated.

Treasure Data Unity SDK (PC Environment)

You can send data from your Unity app to Treasure Data, using our Unity SDK library. In this way, you don’t have to install anything on your server-side to track the Unity app activities.

Table of Contents

GDPR compliance

To support compliance with national and global data privacy requirements such as the European General Data Privacy Regulation, our SDK provides methods that control the collection and tracking of personal data and metadata in applications and websites. When your company defines data privacy policies around personal data, you can use these methods in your code to implement default data collection behaviors, and add controls for individuals to use to manage data collection and privacy themselves.

Customers of Treasure Data must ensure that their usage of the SDK, including its use that collects personal data, complies with the legal agreement that governs access to and use of the Treasure Data service, including specifically Treasure Data's current Terms of Service (, privacy policy (, and Privacy Statement for Customer Data (


  • Basic knowledge of Unity Development
  • Basic knowledge of Treasure Data.

Step 1: Install the Library

Download the most recent version of our Unity package and import it into your Unity project using Assets –> Import Package –> Custom Package.

This feature is available TD Unity SDK version 0.1.11 or newer

Step 2: Enable PC Mode

Add a symbol TD_SDK_DEV_MODE to Player Settings > Scripting Define Symbols

Step 3: Initialize the Library

Next, initialize the library in your app as follows.

public class MyTreasureDataPlugin : MonoBehaviour {

  void OnRuntimeInitialization() {
    // If you want to use a TDClient over scenes, pass `true` to
    // `SimpleTDClient.Create` to prevent it from being removed.
    // Otherwise, pass 'false' If you want to use a TDClient only
    // within a scene, don't pass `true` to `SimpleTDClient.Create`
    // so that you can prevent object leaks.

  void OnApplicationPause(bool pauseStatus) {
    // Make an open request whenever app is resumed
    if (!pauseStatus) {
        delegate() { Debug.LogWarning ("UploadEvents Success!!! "); },
        delegate(string errorCode, string errorMsg) { Debug.LogWarning ("UploadEvents Error!!! errorCode=" + errorCode + ", errorMsg=" + errorMsg); }

  void OnApplicationQuit()
    // Make an open request when app is quitting
      delegate() { Debug.LogWarning ("UploadEvents Success!!! "); },
      delegate(string errorCode, string errorMsg) { Debug.LogWarning ("UploadEvents Error!!! errorCode=" + errorCode + ", errorMsg=" + errorMsg); }

The api key can be retrieved from the console (click here). It’s recommended to use write-only API key for SDKs. Let us know if you’re having any build issues.

When to upload and how often to upload buffered events depends on the characteristics of your application. Good times to upload include:

  • When the current screen is closing or moving to background
  • When closing the application

Step 4: Send Events to the Cloud

Next, call the AddEvent() function at the appropriate time within your applications. The following example shows an event sent to the table table_b within the database database_a.

Dictionary<string, object> ev = new Dictionary<string, object>();
ev["str"] = "strstr";
ev["int"] = 12345;
ev["long"] = 12345678912345678;
ev["float"] = 12.345;
ev["double"] = 12.3459832987654;
ev["bool"] = true;
TreasureData.Instance.AddEvent("database_a", "table_b", ev);
IP whitelist won't be applied. Also we've seen a lot of cases where a lot of PC devices have an invalid timestamp (like 1970/01/01), so we're currently ignoring the log which has a timestamp older than 7 days, and newer than 3 days ahead.

Tracking Application Lifecycle Events

The SDK can be optionally enabled to automatically captures app lifecycle events (disabled by default). You must explicitly enable this option. You can set the target table through setDefaultTable():

static void OnRuntimeInitialization() {
  // TreasureData client setup...
  TreasureData.DefaultTable = "app_lifecycles";

There are 3 kinds of event that are tracked automatically: Application Open, Install and Update. These events will be captured along with relevant metadata depends on the specific type of event:

Application Open

    "td_unity_event": "TD_UNITY_APP_OPEN",
    "td_app_ver": "1.0",

Application Install

    "td_unity_event": "TD_UNITY_APP_INSTALL",
    "td_app_ver": "1.0",

Application Update

    "td_unity_event": "TD_UNITY_APP_UPDATE",
    "td_app_ver": "1.1",
    "td_app_previous_ver": "1.0",


Respecting your user’s privacy is critical to any business. Treasure Data SDK can opt-out all events tracking for a particular device and de-identify users by resetting (or disabling completely). You can change the td_uuid to a different id for all subsequent events:

TreasureData.Instance.disableCustomEvent();        // disable your own events, ones that collect through manual calls to addEvent...
TreasureData.Instance.disableAppLifecycleEvent();  // disable the auto-collected app lifecycle events

Unlike other option flags, enable/disableCustomEvent and enable/disableAppLifecycleEvent are persistent settings, which mean these survive across app launches. It is important to use this call whenever your user signify that they don’t want to be tracked. It is not necessary to call the options on every TreasureData client setup.

TreasureData.Instance.disableAppendUniqId();  // temporary configuration, need to call on TreasureData client setup

resetUniqId also adds an audit event to the DefaultTable

    "td_unity_event": "forget_device_id",
    "td_uuid": <old_uuid>,

Advanced Tips

Retry uploading and deduplication

This SDK imports events in one manner with the following features:

  • This SDK keeps buffered events with adding unique keys and retries to upload them until confirming the events are uploaded and stored on server side (at least once)
  • The server side remembers the unique keys of all events within the past 1 hours by default and prevents duplicated imports (at most once)

As for the deduplication window is 1 hour by default, so it’s important not to keep buffered events more than 1 hour to avoid duplicated events.

Next Steps

For transparency, we are making our Unity SDK available as open source on Github. Check the repository to ensure that you have the most recent SDK.

Last modified: May 18 2018 19:25:55 UTC

If this article is incorrect or outdated, or omits critical information, let us know. For all other issues, access our support channels.