menu
Is this helpful?

# C#-Advanced

# 1. Managing User Identity

SDK instances would use random UUID as the default distinct ID of each user by default, which would be used as the identity identification ID of users under an unlogged-in state. It should be noted that the distinct ID would change after the user reinstalled the App or use the APP with a new device.

# 1.1. Identify

TIP

Generally speaking, you do not need to customize a distinct ID. Please ensure that you understand User Identification Rulesbefore setting a distinct ID.

If you need to change the distinct ID, please call the API immediately after SDK is initialized. To avoid the generation of useless accounts, please do not call such a process multiple times.

If your App has its own distinct ID management system for each user, you can call SetIdentity to set the distinct ID:

TDAnalytics.SetDistinctId("Thinker");
TDAnalytics.GetDistinctId()

# 1.2 Login

When the users log in, Login could be called to set the account ID of the user. TE would use the account ID as the identity identification ID, and the account ID that has been set would be saved before Logout is called. The previous account ID would be replaced if Login has been called multiple times.

TDAnalytics.Login("TA");

Login events wouldn't be uploaded in this method.

# 1.3 Removing Account ID

After the user logs out, Logout could be called to remove the account ID. The distinct ID would be used as the identity identification ID before the next time Login is called.

TDAnalytics.Logout();

It is recommended that you call logout upon explicit logout event. For example, call Logout when the user commits the behavior of canceling an account; do not call such a process when the App is closed.

Logout events wouldn't be uploaded in this method.

# Sending Events

After SDK is initialized, you can track user behaviour data. In general, ordinary events could meet business requirements. You can also use the First/Updatable Event based on your own service scenario.

# 2.1 Ordinary Events

You can call Track to upload events. It is suggested that you set event properties based on the data tracking plan drafted previously. Here is an example of a user buying an item:

Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("product_name", "product name");
TDAnalytics.Track( "product_buy",dic);

# 2.2 First Events

The First Events refers to events that would only be recorded once for the ID of a certain device or other dimensions. For example, under certain scenarios, you may want to record the activation event on a certain device. In this case, you can perform data tracking with the First Event.

Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("key", "value");
TDFirstEventModel firstEventModel = new TDFirstEventModel("device_activation", dic);
TDAnalytics.Track(firstEventModel);

If you want to judge whether an event is the First Event from other dimensions, you can define a first_check_id for the First Event:

Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("key", "value");
TDFirstEventModel firstEventModel = new TDFirstEventModel("device_activation", dic,"TA");
TDAnalytics.Track(firstEventModel);

Note: Since the server has to check whether the event is the First Event, the first event will be put in storage one hour later by default.

# 2.3 Updatable Events

You can meet the requirements for event data modification under specific scenarios through Updatable Events. The ID of Updatable Events should be specified and uploaded when the objects of Updatable Events are created. TE would determine the data to be updated according to the event name and event ID.

 //The event property status is 3 after reporting, with the price being 100
Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("price", 100);
dic.Add("status",3);
TDUpdatableEventModel updatableEventModel = new TDUpdatableEventModel("UPDATABLE_EVENT", dic, "updateEventId");
TDAnalytics.Track(updatableEventModel);

//The event property status is 5 after reporting, with the price remaining the same
Dictionary<string, Object> dic1 = new Dictionary<string, object>();
dic1.Add("status",5);
TDUpdatableEventModel updatableEventModel = new TDUpdatableEventModel("UPDATABLE_EVENT", dic1,"updateEventId");
TDAnalytics.Track(updatableEventModel);

# 2.4 Overwritable Events

Despite the similarity with Updatable Events, Overwritable Events would replace all historical data with the latest date. Looking from the perspective of effect, such a process is equivalent to the behavior of deleting the previous data while putting the latest data in storage. TE would determine the data to be updated according to the event name and event ID.

// Instance: Assume the event name is OVERWRITE_EVENT when reporting an overwritable event
//The event property status is 3 after reporting, with the price being 100
Dictionary<string, Object> dic = new Dictionary<string, object>();
dic.Add("price", 100);
dic.Add("status",3);
TDOverwritableEventModel overwritableEventModel = new TDOverwritableEventModel("OVERWRITABLE_EVENT", dic,"eventId");
TDAnalytics.Track(overwritableEventModel);

//The event property status is 5 after reporting, with the price deleted
Dictionary<string, Object> dic1 = new Dictionary<string, object>();
dic1.Add("status",5);
TDOverwritableEventModel overwritableEventModel = new TDOverwritableEventModel("OVERWRITABLE_EVENT", dic1,"eventId");
TDAnalytics.Track(overwritableEventModel);

# 3. User Properties

User property setting APIs supported by TE include: UserSet,UserSetOnce,UserAdd,UserUnset,UserDelete,UserAppend

# 3.1 UserSet

You can call UserSet to set general user properties. The original properties would be replaced if the properties uploaded via the API are used. The type of newly-created user properties must conform to that of the uploaded properties. User name setting is taken as the example here:

//the username now is TA
TDAnalytics.UserSet(new Dictionary<string, object>(){{"user_name", "TA"}});
//the userName now is TE
TDAnalytics.UserSet(new Dictionary<string, object>(){{"user_name", "TE"}});

# 3.2 UserSetOnce

If the user property you want to upload only needs to be set once, you can call UserSetOnce to set the property. If such property had been set before, this message would be ignored. Let's take the setting of the first payment time as an example:

//first_payment_time is 2018-01-01 01:23:45.678
TDAnalytics.UserSetOnce(new Dictionary<string, object>(){{"first_pay_time","2018-01-01 01:23:45.67"}});
//first_payment_time is still 2018-01-01 01:23:45.678
TDAnalytics.UserSetOnce(new Dictionary<string, object>(){{"first_pay_time","2018-12-31 01:23:45.678"}});

# 3.3 UserAdd

When you want to upload numeric attributes for cumulative operation, you can call UserAdd.

If the property has not been set, it would be given a value of 0 before computing. A negative value could be uploaded, which is equivalent to subtraction operation. Let's take the accumulative payment amount as an example:

//in this case, the total_revenue is 30
TDAnalytics.UserAdd(new Dictionary<string, object>(){{"total_revenue",30}});
//in this case, the total_revenue is 678
TDAnalytics.UserAdd(new Dictionary<string, object>(){{"total_revenue",648}})

The set attribute key is a string, and the Value is only allowed to be a numeric value.

# 3.4 UserUnset

When you need to clear the user properties of users, you can call UserUnSet to clear specific properties. UserUnSet would not create properties that have not been created in the cluster.

List<string> list2 = new List<string>();
list2.Add("nickname");
list2.Add("age");
TDAnalytics.UserUnSet(list2);

# 3.5 UserDelete

You can call UserDelete to delete a user. After deleting the user, you would no longer be able to inquire about its user property, but could still obtain the events data triggered by the user.

TDAnalytics.UserDelete();

# 3.6 UserAppend

You can call UserAppend to add user properties of array type.

Dictionary<string, object> dictionary = new Dictionary<string, object>();
List<string> list6 = new List<string>();
list6.Add("true");
list6.Add("test");
dictionary.Add("arrkey4", list6);
TDAnalytics.UserAppend( dictionary);

# Others

# 4.1 Printing Log

During the process of SDK integration, you can perform real-time debugging by checking SDK's logs in the IDE console or using the Debug feature of TE.

TDAnalytics.EnableLog(true);

# 4.2 Preset Properties of All Events

Property name Display name Property type Instruction
#ip IP address Text IP address of the user, based on which TE would get the geographical location of the user
#country Country Text The country where the user is located; generated based on the IP address
#country_code Country code Text The code of the country where the user is located (ISO 3166-1 alpha-2, two English characters in upper case); generated based on the IP address
#province Province Text The province or state where the user is located; generated based on the IP address
#city City Text The city where the user is located; generated based on the IP address
#os OS Text E.g., Android, iOS, Mac OS, etc.
#device_id Device ID Text The ID of the user device; IDFV or UUID of the user for iOS; androidID for Android
#lib SDK type Text The type of the SDK to which you access, e.g., Android,iOS, etc.
#lib_version SDK version Text The version of the SDK to which you access

#