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

Prerequisites

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

Step 2: Initialize the Library

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

public class MyTreasureDataPlugin : MonoBehaviour {
#if UNITY_IPHONE || UNITY_ANDROID
  private TreasureData td = null;

  void Start() {
    TreasureData.InitializeApiEndpoint("https://in.treasuredata.com");
    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) {
      td.UploadEvents(
        delegate() { Debug.LogWarning ("UploadEvents Success!!! "); },
        delegate(string errorCode, string errorMsg) { Debug.LogWarning ("UploadEvents Error!!! errorCode=" + errorCode + ", errorMsg=" + errorMsg); }
      );
    }
  }
#endif
}

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.

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);
Untitled-3
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.

Next Steps

For the transparency, we’re open sourcing our Unity SDK on Github. Please check the repository if necessary.


Last modified: Feb 23 2017 10:25:06 UTC

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