All Products
Search
Document Center

Simple Log Service:WebTracking JavaScript SDK for mini programs

Last Updated:Jun 16, 2026

The WebTracking JavaScript SDK uploads user logs directly from mini program clients to a Logstore, bypassing your application servers. Use this SDK to collect and analyze user behavior such as app usage, browsing history, and purchase events with minimal server overhead.

Prerequisites

  • WebTracking is enabled for the Logstore. For more information, see Use WebTracking to collect front-end logs.

  • The WebTracking domain is added to the list of allowed request domains in your mini program management console. Mini program platforms require this allowlist for data security. The domain format is https://${project}.${host}, where project and host are the values configured in the SDK.

Limitations

  • Each log upload is limited to 10 MB.

  • Supported platforms: WeChat Mini Program, WeChat Mini Game, Alipay Mini Program, ByteDance Mini Program, DingTalk Mini Program, QQ Mini Program, QQ Mini Game, and Baidu Mini Program.

Step 1: Install and configure the SDK

  1. Install Node.js.

  2. Run the following command to install the SDK dependency:

    npm install --save @aliyun-sls/web-track-mini
  3. Add the following code to configure the SDK:

    Important

    For quick apps, add extra configuration options for the request module before creating the SlsTracker object. The following example uses the requesttask module:

    // Define the quickappSDK object to encapsulate the request API.
    const quickappSDK = {     
          request: requesttask.request
        }
    
    const tracker = new SlsTracker({
          ...opts,
          platformSDK: quickappSDK,
          platformRequestName: 'request',
        })
    import SlsTracker from '@aliyun-sls/web-track-mini'
    
    const opts = {
      host: '${host}', // The endpoint of the region. For example, cn-hangzhou.log.aliyuncs.com.
      project: '${project}', // The name of the project.
      logstore: '${logstore}', // The name of the Logstore.
      time: 10, // The interval for sending logs, in seconds. Default: 10.
      count: 10, // The number of logs to send in each batch. Default: 10.
      topic: 'topic',// A custom log topic.
      source: 'source',
      tags: {
        tags: 'tags',
      },
    }
    
    const tracker = new SlsTracker(opts)  // Create an SlsTracker object.

    Parameter

    Required

    Description

    host

    Yes

    The Simple Log Service endpoint for your region. Example: cn-hangzhou.log.aliyuncs.com. For endpoints in other regions, see Endpoints.

    project

    Yes

    The name of the project.

    logstore

    Yes

    The name of the Logstore.

    time

    No

    The interval, in seconds, at which logs are sent. The default value is 10.

    count

    No

    The number of logs to send in each batch. The default value is 10.

    topic

    No

    The log topic. Specify a custom topic to identify logs.

    source

    No

    The log source. Specify a custom source to identify logs.

    tags

    No

    The log tags. Specify custom tags to identify logs.

Step 2: Upload logs

Pass a single log as an Object or multiple logs as an Array of Objects. The SDK provides the following upload methods:

  • Upload a single log as an Object:

    tracker.send({
      eventType:'view_product',
      productName: 'Tablet',
      price: 500
    })
  • Upload a single log immediately, bypassing the time and count parameters. The type is Object:

    tracker.sendImmediate({
      eventType:'view_product',
      productName: 'Tablet',
      price: 500
    })
  • Upload multiple logs in a batch as an Array:

    tracker.sendBatchLogs([
      {
        eventType: 'view_product',
        productName: 'Tablet',
        price: 500
      },
      {
        eventType: 'view_product',
        productName: 'Laptop',
        price: 1200
      }
    ])
  • Upload multiple logs immediately, bypassing the time and count parameters. The type is Array:

    tracker.sendBatchLogsImmediate([
      {
        eventType:'view_product',
        productName: 'Tablet',
        price: 500
      },
      {
        eventType:'view_product',
        productName: 'Laptop',
        price: 1200
      }
    ])

Step 3: View upload results

After logs are uploaded to the Logstore, create indexes before querying and analyzing them.

Quick view

To preview logs without indexes, go to the Search & Analysis page of your Logstore and click Preview.

Query and analyze logs

  1. Call the Create-Index operation to create a full-text index or a field index. To use SQL statements for queries, create a field index.

  2. Call the GetLogs operation to query log data. The response contains a log array where each element represents a single log entry.

References