1. Ruby SDK User Guide

Before using, please read the Data Model introduction.

2. Sensors Analytics SDK Integration

Integrate the  Sensors Analytics SDK in Ruby scripts to collect and analyze user data.

We recommend using RubyGem to manage Ruby projects and obtain Sensors Analytics SDK:

gem install sensors_analytics_sdk
CODE

If you don't use RubyGem, you can also download the source code of Sensors Analytics SDK from GitHub.

2.1. Initialize Sensors Analytics SDK

2.1.1. Get Configuration Information

Obtain the data receiving URL as shown in the following figure:

2.1.2. Initialize SDK in the Program

Construct an instance of Sensors Analytics SDK in the initialization code section of the program:

Ruby

require 'sensors_analytics_sdk.rb' # 从神策分析配置页面中获取的数据接收的 URL SA_SERVER_URL = 'YOUR_SERVER_URL' # 初始化一个 Consumer,用于数据发送 consumer = SensorsAnalytics::DefaultConsumer.new(SA_SERVER_URL) sa = SensorsAnalytics::SensorsAnalytics.new(consumer) # 记录用户登录事件 distinct_id = 'ABCDEF123456' sa.track(distinct_id, 'UserLogin')
CODE

In the code above,  YOUR_SERVER_URL is the data receiving URL obtained from Sensors Analytics. The user program should always hold this instance until the program ends. Before the program exits, the  close() method should be used to explicitly close it, otherwise some cached data may be lost.

Now we can use Sensors Analytics SDK normally. For more information on the use of the SDK, you can jump to the section on controlling Sensors Analytics SDK at the end of this article.

3. Set up Sensors Analytics SDK

Ruby SDK is mainly composed of the following two components:

  • SensorsAnalytics: Interface object used to send data, the constructor requires a Consumer instance
  • Consumer: Performs actual data sending

For more flexible data access by developers, Sensors Analytics SDK supports the following Consumers:

  • DebugConsumer
  • DefaultConsumer
  • BatchConsumer

3.1. DebugConsumer (for testing purposes)

To verify the correctness of data import, please refer to the Debug 调试模式 for more information. Please note: Debug mode is a mode set up for convenient debugging by developers. This mode will verify the data one by one and throw an exception when the verification fails. Its performance is much lower than that of the normal mode. Using Debug mode in the production environment will severely affect performance and may cause crashes. Be sure to replace/disable Debug mode before the product goes live..

  • The initialization interface is
# server_url 是接收端的 url # writeData表示是否校验数据 # writeData为 # true - 校验数据,并将数据导入到神策分析中 # false - 校验数据,但不进行数据导入 def initialize(server_url, write_data) end 
CODE
  • Example
require 'sensors_analytics_sdk.rb' # 从神策分析配置页面中获取的数据接收的 URL SA_SERVER_URL = 'YOUR_SERVER_URL' # Debug 模式下,是否将数据导入神策分析 # true - 校验数据,并将数据导入到神策分析中 # false - 校验数据,但不进行数据导入 SA_DEBUG_WRITE_DATA = true # 初始化 Debug Consumer consumer = SensorsAnalytics::DebugConsumer.new(SA_SERVER_URL, SA_DEBUG_WRITE_DATA) sa = SensorsAnalytics::SensorsAnalytics.new(consumer)
CODE

3.2. DefaultConsumer

Usually used in scenarios where small-scale historical data needs to be imported. Since the data is sent directly over the network, if there is a network anomaly, it may result in data retransmission or loss. Therefore, do not use it in any online services. A regular consumer that sends data synchronously, line by line, to the receiving server.

  • The initialization interface
# server_url 是接收端的 url def initialize(server_url)   end
CODE
  • Example
require 'sensors_analytics_sdk.rb' # 从神策分析配置页面中获取的数据接收的 URL SA_SERVER_URL = 'YOUR_SERVER_URL' # 初始化一个 Consumer,用于数据发送 consumer = SensorsAnalytics::DefaultConsumer.new(SA_SERVER_URL) sa = SensorsAnalytics::SensorsAnalytics.new(consumer)
CODE

3.3. BatchConsumer

Usually used in scenarios where small-scale historical data needs to be imported, or for offline/side-path data import. Since the data is sent directly over the network, if there is a network anomaly, it may result in data retransmission or loss. Therefore, do not use it in any online services. A consumer that sends data in batches, only when the data reaches a specified amount.

  • The initialization interface
# server_url 是接收端的 url # flush_bulk 最大缓存条数 def initialize(server_url, flush_bulk = MAX_FLUSH_BULK) end
CODE
  • Example
require 'sensors_analytics_sdk.rb' # 从神策分析配置页面中获取的数据接收的 URL SA_SERVER_URL = 'YOUR_SERVER_URL' # 当缓存的数据量达到参数值时,批量发送数据 SA_BULK_SIZE = 100 # 初始化 Batch Consumer consumer = SensorsAnalytics::BatchConsumer.new(SA_SERVER_URL, SA_BULK_SIZE) sa = SensorsAnalytics::SensorsAnalytics.new(consumer) # 程序结束前调用 flush() ,通知 Consumer 发送所有缓存数据 consumer.flush()
CODE

4. API Interface

For the usage documentation of the API interface, please refer to the Basic API Usage Documentation.