1.Import TDBehavior
version:1.4.2
Embed the following code in the head tag of the key business entry page
<head>
<script>
(function() {
var script = document.createElement('script')
script.src = 'https://static.trustdecision.com/tdbh/tdbehavior.min.js?t=' + (new Date().getTime()/3600000).toFixed(0)
document.head.appendChild(script)
})()
</script>
...
</head>
2.Initialization
Interface Definition
function initWithOptions(options)
initWithOptions method Required Configuration
| Key | Definition | Description | Sample code |
|---|---|---|---|
| partner | Partner code | Partner, please contact TrustDecision to obtain | options['partner'] = 'Please enter Your partner code'; |
| appKey | App key | Appkey, please offer your App bundleId for TrustDecision to obtain.appkey creation requires the user to provide the application bundleId | options['appKey'] = 'Please enter Your appKey'; |
| country | Country code | Country/region parameters, such as cn sg us fra idna | options['country'] = 'Please enter Your country code'; |
Sample Code
//init
const options = {}
//Partner code, Refer to `Required Configuration`
options['partner'] = '[Your partner]';
//App key, Refer to `Required Configuration`
options['appKey'] = '[Your appKey]';
//Country code, Refer to `Required Configuration`
options['country'] = '[Your country code]';
TDBehavior.initWithOptions(options);
3.Call behavior collect methods
Behavior collect methods are used to obtain user behavior data. The interface definition is as follows:
/*** TDBehavior start, start behavior collect, write collected data into SDK internal container
*/
TDBehavior.start()
/*** TDBehavior stop, end behavior collect, stop writing collected data into SDK internal container and clear the data in SDK internal container;
*/
TDBehavior.stop()
/*** TDBehavior collect, return behavior collect result, if data exists in SDK internal container, it will return success code == 0, and clear the data in SDK internal container;
* If no data exists in SDK internal container, it will return code == 104 error code
*/
const result = await TDBehavior.collect()
Sample Code
⚠️⚠️⚠️ Ensure that there is user behavior after calling start in the business interface, such as text input, click, swipe, etc. Otherwise, calling collect will return a 104 error code, indicating that no behavior data was collected;
// When entering business interface, call start method to perform behavior collect, for example when entering login interface
TDBehavior.start()
...
// When calling business interface, call collect method to get behavior collect data, for example when clicking login button
const result = await TDBehavior.collect();
const code = result['code'];
const msg = result['msg'];
const payload = result['payload'];
if (code === 0) {
console.log('Get behavior collect result success,payload: ' + payload);
} else {
console.log('Get behavior collect result failed,code: ' + code + ', msg: ' + msg);
}
...
// When leaving business interface, call stop method to end behavior collect, for example when leaving login interface
TDBehavior.stop()
Specify input fields that do not require collected
Set the attribute data-tdbehavior-exkey including "TDBehaviorExInput" in input and textarea fields. After this setting, the text input events of input and textarea fields will no longer be collected
<input data-tdbehavior-exkey="TDBehaviorExInput" placeholder="Current input field does not collect data"></input>
<textarea data-tdbehavior-exkey="TDBehaviorExInput" placeholder="Current input box does not collect data"></textarea>
initWithOptions method Optional Configuration
| Key | Definition | Description | Sample code |
|---|---|---|---|
| behaviorHttpTimeOut | Time out for all network requests within TDBehavior SDK | Unit: seconds, floatDefault: 15.0 | options['behaviorHttpTimeOut'] = 15.0; |
4.Return Codes
| Error Code | Description | Solution |
|---|---|---|
| 0 | Success | No action needed |
| 100 | initWithOptions not called | initWithOptions not called, need to call initWithOptions |
| 101 | Network authentication result not returned due to low network speed | Throw an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again |
| 102 | Network error in authentication interface due to system reasons, such as no network connection | Throw an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again |
| 103 | Authentication interface returns status code is not 200 | Check if partner parameters are correct. If still fails, provide error message resultStruct.msg to our operations team to confirm if parameters are correct and within validity period |
| 104 | No data in SDK internal container | There is no valid collect data,Please Ensure that there is user behavior after calling start in the business interface, such as text input, click, swipe, etc! |
| 109 | No data in SDK internal container | There is no valid collect data,Switches are all NO,Please contact TrustDecision! |
| 110 | No data in SDK internal container | There is no valid collect data,Please call the start method first! |
