Ruby
This guide shows you how to integrate the Ruby SDK into your project.
Latest version: v2.0.2
Update time: 2026-06-05
Downloads: Source code
This document applies to v2.0.0 and later. For earlier versions, see Ruby SDK Integration Guide (V1)
1. Integrate the SDK
- Use the
gemcommand to integrate the SDK package
# Get the SDK
gem install thinkingdata-ruby
- Install Logbus
We recommend using SDK + LogBus to collect and report server-side data. To install Logbus, see the following document: LogBus user guide
2. Initialization
The following sample code initializes the SDK:
require 'thinkingdata-ruby'
consumer = ThinkingData::TDLoggerConsumer.new("LOG_DIRECTORY")
ta = ThinkingData::TDAnalytics.new(consumer)
LOG_DIRECTORY is the path of the local folder that data is written to. Set the folder that LogBus monitors to this path, and LogBus monitors and uploads the data.
3. Common features
To make sure that the distinct ID and account ID can be bound correctly, if your game uses both the distinct ID and the account ID, we strongly recommend that you upload both IDs. Otherwise, accounts may fail to match and users may be counted more than once. For details on ID binding rules, see User identification rules.
3.1 Send events
You can call track to upload events. We recommend setting event properties and the conditions for sending events based on the document you prepared earlier. The following sample code sends an 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, properties: properties)
- 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.
- Key is the name of the property and is of string type. It must start with a letter, can contain digits, letters, and underscores "_", can be up to 50 characters long, and is case-insensitive. AE converts it to lowercase
- Value is the value of the property. Supported types are string, number, Boolean, time, object, object group, and array
User properties have the same requirements as event properties
3.2 Set user properties
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 the username:
DEMO_ACCOUNT_ID = '123'
DEMO_DISTINCT_ID = 'aaa'
user_data = {
array: ["str1", 11, 22.22],
prop_date: Time.now,
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)
3.3 Send data
When you use TDLoggerConsumer, the SDK writes collected data to disk in real time, so you don't need to call the flush() method.
3.4 Shut down the SDK
# Close and exit the SDK
ta.close
Shuts down and exits the SDK. Call this API before you shut down the server to avoid losing data in the cache
4. Best practices
The following sample code includes all of the operations above. We recommend using them in the following order:
ThinkingData::set_stringent(false)
ThinkingData::set_enable_log(false)
consumer = ThinkingData::TDLoggerConsumer.new( 'LOG_DIRECTORY', 'hourly')
ta = ThinkingData::TDAnalytics.new(consumer, my_error_handler, uuid: true)
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)
user_data = {
array: ["str1", 11, 22.22],
prop_date: Time.now,
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)
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)
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)
ta.user_add(distinct_id: DEMO_DISTINCT_ID, properties: {prop_int: 10, prop_double: 15.88})
ta.user_unset(distinct_id: DEMO_DISTINCT_ID, property: [:prop_string, :prop_int])
ta.user_del(distinct_id: DEMO_DISTINCT_ID)

