Advanced guide
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:
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
properties = {
array: ["str1", "11", Time.now, "2020-02-11 17:02:52.415"],
prop_date: Time.now,
prop_double: 134.1,
prop_string: 'hello world',
prop_bool: true,
}
ta.track(event_name: 'test_event', distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: 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.
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
properties = {
array: ["str1", "11", Time.now, "2020-02-11 17:02:52.415"],
prop_date: Time.now,
prop_double: 134.1,
prop_string: 'hello world',
prop_bool: true,
}
ta.track(event_name: 'test_event', distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, first_check_id:'first_id', properties: 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.
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
properties = {
array: ["str1", "11", Time.now, "2020-02-11 17:02:52.415"],
prop_date: Time.now,
prop_double: 134.1,
prop_string: 'hello world',
prop_bool: true,
}
ta.track_update(event_name: 'update', event_id: 'id123', distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties:properties)
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.
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
properties = {
array: ["str1", "11", Time.now, "2020-02-11 17:02:52.415"],
prop_date: Time.now,
prop_double: 134.1,
prop_string: 'hello world',
prop_bool: true,
}
ta.track_overwrite(event_name: 'update', event_id: 'id123', distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties:properties)
2. User properties
The user property APIs supported by the AE platform are: user_set, user_set_once, user_add,
user_unset、user_del、user_append、user_uniq_append。
2.1 user_set
For general user properties, you can call user_set 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 four properties: array, prop_double, prop_string, and prop_int:
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
user_data = {
array: ["str1", 11, 22.22],
prop_double: 134.12,
prop_string: 'hello',
prop_int: 666,
}
ta.user_set(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_data)
2.2 user_set_once
If a user property only needs to be set once, you can call user_set_once to set it. If the property already has a value, this record is ignored. The following example sets the prop_int_new property:
user_set_once_data = {
prop_int_new: 888,
}
ta.user_set_once(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_set_once_data)
2.3 user_add
To upload a numeric property, you can call user_add 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:
# Accumulate values of numeric properties
ta.user_add(distinct_id: DEMO_DISTINCT_ID, properties: {prop_int: 10, prop_double: 15.88})
The property key is a string, and the Value can only be a number.
2.4 user_append
You can call user_append to append values to an array-type user property.
user_append_data = {
array: %w[33 44]
}
ta.user_append(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_append_data)
user_append_data_new = {
array: %w[44 55]
}
ta.user_append(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_append_data_new)
2.5 user_uniq_append
You can call user_uniq_append to append values to array-type user properties. user_uniq_append deduplicates the appended values, while user_append does not, so the user property may contain duplicates.
user_append_data = {
array: %w[33 44]
}
ta.user_append(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_append_data)
user_uniq_append_data = {
array: %w[44 55]
}
ta.user_uniq_append(distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: user_uniq_append_data)
2.6 user_unset
To clear the value of a user property, you can call user_unset to clear the specified property. If the property hasn't been created in the cluster yet, user_unset does not create it
ta.user_unset(distinct_id: DEMO_DISTINCT_ID, property: [:prop_string, :prop_int])
user_unset: The value passed in is the Key of the property to clear.
2.7 user_del
To delete a user, you can call user_del. 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
ta.user_del(distinct_id: DEMO_DISTINCT_ID)
3. Other features
3.1 TDBatchConsumer
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 = ThinkingData::TDBatchConsumer.new('SERVER_URL', 'APPID', 30)
ta = ThinkingData::TDAnalytics.new(consumer)
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
properties = {
array: ["str1", "11", Time.now, "2020-02-11 17:02:52.415"],
prop_date: Time.now,
prop_double: 134.1,
prop_string: 'hello world',
prop_bool: true,
}
ta.track(event_name: 'test_event', distinct_id: DEMO_DISTINCT_ID, account_id: DEMO_ACCOUNT_ID, properties: properties)
Parameters:
-
APPID: The APP ID 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

