Skip to main content

C#

Last updated 10/05/2026
tip

Before you integrate the SDK, read Pre-integration preparation.

The C# SDK runs on Windows and Mac.

Latest version: 2.0.1

Update time: 2025-10-13

Downloads: SDK source code Download

Note

This document applies to v2.0.0 and later. For earlier versions, see the C# Integration Guide (Legacy)

1. Integrate the SDK​

1.1 Download the SDK​

Download the C# SDK source code from GitHub and add the files in the TDAnalyticsSDK directory to your project.

The SDK depends on the Newtonsoft.Json framework for JSON parsing. If you use Visual Studio, use NuGet to add the Newtonsoft.Json library.

1.2 Add the dynamic libraries​

  • Download the DLLs and unzip them (either download works)

  • On Windows, find the DLL files in the windows folder and copy them to the project root directory. (Note: If a DLL conflicts with a DLL already in the project, use the one already in the project.)

  • Mac

    • Step 1: Install the 4 libraries sqlite, curl, zlib, and openssl through Homebrew
    • Step 2: Find the library files (libCppWrapper.dylib, libthinkingdata.a) in the macOS folder and copy them to the project root directory

2. Initialization​

Import ThinkingData.Analytics and use the TDAnalytics class to initialize the AE instance.

using ThinkingData.Analytics;
TDAnalytics.Init(APPID,SERVER_URL);

Parameters:

  • APPID: The APPID of your project, which you can find on the Project Settings page in the AE backend

  • SERVER_URL: The URL that data is uploaded to

    • If you use the cloud service, enter: https://global-receiver-ta.thinkingdata.cn
    • If you use an on-premises deployment, bind a domain name to the data collection URL and configure an HTTPS certificate: https://your-domain-for-data-collection

3. Common features​

Before you use the common features, we recommend that you read the user identification rules. By default, the SDK generates a random number as the distinct ID and stores it locally. Before a user logs in, the distinct ID is used as the user's identifier. Note: The distinct ID changes when the user reinstalls the app or switches devices.

3.1 Set the account ID​

When a user logs in, you can call Login to set the user's account ID. The AE platform uses the account ID as the identifier, and the account ID you set is kept until Logout is called. Calling Login multiple times overwrites the previous account ID.

TDAnalytics.Login("TA");

You can call Login multiple times. Each call checks whether the account ID passed in matches the previously saved ID. If they match, the call is ignored; otherwise, the previous ID is overwritten.

This method does not upload a login event

3.2 Send events​

You can call Track to upload events. We recommend setting event properties and the conditions for sending them according to the tracking plan you prepared earlier. The following example tracks a user purchasing a product:

Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("channel", "ta");//String
dic.Add("id", 618834);//Number
dic.Add("isSuccess", true);//Boolean
dic.Add("create_date", Convert.ToDateTime("2019-7-8 20:23:22"));//Time
List<string> arr = new List<string>();
arr.Add("value");
dic.Add("arr", arr);//Array
TDAnalytics.Track("product_buy", dic);
  • The event name is of the string type. It must start with a letter, can contain digits, letters, and underscores "_", and can be up to 50 characters long.

  • Event properties are of the Dictionary type

    • Key is the name of the property and is of the string type. It must start with a letter, can contain only digits, letters, and underscores "_", and can be up to 50 characters long. Keys are not case-sensitive; AE converts all letters to lowercase
    • Value is the value of the property. Supported types are string, number, Boolean, time, and object

User properties have the same requirements as event properties

3.3 Set user properties​

For general user properties, you can call UserSet to set them. Properties uploaded through this API overwrite the existing property values. If the user property did not exist before, it is created with the same type as the value passed in. The following example sets the user name:

TDAnalytics.UserSet(new Dictionary<string, object>(){{"user_name", "TA"}});

4. Best practices​

The following sample code includes all of the operations above. We recommend using them in the following order:

using ThinkingData.Analytics;
//Initialize the SDK
TDAnalytics.Init(APPID,SERVER_URL);
//If the user has logged in, you can set the user's account ID as the unique identifier
TDAnalytics.Login("TA");
//Send an event
Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("channel", "ta");//String
dic.Add("id", 618834);//Number
dic.Add("isSuccess", true);//Boolean
dic.Add("create_date", Convert.ToDateTime("2019-7-8 20:23:22"));//Time
List<string> arr = new List<string>();
arr.Add("value");
dic.Add("arr", arr);//Array
// Complex data object
Dictionary<string, object> json = new Dictionary<string, object>();
json.Add("key", "value");
dic.Add("object", json);
// Complex data object group
List<Dictionary<string, object>> arr1 = new List<Dictionary<string, object>>();
Dictionary<string, object> json1 = new Dictionary<string, object>();
json1.Add("key", "value");
arr1.Add(json1);
dic.Add("objects", arr1);
TDAnalytics.Track("product_buy", dic);
//Set user properties
TDAnalytics.UserSet(new Dictionary<string, object>(){{"user_name", "TA"}});
Was this page helpful?