目录
此内容是否有帮助?

# Java

TIP

Before you begin, please read Preparation before Data Ingestion.

The minimum compatible JDK version is 1.7

Latest version: 2.1.0

Update time: 2022-11-28

Resource download: JAR (opens new window)Source Code (opens new window)

# SDK Integration

1.1 With the Maven integration SDK, place the following dependency information in the pom.xml file (recommended):

<dependencies>
    // others...
    <dependency>
        <groupId>cn.thinkingdata</groupId>
        <artifactId>thinkingdatasdk</artifactId>
        <version>1.9.1</version>
    </dependency>
</dependencies>

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:

ThinkingDataAnalytics te = new ThinkingDataAnalytics(new LoggerConsumer(LOG_DIRECTORY));

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:

Map<String,Object> properties = new HashMap<String,Object>();
// Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
properties.put("#ip", "192.168.1.1");//string
properties.put("channel","te");//string
properties.put("age",1);//number
properties.put("isSuccess",true);//bool
properties.put("birthday",new Date());//datatime
    
Map<String,Object>  object = new HashMap<String,Object>();
object.put("key", "value");
superProperties.put("object",object);//object
        
Map<String,Object> object1 = new HashMap<String,Object>();
object1.put("key", "value");

List<Object>  arr   = new ArrayList<Object>();
arr.put(object1);
properties.put("object_arr",arr);//object group
        
List<String>  arr1    = new ArrayList<String>();
arr1.put("value");
properties.put("arr",arr1);//array

try {
     te.track("account_id","distinct_id","payment",properties);
} catch (Exception e) {
     System.out.println("except:"+e);
}
  • 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
Map<String,Object> userProperties = new HashMap<String,Object>();
userProperties.put("user_name", "TA");
try {
   te.user_set("account_id","distinct_id",userProperties);
} catch (Exception e) {
  System.out.println("except:"+e);
}
 
//UserName is TE
Map<String,Object> newUserProperties = new HashMap<String,Object>();
newUserProperties.put("user_name", "TE");
try {
   te.user_set("account_id","distinct_id",newUserProperties);
} catch (Exception e) {
  System.out.println("except:"+e);
 }

# 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
ThinkingDataAnalytics te = new ThinkingDataAnalytics(new LoggerConsumer(LOG_DIRECTORY));

Map<String,Object> properties = new HashMap<String,Object>();
//Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
properties.put("#ip", "192.168.1.1");//string
properties.put("channel","te");//string
properties.put("age",1);//number
properties.put("isSuccess",true);//bool
properties.put("birthday",new Date());//datetime
    
Map<String,Object>  object = new HashMap<String,Object>();
object.put("key", "value");
superProperties.put("object",object);//object
        
Map<String,Object> object1 = new HashMap<String,Object>();
object1.put("key", "value");

List<Object>  arr   = new ArrayList<Object>();
arr.put(object1);
properties.put("object_arr",arr);//object group
        
List<String>  arr1    = new ArrayList<String>();
arr1.put("value");
properties.put(arr1);//array
try {
     te.track("account_id","distinct_id","payment",properties);
} catch (Exception e) {
     System.out.println("except:"+e);
}

//set user properties
Map<String,Object> userProperties = new HashMap<String,Object>();
userProperties.put("user_name", "TE");
try {
   te.user_set("account_id","distinct_id",userProperties);
} catch (Exception e) {
  System.out.println("except:"+e);
}
//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();