# uni-app
TIP
Before data integration, please read Preparation before Data Ingestion
uni-app SDK supports platforms:iOS, Android, Web, Wechat mini-program, Baidu mini-program, Bytedance mini-program, Alipay mini-program, Dingding mini-program, Kuaishou mini-program, QQ mini-program, Jingdong mini-program, 360 mini-program.
Latest version: 3.0.0
Update time: November 10, 2023
Resource download: SDK (opens new window)
# 1. Integrate SDK
Download and decompress uni-app SDK (opens new window)
You can put thinkingdata.uniapp.js
into your project directly, referencing it in the source code:
import TDAnalytics from './static/thinkingdata.uniapp.js'
# 2. Initialization
With the TA SDK introduced, you can use ThinkingAnalyticsAPI in your code:
var config = {
appId: "YOUR_APPID",
serverUrl: "YOUR_SERVER_URL",
autoTrack: {
appLaunch: true, // auto-tracking ta_mp_launch
appShow: true, // auto-tracking ta_mp_show
appHide: true, // auto-tracking ta_mp_hide
pageShow: true, // auto-tracking ta_mp_view
pageShare: true // auto-tracking ta_mp_share
},
};
// initialize
TDAnalytics.init(config)
Instruction on parameters:
appId
: The APPID of your project, which can be obtained on the project management page of the TE.serverUrl
:- If you are using a sass version, please check the receiver URL in project management->data ingestion configuration.
- If you are using a privatized deployment version, please bind the data tracking URL with a domain name and configure it with an HTTPS certificate:
https://bind the domain name with the data tracking URL
autoTrack
:the auto-tracking event types supported include:appLaunch
:auto tracking mini-program launchappShow
:auto tracking mini-program active and resume mini-program from the backgroundappHide
:auto tracking mini-program enter in backgroundpageShow
:auto tracking mini-program page display or enter the foregroundpageShare
:auto tracking mini-program share
WARNING
before reporting data, add the data transmission URL to the request list of the server domain name in the development Settings of wechat public platform or other platforms.
# 3. Common Functions
It is suggested 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 visitor ID would be used as the user's identity identification ID. Note: The Distinct ID would change after the user clear cache or use the APP with a new device.
# 3.1 Login
When the user is logging in, login
could be called to set the account ID of the user. TE platform would use the account ID as the identity identification ID, and the account ID that has been set would be saved before logout
is called. The previous account ID would be covered 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
TDAnalytics.login("TA");
The method will not upload login events
# 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 public event properties.
var superProperties = {
channel : "ta", //character string
age : 1,//number
isSuccess : true,//boolean
birthday : new Date(),//time
object : { key : "value" },//object
object_arr : [ { key : "value" } ],//array object
arr : [ "value" ]//array
};
TDAnalytics.setSuperProperties(superProperties);//set super properties
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 covered.
- 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 with that for super properties
# 3.3 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:
TDAnalytics.track(
eventName: "product_buy",
properties: {
product_name: "tv"
}
);
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.4 User Properties
You can set general user properties by calling user_set
api. The original properties would be covered by the properties uploaded via this api. If no user properties are set before, user properties would 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:
//the username now is TA
TDAnalytics.userSet({
properties: {
username: "TA"
}
});
//the username now is TE
TDAnalytics.userSet({
properties: {
username: "TE"
}
});
# 4. Best Practice
The following sample code covers all the above-mentioned operations. It is recommended that the codes be used in the following steps:
var config = {
appId: "YOUR_APPID",
serverUrl: "YOUR_SERVER_URL",
autoTrack: {
appLaunch: true, // auto-tracking ta_mp_launch
appShow: true, // auto-tracking ta_mg_show
appHide: true, // auto-tracking ta_mp_hide
pageShow: true, // auto-tracking ta_mp_view
pageShare: true // auto-tracking ta_mp_share
}
};
// creating a TA Instance
TDAnalytics.init(config);
// if the user has logged in, the account ID of the user could be set as the unique identifier
TDAnalytics.login("TA");
//set super properties
var superProperties = {
channel : "ta", //character string
age : 1,//number
isSuccess : true,//boolean
birthday : new Date(),//time
object : { key : "value" },//object
object_arr : [ { key : "value" } ],//array object
arr : [ "value" ]//array
};
TDAnalytics.setSuperProperties(superProperties);
//send event
TDAnalytics.track({
eventName: "product_buy",
properties: {
product_name: "tv"
}
);
//Set user properties
TDAnalytics.userSet({
properties: {
username: "TE"
}
});