menu
Is this helpful?

# Lua

TIP

Before you begin, please read Preparation before Data Ingestion.

Latest version: v2.0.0

Update time: 2023-12-01

Resource download: Source Code (opens new window)

Notice

Current documentation applies to v2.0.0 and later. For historical versions, see Data Ingestion Guide - Lua (V1) (opens new window)

# SDK Integration

1.1 You can download the SDK source code (opens new window) from GitHub and integrate it into your project. Just put ThinkingDataSdk.lua in the directory of your project.

Use the luarocks management tool to install third-party libraries:

luarocks install uuid
 
# To install the luasec library, specify the OPENSSL_DIR path
luarocks install luasec OPENSSL_DIR=[PATH]

luarocks install lua-cjson

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:

local tdAnalytics = require "ThinkingDataSdk"

local consumer = tdAnalytics.TDLogConsumer("LOG_DIRECTORY", tdAnalytics.LOG_RULE.HOUR, 200, 500)
local sdk = tdAnalytics(consumer)

LOG_DIRECTORY is the local folder path.

# 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.

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

local distinctId = "ABCDEFG123456789"
local accountId = "TE_10001"
local properties = {}
properties["#time"] = os.date("%Y-%m-%d %H:%M:%S")
properties["#ip"] = "192.168.1.1"
properties["Product_Name"] = "card"
properties["Price"] = 30
properties["OrderId"] = "abc_123"

sdk:track(accountId, distinctId, "payment", properties)
  • 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 userSet 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:

local distinctId = "ABCDEFG123456789"
local accountId = "TE_10001"

local userSetProperties = {}
userSetProperties["user_name"] = "ABC"
sdk:userSet(accountId, distinctId, userSetProperties)
userSetProperties = {}
userSetProperties["user_name"] = "abc"

sdk:userSet(accountId, distinctId, userSetProperties)

# Best Practice

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

local tdAnalytics = require "ThinkingDataSdk"

local consumer = tdAnalytics.TDLogConsumer("LOG_DIRECTORY", tdAnalytics.LOG_RULE.HOUR, 200, 500)
local sdk = tdAnalytics(consumer)

local distinctId = "ABCDEFG123456789"
local accountId = "TE_10001"

local properties = {}
properties["#time"] = os.date("%Y-%m-%d %H:%M:%S")
properties["Product_Name"] = "card"
properties["Price"] = 30
properties["OrderId"] = "abc_123"
sdk:track(accountId, distinctId, "payment", properties)

local userSetProperties = {}
userSetProperties["user_name"] = "ABC"
sdk:userSet(accountId, distinctId, userSetProperties)

userSetProperties = {}
userSetProperties["user_name"] = "abc"
sdk:userSet(accountId, distinctId, userSetProperties)

sdk:flush()