# Node.js
最新バージョン:1.3.6
更新時間:2022-11-10
ダウンロード:Source Code (opens new window)
# SDK 実装
npm
を利用して Node.js SDK を取得します
# install SDK
npm install thinkingdata-node --save
# update SDK
npm i thinkingdata-node@{version}
- Logbus をインストール
TE システムにより迅速かつ正確なデータを転送するために、SDK + LogBus の併用でサーバデータのデータレポートを推奨しております。
# 初期設定
SDK の初期設定のコード例:
var te = ThinkingAnalytics.initWithLoggingMode(LOG_DIRECTORY);
LOG_DIRECTORY
はローカルアクセスファイルのアドレスとなります。LogBus のモニターアドレスをこのアドレスに設定すれば自動でアップロードされます。
pm2 管理ツールを使用している時は、以下を注意してください:
loggingMode を初期設定する時は API の注釈を確認した上、pm2 の関連パラメータを設定してください
/**
* write data to file, it works with LogBus
*
* The Logging Mode uses log4js to save data, you need to report data by LogBus.
*
* if run in pm2 mode, you need set config, and install pm2-intercom: pm2 install pm2-intercom
*
* config(optional) incloud:
* - rotateHourly: false(default) by the day; true by the hour.
* - filePrefix: file prefix
* - pm2: it need set when you run in pm2 mode
* - pm2InstanceVar: 'NODE_APP_INSTANCE' is default.
*
* @param {string} path
* @param {object} config
*/
initWithLoggingMode: function (path, config) {
return _createClient(LoggingConsumer.init(path, config));
},
初期設定フォーマット:
var te = ThinkingAnalytics.initWithLoggingMode(LOG_DIRECTORY, { pm2: true });
# メイン機能
ゲスト ID とアカウント ID をうまく紐付けるために、もしゲーム内でゲスト ID とアカウント ID 両方を使われる場合は、それらの ID を同時にアップロードするのを推奨しております。同時にアップロードしない場合は、ユーザーを重複にカウントされてしまう可能性があります。
# 3.1 イベント送信
track
を利用してイベントの送信を行います。事前にデータプランをご用意の上、送信してください。以下はモデルコードとなります。
var event = {
accountId: "node_test",
distinctId: "node_distinct_id",
event: "test_event",
time: new Date(),
// Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
ip: "202.38.64.1",
properties: {
prop_date: new Date(), // date
prop_double: 134.1, // number
prop_string: "hello world", // string
prop_int: 67,
},
callback(e) {
if (e) {
console.log(e);
}
},
};
te.track(event);
- イベント名称は string タイプで、英文字と数字、 "_"を含め、最大 50 文字
- Key は当プロパティの名称で string タイプで、英文字と数字、 "_"を含め、最大 50 文字。TE システムは一律で小英文字に統一されます
- Value は当プロパティの値で、String, Number, Bloon, Time, object, array, list object を対応しております。
ユーザープロパティはイベントプロパティと一致する必要があります
# 3.2 ユーザープロパティを設定
一般のユーザープロパティに関しては、user_set
を利用して設定することができますが、 UserSet は元の値を上書きされます。本来該当プロパティに値がない場合は、プロパティが新規作成されます。以下はコード例となります。
var userData = {
accountId: "node_test",
distinctId: "node_distinct_id",
properties: {
userName: "TE",
},
callback(e) {
if (e) {
console.log(e);
}
},
};
te.userSet(userData);
# コード例のまとめ
以下のコード例で全ての操作が含まれます、以下の順で利用推奨しております。
//LOG_DIRECTORY is the local folder address
var te = ThinkingAnalytics.initWithLoggingMode(LOG_DIRECTORY);
var event = {
accountId: "node_test",
distinctId: "node_distinct_id",
event: "test_event",
time: new Date(),
//Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
ip: "202.38.64.1",
properties: {
prop_date: new Date(),
prop_double: 134.1,
prop_string: "hello world",
prop_int: 67,
},
callback(e) {
if (e) {
console.log(e);
}
},
};
te.track(event);
var userData = {
accountId: "node_test",
distinctId: "node_distinct_id",
properties: {
prop_date: new Date(),
prop_double: 134.12,
prop_string: "hello",
prop_int: 666,
},
callback(e) {
if (e) {
console.log(e);
}
},
};
te.userSet(userData);
//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();