目录
此内容是否有帮助?

# Golang

TIP

Before you begin, please read Preparation before Data Ingestion.

Latest version: 1.6.8

Update time: 2023-02-23

Resource download: Source Code (opens new window)

# SDK Integration

1.1 Execute the following command to get the latest version of Golang SDK:

# install SDK
go get github.com/ThinkingDataAnalytics/go-sdk/thinkingdata

# update SDK
go get -u github.com/ThinkingDataAnalytics/go-sdk/thinkingdata

1.2 used Module

//Introduce thinkingdata at the beginning of the code file
import	"github.com/ThinkingDataAnalytics/go-sdk/thinkingdata"

# Pull out the latest SDK module
go mod tidy

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

config := thinkingdata.LogConfig{
   Directory: LOG_DIRECTORY
}
// create logConsumer
consumer, _ := thinkingdata.NewLogConsumerWithConfig(config)
// init TE instance
te := thinkingdata.New(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 (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:

accountId := "te_account_id"
distinctId := "te_distinct_id"
properties := map[string]interface{}{
   // Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
   "#ip":       "123.123.123.123",
   "channel":   "te",       // string
   "age":       1,          // number
   "is_success": true,      // bool
   "birthday":  time.Now(), // time
   // object
   "object": map[string]interface{}{
      "key": "value",
   }, 
   // array object
   "objectArr": []interface{}{
      map[string]interface{}{
         "key": "value",
      },
   },
   "arr":     []string{"value"}, // array
}

err := te.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 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:

// username is TA
err := te.UserSet("accountId", "distinctId", map[string]interface{}{
   "user_name": "TA",
})
// username is TE
err = te.UserSet("accountId", "distinctId", map[string]interface{}{
   "user_name": "TE",
})

# Best Practice

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

//LOG_DIRECTORY is the local folder address
config := thinkingdata.LogConfig{
   Directory: LOG_DIRECTORY,
}
// init consumer
consumer, _ := thinkingdata.NewLogConsumerWithConfig(config)

te := thinkingdata.New(consumer)

accountId := "te_account_id"
distinctId := "te_distinct_id"
properties := map[string]interface{}{
   //Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
   "#ip":       "123.123.123.123",
   "channel":   "te",       // string
   "age":       1,          // number
   "isSuccess": true,       // bool
   "birthday":  time.Now(), // time
   // object
   "object": map[string]interface{}{
      "key": "value",
   }, 
   // array object
   "objectArr": []interface{}{
      map[string]interface{}{
         "key": "value",
      },
   },
   "arr":     []string{"value"}, // array
}
err := te.Track(accountId, distinctId, "payment", properties)

// set user properties
err := te.UserSet("accountId", "distinctId", map[string]interface{}{
   "user_name": "TE",
})

//Calling the flush API will immediately write the data to the file. 
//In the production environment, pay attention to avoid IO or network overhead caused by frequent calls to flush
te.Flush()