Treasure Data Unity SDK

This article will help you start sending the 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 mobile app activities.

Table of Contents


  • 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.

For iOS Application development

On Xcode, the following steps are needed.

  • In Build Phases -> Link Binary With Libraries, add libz.tbd
  • In Build Phases -> Compile Sources add -fno-objc-arc compile flag to

Step 2: Initialize the Library

Next, please initialize the library in your app like followings.

public class MyTreasureDataPlugin : MonoBehaviour {
  private TreasureData td = null;

  void Start() {
    td = new TreasureData("YOUR_API_KEY");
     * This is optional, but you can encrypt the buffered data on mobile devices.
     * You can prevent people from checking the buffered events on the disk.
    // TreasureData.InitializeEncryptionKey("RANDOM_STRING_TO_ENCRYPT_DATA");

  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); }

The apikey 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.

It depends on the characteristic of your application when to upload and how often to upload buffered events. But we recommend the followings at least as good timings to upload.

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

Step 3: Send Events to the Cloud

Next, please call AddEvent() function at the appropriate timing within your applications. This example sends the event to table table_b within 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;
td.AddEvent("database_a", "table_b", ev);
IP whitelist won't be applied to any import from iOS or Android SDK. Also we've seen a lot of cases where a lot of iOS or Android 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.

Advanced Tips

Retry uploading and deduplication

This SDK imports events in exactly once style with the combination of these 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 the transparency, we’re open sourcing our Unity SDK on Github. Please check the repository if necessary.

Last modified: May 23 2017 07:16:52 UTC

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