Log Service provides the web tracking feature that you can use to collect logs from the HTML, HTML5, iOS, and Android platforms. You can also customize dimensions and metrics to collect logs. This topic describes how to use the web tracking feature to collect logs.

Background information

You can use the web tracking feature to collect user information from browsers, iOS apps, or Android apps. The information includes:
  • Browsers, operating systems, and resolutions that are used by users.
  • The browsing behavior, such as clicks and purchases on a website.
  • The amount of time that users spend on an app and whether the users are active.

Usage notes

  • After you enable the web tracking feature for a Logstore, the write permissions on the Logstore are granted to anonymous users from the Internet. This may generate dirty data because authentication may not be performed.
  • The body of each GET request cannot exceed 16 KB in size.
  • You can write a maximum of 10 MB of log data at a time by sending a POST request. For more information, see PutLogs.

Step 1: Enable the web tracking feature

You can use the Log Service console or an SDK to enable the web tracking feature for a Logstore.

  • Enable the web tracking feature in the Log Service console.
    1. Log on to the Log Service console.
    2. In the Projects section, click the project in which you want to enable web tracking for a Logstore.
    3. Choose Log Storage > Logstores. On the Logstores tab, find the Logstore that you want to manage and choose Icon > Modify.
    4. In the upper-right corner of the Logstore Attributes page, click Modify.
    5. Turn on WebTracking and click Save.
  • Use an SDK to enable the web tracking feature.
    The following script shows how to use Log Service SDK for Java to enable the web tracking feature. For more information, see Get started with Log Service SDK for Java.
    import com.aliyun.openservices.log.Client;
    import com.aliyun.openservices.log.common.LogStore;
    import com.aliyun.openservices.log.exception.LogException;
    public class WebTracking {
      static private String accessId = "your accesskey id";
      static private String accessKey = "your accesskey";
      static private String project = "your project";
      static private String host = "log service data address";
      static private String logStore = "your logstore";
      static private Client client = new Client(host, accessId, accessKey);
      public static void main(String[] args) {
          try {
              // Enable the web tracking feature for an existing Logstore. 
              LogStore logSt = client.GetLogStore(project, logStore).GetLogStore();
              client.UpdateLogStore(project, new LogStore(logStore, logSt.GetTtl(), logSt.GetShardCount(), true));
              // Disable the web tracking feature. 
              //client.UpdateLogStore(project, new LogStore(logStore, logSt.GetTtl(), logSt.GetShardCount(), false));
              // Create a Logstore for which you want to enable the web tracking feature. 
              //client.UpdateLogStore(project, new LogStore(logStore, 1, 1, true));
          }
          catch (LogException e){
              e.printStackTrace();
          }
      }
    }

Step 2: Collect logs

After you enable the web tracking feature for a Logstore, you can upload logs to the Logstore by using the following methods:
  • Use web tracking SDK for JavaScript to collect browser logs.
  • Use web tracking SDK for JavaScript to collect mini program logs.
  • Use the GET method to upload logs.
    Run the following command to upload logs. Replace the values of the parameters based on your business requirements. The following table describes the parameters.
    curl --request GET 'http://${project}.${host}/logstores/${logstore}/track?APIVersion=0.6.0&key1=val1&key2=val2'
    Parameter Required Description
    ${project} Yes The name of the project.
    ${host} Yes The endpoint of the region where Log Service resides. For more information, see Endpoints.
    ${logstore} Yes The name of the Logstore.
    APIVersion=0.6.0 Yes A reserved parameter.
    __topic__=yourtopic No The topic of the log that you want to upload.
    key1=val1&key2=val2 Yes The key-value pairs that you want to upload to Log Service.

    Make sure that the data size is less than 16 KB.

  • Use HTML <img> tags to upload logs.
    <img src='http://${project}.${host}/logstores/${logstore}/track.gif?APIVersion=0.6.0&key1=val1&key2=val2'/>
    <img src='http://${project}.${host}/logstores/${logstore}/track_ua.gif?APIVersion=0.6.0&key1=val1&key2=val2'/>
    The track_ua.gif file contains the custom parameters that you want to upload to Log Service. If you use this method to upload logs, Log Service records the custom parameters and the User-Agent and Referer HTTP headers as log fields.
    Note To collect the Referer HTTPS header, make sure that the URL in the preceding <img> tag uses the HTTPS protocol.
  • Use the POST method to upload logs.

    You can send an HTTP POST request to upload a large amount of data. For more information, see PutWebtracking.