Skip to main content

Advanced guide

Last updated 10/03/2026

1. Send events​

After the SDK is initialized, you can start tracking data to collect user behavior. In most cases, regular events meet business needs. You can also use first events, updatable events, and other event types based on your actual business scenarios.

1.1 Regular events​

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

// Set event properties
properties := map[string]interface{}{
"product_name": "Product name",
}
// Report an event named product_buy. account_id and distinct_id can't both be empty
te.Track("account_id", "distinct_id", "product_buy", properties)

1.2 First events​

A first event is an event that is recorded only once for a given device or an ID of another dimension. For example, in some scenarios you may want to record the activation event on a device; you can report this data as a first event.

// Report a first event named device_activation, with first_check_id set to first_event_flag
properties := map[string]interface{}{
"prop_string": "value",
}
err := te.TrackFirst("account_id", "distinct_id", "device_activation", "first_event_flag", properties)

Note: Because the first-event check is performed on the server, first events are stored with a 1-hour delay by default.

1.3 Updatable events​

You can use updatable events to modify event data in specific scenarios. An updatable event requires an ID that identifies the event, which you pass in when you create the updatable event object. The AE backend determines which data to update based on the event name and event ID.

// Report an updatable event. Assume the event name is UPDATABLE_EVENT
// After reporting, the event property status is 3 and price is 100
properties := make(map[string]interface{})
properties["status"] = 3
properties["price"] = 100
err := te.TrackUpdate("account_id", "distinct_id", "UPDATABLE_EVENT", "test_event_id", properties)
propertiesNew := make(map[string]interface{})
propertiesNew["status"] = 5

// After reporting, the event property status is updated to 5, and price stays the same
err = te.TrackUpdate("account_id", "distinct_id", "UPDATABLE_EVENT", "test_event_id", propertiesNew)

1.4 Overwritable events​

Overwritable events are similar to updatable events, except that an overwritable event completely overwrites historical data with the latest data. In effect, the previous record is deleted and the latest data is ingested. The AE backend determines which data to update based on the event name and event ID.

// Report an overwritable event named OVERWRITE_EVENT
// After reporting, the event property status is 3 and price is 100
properties := make(map[string]interface{})
properties["status"] = 3
properties["price"] = 100
err := te.TrackOverwrite("account_id", "distinct_id", "OVERWRITE_EVENT", "test_event_id", properties)

// After reporting, the event property status is updated to 5, and the price property is deleted
propertiesNew := make(map[string]interface{})
propertiesNew["status"] = 5
err = te.TrackOverwrite("account_id", "distinct_id", "OVERWRITE_EVENT", "test_event_id", propertiesNew)

2. User properties​

The user property APIs supported by the AE platform are: UserSet, UserSetOnce, UserAdd, UserDelete, UserUnset, UserAppend, and UserUniqAppend.

2.1 UserSet​

For general user properties, you can call UserSet to set them. Properties uploaded through this API overwrite the original values. If the user property doesn't exist yet, it is created with the same type as the value passed in. The following example sets the username:

//userName is now TA
err := te.UserSet("account_id", "distinct_id", map[string]interface{}{
"user_name": "TA",
})
//userName is now AE
err = te.UserSet("account_id", "distinct_id", map[string]interface{}{
"user_name": "AE",
})

2.2 UserSetOnce​

If a user property only needs to be set once, you can call UserSetOnce to set it. If the property already has a value, this call is ignored. The following example again sets the username:

//first_payment_time is 2018-01-01 01:23:45.678
err := te.UserSetOnce("account_id", "distinct_id", map[string]interface{}{
"first_payment_time":"2018-01-01 01:23:45.678",
})

//first_payment_time is still 2018-01-01 01:23:45.678
err = te.UserSetOnce("account_id", "distinct_id", map[string]interface{}{
"first_payment_time":"2018-12-31 01:23:45.678",
})

2.3 UserAdd​

To upload a numeric property, you can call UserAdd to accumulate its value. If the property hasn't been set yet, it is assigned 0 before the calculation. You can pass a negative value, which is equivalent to subtraction. The following example accumulates the total payment amount:

//Upload a user property. The value of "total_revenue" is now 30
err := te.UserAdd("account_id", "distinct_id", map[string]interface{}{
"total_revenue":30,
})
//Upload the user property again. The value of "total_revenue" is accumulated to 678
err = te.UserAdd("account_id", "distinct_id", map[string]interface{}{
"total_revenue":648,
})

The property key is a string, and the Value can only be a number.

2.4 UserAppend​

You can call UserAppend to append values to an array-type user property.

//The value of user_list is now ["apple","ball"]
err := te.UserAppend("account_id", "distinct_id", map[string]interface{}{
"user_list": []string{"apple", "ball"},
})

2.5 UserUniqAppend​

Starting from v1.6.0, you can call UserUniqAppend to append elements to array-type user properties. Calling UserUniqAppend deduplicates the appended user property values, whereas the UserAppend API doesn't deduplicate, so the user property may contain duplicates.

//in this case, the property value of user_list is ["apple","ball"]
err := te.UserAppend("account_id", "distinct_id", map[string]interface{}{
"user_list": []string{"apple", "ball"},
})
//in this case, the property value of user_list is ["apple","apple","ball","cube"]
err = te.UserAppend("account_id", "distinct_id", map[string]interface{}{
"user_list": []string{"apple", "cube"},
})
//in this case, the property value of user_list is ["apple","ball","cube"]
err = te.UserUniqAppend("account_id", "distinct_id", map[string]interface{}{
"user_list": []string{"apple", "cube"},
})

2.6 UserUnset​

To clear the value of a user property, you can call UserUnset to clear the specified property. If the property hasn't been created in the cluster yet, UserUnset does not create it

// Clear a user property of a user. Pass in the property name as the parameter
err := te.UserUnset("account_id","distinct_id", property_name)

UserUnset: the value passed in is the key of the property to be cleared.

2.7 UserDelete​

To delete a user, you can call UserDelete. After that, you can no longer query this user's user properties, but the events generated by the user can still be queried. This operation may have irreversible consequences, so use it with caution

err := te.UserDelete("account_id", "distinct_id")

3. Other features​

3.1 BatchConsumer​

Note

When the data volume is too large or the network is abnormal, data may be lost. We don't recommend using it in the production environment

Transfers data to the AE server in batches in real time, without a transfer tool.

consumer, err := thinkingdata.NewBatchConsumer("SERVER_URL", "APP_ID")
te := thinkingdata.New(consumer)

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
Was this page helpful?