目录
此内容是否有帮助?

# CocosCreator

::: Tips

Before you begin, please read Preparations before Data Ingestion

CocosCreator SDK support Android, iOS, Web, Wechat Mini Game, Alipay Mini Game, Bytedance Mini Game, OPPO Quick Game, Huawei Quick Game, VIVO Quick Game, Xiaomi Quick Game, Facebook Mini Game, Google Play Instant Game.

:::

Latest version : 2.2.0

Update time: 2022-08-15

Resource download: SDK (opens new window), Source Code (opens new window)

# 1. SDK Integration

Download and unzip CocosCreator SDK (opens new window)

# 2. Initialization

Importing ThinkingAnalyticsAPI, then you can use CocosCreator SDK:

// config SDK infomation
var config = {
  appId: "YOUR_APPID", // project APP ID
  serverUrl: "YOUR_SERVER_URL", // receiver URL
  autoTrack: { // auto-tracking
    appShow: true, // open APP
    appHide: true // close APP
  }
};
// creat SDK instance object
var te = new ThinkingAnalyticsAPI(config);
// SDK initialization
te.init();

Parameters:

  • APPID: The APPID of your project, which can be found on the project management page of TE.
  • SERVER_URL:
    • If you are using a SaaS version, please check the receiver URL on this page

    • If you use the private deployment version, you can customize the data tracking URL .

Since Android 9.0+ restricts HTTP requests by default, please use HTTPS protocol only.

  • autoTrack: Optional, enable auto-tracking
    • appShow: Enable auto-tracking APP open events
    • appHide: Enable auto-tracking APP open events

Note: Before reporting the data, please add the data receiving URL to the request list of the server domain name in the development settings of the WeChat public platform or other platforms.

# 3. Common Features

We suggest that you read User Identification Rules before using common functions; SDK would generate a random number that would be used as the distinct ID, and save the ID locally. Before the user logs in, the distinct ID would be used as the identification ID. Note: The distinct ID would change after the user reinstalled the App or used the APP with a new device.

# 3.1 Login

When the users log in , login could be called to set the account ID of the user. TE platform would use the account ID as the identification ID, and this ID would be saved before logout is called. The previous account ID would be replaced if login has been called multiple times.

// The login unique identifier of the user, corresponding to the #account_id in data tracking. #Account_id now is TE
te.login("TE");

Note: Login events wouldn't be uploaded in this method.

# 3.2 Super Properties

Super properties refer to properties that each event might have. You can call setSuperProperties to set super properties. It is recommended that you set super properties first before sending data. Some important properties (e.g., the membership class of users, source channels, etc.) should be set in each event. At this time, you can set these properties as super properties.

var superProperties = {
    channel : "te", //string
    age : 1,//number
    isSuccess : true,//boolean
    birthday :  new Date(),//time
    object : { key : "value" },//object
    object_arr : [ { key : "value" } ],//object array
    arr : [ "value" ]//array
};
// set super properties
te.setSuperProperties(superProperties);

Super properties would be saved in local storage, and will not need to be called every time the App is opened. If the super properties set previously are uploaded after calling setSuperProperties, previous properties would be replaced.

  • 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

Note: The requirements for event properties and user properties are the same with that for super properties

# 3.4 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:

te.track(
  "product_buy",
  {product_name: "product name"}
);

The event name is string type. It could only start with a character and could contain figures, characters, and an underline "_", with a maximum length of 50 characters.

# 3.5 User Properties

You can set general user properties by calling userSet API. The original properties would be replaced by the properties uploaded via this API. The data type of newly-created user properties must be the same as the uploaded properties. User name setting is taken as the example here:

//current username is TA
te.userSet({ username: "TA" });
//current username is TE
te.userSet({ username: "TE" });

# 4. Best Practice

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

var config = {
  appId: "YOUR_APPID",
  serverUrl: "YOUR_SERVER_URL",
  autoTrack: {
    appShow: true,
    appHide: true
  }
};
// creat TE instance
var te = new ThinkingAnalyticsAPI(config);
//if the user has logged in, the account ID of the user could be set as the unique identifier 
te.login("TE");
//After setting super properties, each event would have super properties
var superProperties = {
    channel : "te", //字符串
    age : 1,//数字
    isSuccess : true,//布尔
    birthday :  new Date(),//对象
    object : { key : "value" },//对象
    object_arr : [ { key : "value" } ],//对象组
    arr : [ "value" ]//数组
};
te.setSuperProperties(superProperties);
// initialization
te.init();
// upload events
te.track(
  "product_buy",
  {product_name: "product name"} 
);
// set user properties
te.userSet({ username: "TE" });

#

#

#