目录
此内容是否有帮助?

# Erlang

TIP

Before you begin, please read Preparation before Data Ingestion.

Latest version: 1.2.4

Update time: 2022-11-17

Resource download: Source Code (opens new window)

# SDK Integration

# 1.1 rebar3 (SDK version ≥ v1.2.0)

(1) rebar3 is installed.

(2) modify rebar.config file, add dependency of thinkingdata_analytics SDK

{erlopts, [debuginfo, 
    %% it is necessary when use "lager"
    {parsetransform, lagertransform}
]}.

{deps, [ 
    %% add thinkingdata_analytics SDK 
    {thinkingdata_analytics, {git, "https://github.com/ThinkingDataAnalytics/erlang-sdk.git", {tag, "v1.2.4"}}}
]}.

{shell, [ 
    %% Specify configuration file
    {config, "config/sys.config"},
    {apps, [app_name]}
]}.

Execute command:

rebar3 compile

(3) modify configuration file of your project,add a sink that belongs to thinkingdata_analytics SDK .

[
  %% lager configuration
  {lager, [
    {colored, true},
    {log_root, "./log"}, %% system log path
    %% add a sink that belongs to thinkingdata_analytics SDK, name is "ta_logger_lager_event"
    {extra_sinks,
      [
        {ta_logger_lager_event,
          [{handlers, [
            {lager_file_backend, [
              {file, "LOG_DIRECTORY"}, %% TE log file path
              {level, info},
              {formatter, lager_default_formatter},
              {formatter_config, [message, "\n"]},
              {size, 10485760}, %% log file size limit, default is 10Mb
              {rotator, ta_lager_rotator} %% custom rotator of TE log file 
            ]}]},
            {async_threshold, 500},
            {async_threshold_window, 50}
          ]
        }]
    }
  ]
  }
].

There is an example in the SDK directory: example_sys.config

LOG_DIRECTORY is the local folder path.

(4) modify configuration of your app. Add launch items of thinkingdata_analytics SDK in xxxx.app.src.

{application, your_name,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, {your_name_app, []}},
  {applications,
   [kernel,
    stdlib,
    thinkingdata_analytics %% add thinkingdata_analytics launch item
   ]},
  {env,[]},
  {modules, []},

  {licenses, ["Apache-2.0"]},
  {links, []}
 ]}.

# 1.2 Logbus Integration

We recommend using SDK+LogBus to track and report data on server. You can refer to the following documents to complete the installation of Logbus: LogBus User Guide

# Initialization

The following is the sample code for SDK initialization:

%% pre-init SDK
ta_consumer_log:init(),

%% init SDK with consumer
thinking_analytics_sdk:init(thinking_analytics_sdk:consumer_type_log()).

# Common Features

In order to ensure that the distinct ID and account ID can be bound smoothly, if your game uses the distinct ID and account ID, we strongly recommend that you upload these two IDs at the same time, otherwise the account will not match, causing users to double count. For specific ID binding rules, please refer to the chapter on User identification rules (opens new window).

# 3.1 Sending 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:

thinking_analytics_sdk:track("account_id", "distinct_id", "EventName", #{"#ip" => "123.123.123.123", "#time" => os:timestamp()}).
  • Key is the name of the property and refers to the string type. It must start with a character, and contain numbers, characters (insensitive to case, and upper cases would be transformed into lower cases by TE) and underscores "_", with a maximum length of 50 characters.
  • Value, the value of the property, supports string, numbers, Boolean, time, object, array object, and array

The requirements for event properties and user properties are the same as that for super properties

# 3.2 User Properties

You can set general user properties by calling user_set API. The original properties would be replaced by the properties uploaded via this API. If no user properties are set before, user properties will be newly created. The type of newly-created user properties must conform to that of the uploaded properties. User name setting is taken as the example here:

thinking_analytics_sdk:user_set("account_id", "distinct_id", #{"age" => 18, "abc" => ["a", "b", "c"]}),

# Best Practice

The following sample code covers all the above-mentioned operations. It is recommended that the codes be used in the following steps:

%% pre-init SDK
ta_consumer_log:init(),

%% init SDK with consumer
thinking_analytics_sdk:init(thinking_analytics_sdk:consumer_type_log()),

%% track event
thinking_analytics_sdk:track("account_id_Erlang", "distinct_logbus", "ViewProduct", #{"key_1" => "value_1", "key_2" => "value_2"}),

%% track event whit object model nesting
thinking_analytics_sdk:track("account_id_Erlang", "distinct_logbus", "ViewProduct", #{"custom_property_1" => [#{"key_1" => "value_1"}, #{"key_2" => "value_2"}, #{"key_3_list" => ["a", "b", #{"child_key" => "child_value"}]}]}),

%% time is must use by "ta_utils:format_time()"
thinking_analytics_sdk:track("account_id_Erlang", "distinct_logbus", "ViewProduct", #{"register_time" => ta_utils:format_time(os:timestamp())}),

thinking_analytics_sdk:close().