目录
此内容是否有帮助?

# Node.js

最新バージョン:1.3.6

更新時間:2022-11-10

ダウンロード:Source Code (opens new window)

# SDK実装

  1. npmを利用してNode.js SDKを取得します
# install SDK
npm install thinkingdata-node --save

# update SDK
npm i thinkingdata-node@{version}
  1. 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();