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}, whereprojectandhostare 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
-
Install Node.js.
-
Run the following command to install the SDK dependency:
npm install --save @aliyun-sls/web-track-mini -
Add the following code to configure the SDK:
ImportantFor quick apps, add extra configuration options for the request module before creating the SlsTracker object. The following example uses the
requesttaskmodule:// 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
timeandcountparameters. The type isObject: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
timeandcountparameters. The type isArray: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
-
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.
-
Call the GetLogs operation to query log data. The response contains a log array where each element represents a single log entry.
References
-
If an API call returns an error, see API error codes for troubleshooting.
-
Simple Log Service also provides Alibaba Cloud SDKs. For details, see Simple Log Service in SDK Center - Alibaba Cloud OpenAPI Developer Portal.
-
Simple Log Service provides a CLI for automated configuration. For details, see Simple Log Service Command Line Interface (CLI).