• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Haixing-Hu / js-common-util / ca172817-e79b-4d73-b001-ce3615200b81

25 Dec 2023 09:03AM UTC coverage: 58.668%. Remained the same
ca172817-e79b-4d73-b001-ce3615200b81

push

circleci

Haixing-Hu
refactor: change the dependency of `qs` to `query-string`

296 of 530 branches covered (0.0%)

Branch coverage included in aggregate %.

4 of 5 new or added lines in 4 files covered. (80.0%)

347 of 566 relevant lines covered (61.31%)

14.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.9
/src/talking-data.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2023.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9
import { Logger } from '@haixing_hu/logging';
10
import queryString from './query-string';
11
import getSearch from './get-search';
12
import addSearchParams from './add-search-params';
13
import redirect from './redirect';
14
import loadScript from './load-script';
15

16
/**
17
 * The URL of TalkingData’s H5 SDK.
18
 *
19
 * @private
20
 */
21
const SDK_URL = 'https://jic.talkingdata.com/app/h5/v1';
34✔
22

23
const logger = Logger.getLogger('TalkingData');
34✔
24

25
/**
26
 * Encapsulates the API function of TalkingData application tracking.
27
 *
28
 * @author Haixing Hu
29
 */
30
class TalkingData {
31
  constructor() {
32
    this.source = '';   // 记录当前渠道代码
34✔
33
  }
34

35
  /**
36
   * Initialize the SDK of TalkingData.
37
   *
38
   * @param {String} appId
39
   *     The App ID of the application in TalkingData.
40
   * @param {String} appName
41
   *     The name of the application.
42
   * @param {String} appVersion
43
   *     The version of the application.
44
   * @param {String} defaultSource
45
   *     The optional default source of the application.
46
   * @return {Promise}
47
   *     A {@link Promise} object.
48
   */
49
  init(appId, appName, appVersion, defaultSource = undefined) {
×
50
    return new Promise((resolve, reject) => {
×
51
      logger.info('Initializing the TalkingData SDK ...');
×
52
      const search = getSearch();
×
NEW
53
      const args = queryString.parse(search);
×
54
      if (!args.source) {
×
55
        if (defaultSource) {
×
56
          const url = addSearchParams({
×
57
            td_channelid: defaultSource,
58
            source: defaultSource,
59
          });
60
          redirect(url, 0).then(() => resolve(null));
×
61
          return;
×
62
        }
63
        // if the `defaultSource` is `undefined`, pass through
64
      } else if (!args.td_channelid) {
×
65
        const url = addSearchParams({
×
66
          td_channelid: args.source,
67
        });
68
        redirect(url, 0).then(() => resolve(null));
×
69
        return;
×
70
      }
71
      // Remember the source. Note that the source maybe `undefined`
72
      this.source = args.source || defaultSource;
×
73
      const url = `${SDK_URL}?appid=${appId}&vn=${appName}&vc=${appVersion}`;
×
74
      logger.info('Loading Talking Data SDK script:', url);
×
75
      loadScript(url).then((script) => {
×
76
        logger.info('Successfully loading the Talking Data SDK script.');
×
77
        resolve(script);
×
78
      }).catch((error) => {
79
        logger.error('Failed to load the Talking Data SDK script:', error);
×
80
        reject(error);
×
81
      });
82
    });
83
  }
84

85
  /**
86
   * Triggers the TalkingData application usage statistics tracking event.
87
   *
88
   * Do not call this function directly. It is recommended to use the
89
   * {@link enter} and {@link perform} functions.
90
   *
91
   * @param {String} event
92
   *     The event.
93
   * @param {String} label
94
   *     The optional tag.
95
   * @author Haixing Hu
96
   */
97
  trace(event, label = undefined) {
×
98
    if (window.TDAPP) {
×
99
      if (label) {
×
100
        logger.info('Fire the event: %s (%s)', event, label);
×
101
        window.TDAPP.onEvent(event, label);
×
102
      } else {
103
        logger.info('Fire the event:', event);
×
104
        window.TDAPP.onEvent(event);
×
105
      }
106
    } else if (label) {
×
107
      logger.error('Fire the event: %s (%s), but the TalkingData SKD was not '
×
108
        + 'loaded.', event, label);
109
    } else {
110
      logger.error('Fire the event: %s, but the TalkingData SKD was not loaded.', event);
×
111
    }
112
  }
113

114
  /**
115
   * When visiting a page, a TalkingData application usage statistics tracking
116
   * event is triggered.
117
   *
118
   * @param {String} page
119
   *     The name of the page.
120
   * @param {String} source
121
   *     The optional source code. If this parameter is not provided, the
122
   *     source code obtained from the URL during initialization is used.
123
   */
124
  enter(page, source = undefined) {
×
125
    this.trace(`Enter ${page}`, source || this.source);
×
126
  }
127

128
  /**
129
   * When an operation is performed on a specified page, the TalkingData
130
   * application usage statistics tracking event is triggered.
131
   *
132
   * @param {String} page
133
   *     The name of the page.
134
   * @param {String} action
135
   *     The name of the operation.
136
   * @param {String} source
137
   *     The optional source code. if this parameter is not provided, the
138
   *     source code obtained from the URL during initialization is used.
139
   */
140
  perform(page, action, source = undefined) {
×
141
    this.trace(`Enter ${page} and perform ${action}`, source || this.source);
×
142
  }
143
}
144

145
const talkingData = new TalkingData();
34✔
146

147
export default talkingData;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc