Web&H5

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

KeyDefinitionDescriptionSample code
partnerPartner codePartner, please contact TrustDecision to obtainoptions['partner'] = 'Please enter Your partner code';
appKeyApp keyAppkey, please offer your App bundleId for TrustDecision to obtain.appkey creation requires the user to provide the application bundleIdoptions['appKey'] = 'Please enter Your appKey';
countryCountry codeCountry/region parameters, such as cn sg us fra idnaoptions['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

KeyDefinitionDescriptionSample code
behaviorHttpTimeOutTime out for all network requests within TDBehavior SDKUnit: seconds, floatDefault: 15.0options['behaviorHttpTimeOut'] = 15.0;

4.Return Codes

Error CodeDescriptionSolution
0SuccessNo action needed
100initWithOptions not calledinitWithOptions not called, need to call initWithOptions
101Network authentication result not returned due to low network speedThrow an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again
102Network error in authentication interface due to system reasons, such as no network connectionThrow an error to the user: "Network failure, please check the network and try again", guiding the user to call the collect method again
103Authentication interface returns status code is not 200Check 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
104No data in SDK internal containerThere 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!
109No data in SDK internal containerThere is no valid collect data,Switches are all NO,Please contact TrustDecision!
110No data in SDK internal containerThere is no valid collect data,Please call the start method first!