# Java
::: Tips
実装する前にデータアクセスの準備を確認しておいてください。
Java SDK は JDK1.7ver 以上対応しております。
:::
最新バージョン:2.1.1
更新時間:2022-11-15
ダウンロード: JAR (opens new window)、Source Code (opens new window)
# SDK 実装
- Maven を利用して SDK 実装し、
pom.xml
ファイルに以下の依頼情報を記入してください
<dependencies>
// others...
<dependency>
<groupId>cn.thinkingdata</groupId>
<artifactId>thinkingdatasdk</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
- Logbus をインストール
TE システムにより迅速かつ正確なデータを転送するために、SDK + LogBus の併用でサーバデータのデータレポートを推奨しております。
# 初期設定
以下は SDK の初期設定のフォーマットコードとなります:
ThinkingDataAnalytics te = new ThinkingDataAnalytics(new LoggerConsumer(LOG_DIRECTORY));
LOG_DIRECTORY
はローカルアクセスファイルのアドレスとなります。
# メイン機能
ゲスト ID とアカウント ID をうまく紐付けるために、もしゲーム内でゲスト ID とアカウント ID 両方を使われる場合は、それらの ID を同時にアップロードするのを推奨しております。同時にアップロードしない場合は、ユーザーを重複にカウントされてしまう可能性があります。
# 3.1 イベント送信
track
を利用してイベントの送信を行います。事前にデータプランをご用意の上、送信してください。以下はモデルコードとなります。
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);
}
- イベント名称は string タイプで、英文字と数字、 "_"を含め、最大 50 文字
- Key は当プロパティの名称で string タイプで、英文字と数字、 "_"を含め、最大 50 文字。TE システムは一律で小英文字に統一されます
- Value は当プロパティの値で、String, Number, Bloon, Time, object, array, list object を対応しております。
ユーザープロパティはイベントプロパティと一致する必要があります
# 3.2 ユーザープロパティを設定
一般のユーザープロパティに関しては、user_set
を利用して設定することができますが、 UserSet は元の値を上書きされます。本来該当プロパティに値がない場合は、プロパティが新規作成されます。以下はコード例となります。
//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);
}
# コード例のまとめ
以下のコード例で全ての操作が含まれます、以下の順で利用推奨しております。
//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();