Updated on 2025-09-07 GMT+08:00

LTS WeChat Applet SDK

If you need to collect and analyze user information on WeChat, such as browser, browsing behavior, device, device exception, and network usage records, you can use this SDK to report logs to LTS.

To use this SDK, enable the anonymous write function of log streams. After this function is enabled, the reported logs are not authenticated and may generate dirty data. For details, see Managing Log Streams.

The WeChat SDK supports cross-cloud and local log reporting for whitelisted users in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. To use it, create a service ticket.

Transport Protocol

HTTPS

Prerequisites

  • You have obtained the name of the region where LTS is deployed.
  • You have obtained a project ID of your Huawei Cloud account. For details, see API Credentials.
  • You have obtained the IDs of the target log group and log stream in LTS.

Version Updates

Table 1 Version updates

Version

Description

1.0.24

Added support for regions CN East-Shanghai1 and CN South-Guangzhou.

1.0.23

Supported quick app reporting.

1.0.21

  • Deprecated the config method. Use the new SDK to create an instance.
  • Removed the dependency on third-party packages and Chinese characters in the code.

1.0.19

Changed the time threshold range from 1–60 to 1–1800, and changed the default value from 30 to 3.

1.0.18

  • Adjusted log levels.
  • Supported labels nesting.

1.0.15

Supported reporting logs to LTS through multiple log streams.

Instructions

To ensure that WeChat logs can be reported, add the reporting address to the domain name list of the WeChat developer platform. For details about the reporting address, see Table 2.

Installing the SDK

Run the npm i lts-mini-sdk command in the root directory of the applet to install the SDK.

You can download the latest SDK from the open-source repository. You can copy the app.js file in the SDK package to the project file directory or enable npm in the project to use the files in node_module.

Integrating the SDK

  • Integrating the SDK using npm
    1. Ensure that the project has the package.json file. If it does not exist, run the npm init command in the root directory of the project to create one.
    2. Run the SDK installation command to install the SDK software package.
    3. Use the developer tool to build the npm library file of the current project.
  • Integrating the SDK by importing files
    1. Run the SDK installation command to install the SDK software package.
    2. Find the app.js file (node_modules > lts-mini-sdk > app.js) in the SDK folder, copy it from node_module to the root directory, and rename it.

Sample Code

const miniSDK = require('lts-mini-sdk').default;
// import miniSDK from 'lts-mini-sdk';
App({
    onLaunch(options) {
        // Initialization
        const wxmini = new miniSDK({
            // Reporting region
            region: string,
            // Project ID
            projectId: string,
            // Log reporting address
            url: string,  
           // LTS log group ID
            groupId: string,
           // LTS log stream ID
            streamId: string,
            // Debug log level
            debug: string,
            // Platform of the current applet
            platform: string,
            // Records threshold
            cacheThreshold: number,
            // Time interval threshold
            timeInterval: number,
        });
        // Immediately report a single record, with labels
        wxmini.reportImmediately({ 'name': 'xiaoming', 'age': 18 }, { 'key': 'value' });
        // Immediately report a single record, without labels
        wxmini.reportImmediately([{   key: 'value',   number: 1,   array: [],   json: {     json: 'json'   } }, { 'key': 'value' }]);
        // Cache and report multiple records, with labels
        wxmini.report([{ 'name': 'xiaohong', 'age': 18 }, { 'name': 'xiaobai', 'age': 20 }], { 'key': 'value' });
        // Cache and report multiple records, without labels
        wxmini.report([{ 'name': 'xiaohong', 'age': 18 }, {   key: 'value',   number: 1,   array: [],   json: {     json: 'json'   } }]);
        // Cache and report multiple records, with multiple labels (max. 50)
        wxmini.report([{ 'name': 'xiaohong', 'name': 'xiaolan' }], {'version': '1.0.0', 'render': 'mini', 'link': '/', from: 'wx'});
    }
});

Parameters

  • Parameters
    Table 2 Parameters

    Field

    Type

    Mandatory

    Default Value

    Description

    region

    String

    Yes

    -

    Region where LTS is located.

    projectId

    String

    Yes

    -

    Project ID of the account. The value contains a maximum of 128 characters.

    groupId

    String

    Yes

    -

    Log group ID. The value contains a maximum of 128 characters.

    streamId

    String

    Yes

    -

    Log stream ID. The value contains a maximum of 128 characters.

    url

    String

    No

    -

    Public network domain name for reporting. The value can contain a port number, for example, https://lts-access.cn-north-4.myhuaweicloud.com:443. If no URL is set, a link is automatically generated based on the region. The format is as follows: https://lts-access.{region}.myhuaweicloud.com. The domain name must be valid on the WeChat applet background (for details, see the server domain name setting on the applet official website). The value contains a maximum of 128 characters.

    • When the SDK is used to report logs across clouds or locally, port 443 is used.
    • When Huawei Cloud hosts report logs via the Huawei Cloud intranet, port 8102 is used.

    debug

    String

    No

    OFF

    Output level of the console debugging information. The options are OFF, ERROR, WARN, INFO, and DEBUG, in ascending order. The highest level is DEBUG. If an invalid value is entered, the default value OFF is used. If this parameter is set to true, DEBUG logs are enabled. If this parameter is set to false, DEBUG logs are disabled.

    cacheThreshold

    int

    No

    30

    By default, the cache threshold is 30 records. When the number of cached records reaches the threshold (range: 30 to 1,000), cached records are reported.

    timeInterval

    number

    No

    3

    By default, records are reported every 3 seconds. When the threshold interval (range: 1 to 1,800) is reached, cached records are reported.

    platform

    String

    No

    baidu

    Platform that supports data reporting. Currently, the following platforms are supported: baidu (Baidu applet), wx (WeChat applet), dd (DingTalk applet), my (Alipay applet), and quick (quick app).

  • Parameters of the report method for reporting logs
    Table 3 report parameters

    Field

    Type

    Mandatory

    Default Value

    Description

    content

    Object | Array<Object>

    Yes

    -

    Object or object array to be reported. Each object can contain a maximum of 300 key-value pairs. The maximum length of the JSON string is 1024 × 30 characters. Excess characters will be truncated.

    Each object in the array can contain a maximum of 300 key-value pairs. The maximum length of the JSON string is 1024 × 30 bytes. Excess characters will be truncated.

    The log field name cannot contain double underscores (__).

    labels

    Object

    No

    None

    Log labels. The outermost key-value pair can contain a maximum of 50 key-value pairs. The outermost key can contain a maximum of 64 characters including letters, digits, and underscores (_), and must start with a letter. The maximum length of the converted JSON string is 1024 × 30 characters. If the length exceeds the limit, the record will not be reported.

    The log field name cannot contain double underscores (__).

  • Parameters of the reportImmediately method for reporting logs immediately
    Table 4 reportImmediately parameters

    Field

    Type

    Mandatory

    Default Value

    Description

    content

    Object | Array<Object>

    No

    -

    Object or object array to be reported immediately. Each object can contain a maximum of 300 key-value pairs. The maximum length of the JSON string is 1024 × 30 characters. Excess characters will be truncated.

    Each object in the array can contain a maximum of 300 key-value pairs. The maximum length of the JSON string is 1024 × 30 bytes. Excess characters will be truncated. If this parameter is left empty, cached logs will be reported immediately.

    The log field name cannot contain double underscores (__).

    labels

    Object

    No

    None

    Log labels. The outermost key-value pair can contain a maximum of 50 key-value pairs. The outermost key can contain a maximum of 64 characters including letters, digits, and underscores (_), and must start with a letter. The maximum length of the converted JSON string is 1024 × 30 characters. If the length exceeds the limit, the record will not be reported.

    The log field name cannot contain double underscores (__).

Obtaining Parameters

  • Regions
    Table 5 Regions

    Name

    Region

    CN North-Beijing4

    cn-north-4

    CN East-Shanghai1

    cn-east-3

    CN South-Guangzhou

    cn-south-1

  • To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
  • To obtain a log stream's ID, click next to the corresponding log group and hover over the target log stream's name.