visual-buried-point-platform-uni 1.0.0-alpha.1 → 1.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,18 +1,19 @@
1
1
  ## Uni版自定义埋点SDK集成文档
2
2
 
3
+ ### SDK更新日志
4
+ - 1.0.0-alpha.07 最新uni、终端通用埋点sdk;
5
+ - 1.0.0-alpha.08 自定义上报数据结构删除path字段;
6
+
3
7
  ### 1、引入方式
4
8
 
5
9
  ```js
6
- npm install visual-buried-point-platform-uni -S --registry=http://verdaccio.gogdev.cn/
7
- 工具-构建npm
10
+ //内网npm链接:http://verdaccio.gogdev.cn/
11
+ npm install visual-buried-point-platform-uni
8
12
  ```
9
13
 
10
14
  ### 2、初始化方式
11
15
 
12
- **app.js**
13
-
14
16
  ```js
15
- //require引入
16
17
  import { init } from 'visual-buried-point-platform-uni'
17
18
  onLaunch: function() {
18
19
  //初始化sdk
@@ -47,8 +48,6 @@ setUserId(123456);
47
48
  setUserId("");
48
49
  ```
49
50
 
50
-
51
-
52
51
  ### 4、自定义事件上报
53
52
 
54
53
  - sdk初始化后,可通过 ***setCustomEvent()*** 方法上报自定义埋点事件,并为事件添加属性值:
package/index.js CHANGED
@@ -11,6 +11,8 @@ import {
11
11
  getDeviceInfoError,
12
12
  timeToStr,
13
13
  getTrackObj,
14
+ getCurPath,
15
+ getUrl,
14
16
  } from "./tools.js";
15
17
  import { setReportUrl } from "./config.js";
16
18
 
@@ -29,6 +31,8 @@ let _config = {
29
31
  srcDomain: "",
30
32
  flushInterval: 15,
31
33
  };
34
+ //custom
35
+ let timer = null;
32
36
  //track upload interval
33
37
  let tInterval = null;
34
38
  let busObj = null;
@@ -74,14 +78,13 @@ export function init(config) {
74
78
  startGlobalTime();
75
79
  }
76
80
 
77
- //the general custom report interval is enabled
81
+ //the general custom reportCustom interval is enabled
78
82
  function startGlobalTime() {
79
83
  clearInterval(cInterval);
80
84
  cInterval = setInterval(() => {
81
85
  let cData = getSData();
82
- console.log("storage data " + cData);
83
86
  if (cData) {
84
- report(cData);
87
+ reportCustom(cData);
85
88
  delSData();
86
89
  }
87
90
  }, _config.flushInterval * 1000);
@@ -106,7 +109,7 @@ export function setCustomEvent(data) {
106
109
  reportData = {
107
110
  ctk: data.$ctk ? data.$ctk : "",
108
111
  duration: "",
109
- element: null,
112
+ element: data.$element ? data.$element : null,
110
113
  eventId: data.$event_id ? data.$event_id : "",
111
114
  event: data.$event_code ? data.$event_code : "",
112
115
  groupId: "",
@@ -117,27 +120,29 @@ export function setCustomEvent(data) {
117
120
  pageId: MD5(_path),
118
121
  path: _path,
119
122
  title: _page.title ? _page.title : "",
123
+ url: _path ? _path : getUrl(platform),
120
124
  },
121
- path: data.$path ? data.$path : "",
122
125
  properties: data.$extend_param ? JSON.stringify(data.$extend_param) : "",
123
126
  traceId: "",
124
127
  trackId: trackId,
125
128
  triggerTime: timeToStr(Date.now()),
126
- url: "",
127
129
  };
128
130
  if (reportData) {
129
131
  addCache(reportData);
130
- const data = getCache();
131
- if (data.length) {
132
- addSData(data);
133
- clearCache();
134
- }
132
+ clearTimeout(timer);
133
+ timer = setTimeout(() => {
134
+ const data = getCache();
135
+ if (data.length) {
136
+ addSData(data);
137
+ clearCache();
138
+ }
139
+ }, 1000);
135
140
  }
136
141
  }
137
142
 
138
- function report(data) {
143
+ function reportCustom(data) {
139
144
  if (!reportUrl) {
140
- //console.error("please set upload data url");
145
+ console.error("please set upload data reportUrl");
141
146
  return;
142
147
  }
143
148
  const comData = commonData(data);
@@ -145,25 +150,23 @@ function report(data) {
145
150
  ...comData,
146
151
  dataAbstract: MD5(JSON.stringify(comData)),
147
152
  };
148
- uni.nextTick(function () {
149
- uni.request({
150
- url: reportUrl,
151
- method: "POST",
152
- header: {
153
- "Content-Type": "application/json",
154
- appKey: _config.token,
155
- projectId: _config.token,
156
- },
157
- data: reportData,
158
- success: function (res) {},
159
- fail: function (error) {
160
- console.error(error);
161
- },
162
- });
153
+ uni.request({
154
+ url: reportUrl,
155
+ method: "POST",
156
+ header: {
157
+ "Content-Type": "application/json",
158
+ appKey: _config.token,
159
+ projectId: _config.token,
160
+ },
161
+ data: reportData,
162
+ success: function (res) {},
163
+ fail: function (error) {
164
+ console.error(error);
165
+ },
163
166
  });
164
167
  }
165
168
 
166
- //track upload-注册
169
+ //track upload-registry
167
170
  export function onStartTrack(tData) {
168
171
  busObj = null;
169
172
  startTime = 0;
@@ -186,17 +189,27 @@ export function onDestroyTrack() {
186
189
 
187
190
  function assemblyTrackData(type) {
188
191
  if (busObj) {
189
- let obj = getTrackObj(busObj);
192
+ let obj = getTrackObj(busObj, platform);
190
193
  let trackData = null;
194
+ let cirtemp = -1;
191
195
  if (type === "destroy") {
196
+ cirtemp = 3;
192
197
  startTime = (Date.now() - startTimeLong) / 1000;
198
+ } else {
199
+ if (startTime === 0) {
200
+ startTime === 0;
201
+ cirtemp = 1;
202
+ } else {
203
+ startTime = 15;
204
+ cirtemp = 2;
205
+ }
193
206
  }
194
207
  trackData = {
195
208
  busSegment: busObj.busSegment,
196
- circulation: obj.circulation,
209
+ circulation: cirtemp,
197
210
  ctk: busObj.ctk ? busObj.ctk : "",
198
211
  domain: obj.domain,
199
- duration: startTime === 0 ? 0 : startTime,
212
+ duration: startTime,
200
213
  module: busObj.module,
201
214
  path: obj.path,
202
215
  properties: busObj.extend_param
@@ -243,24 +256,11 @@ function reportTrack(data) {
243
256
  });
244
257
  }
245
258
 
246
- function getCurPath() {
247
- if (platform) {
248
- if (platform == "wx") {
249
- let gcp = getCurrentPages();
250
- return gcp ? gcp[gcp.length - 1].route : "/";
251
- } else if (platform == "web") {
252
- return window.location.hostname;
253
- } else if (platform == "android" || platform === "ios") {
254
- return "";
255
- }
256
- }
257
- }
258
-
259
259
  function commonData(allData) {
260
260
  return {
261
261
  app: appData,
262
262
  data: allData,
263
- deviceData: deviceData,
263
+ device: deviceData,
264
264
  lsi: localLsi,
265
265
  projectId: _config.token,
266
266
  userAgent: uAgent ? uAgent : "",
@@ -289,7 +289,7 @@ function getBaseicInfo() {
289
289
  platform = "wx";
290
290
  appData = wxInfo();
291
291
  }
292
- deviceData = getDeviceInfo(res);
292
+ deviceData = getDeviceInfo(res, platform);
293
293
  },
294
294
  fail: function (error) {
295
295
  deviceData = getDeviceInfoError();
@@ -297,10 +297,7 @@ function getBaseicInfo() {
297
297
  complete: function () {
298
298
  uni.getNetworkType({
299
299
  success(res) {
300
- deviceData = {
301
- ...deviceData,
302
- network: res.networkType,
303
- };
300
+ deviceData.network = res.networkType;
304
301
  },
305
302
  });
306
303
  },
package/lsi-md5.js CHANGED
@@ -1,15 +1,14 @@
1
1
  // 此js在npm build,根据name + version + md5后会自动修改package.json中的lsi
2
2
  // 注:发给后端,作为标识依据和上报字段lsi值
3
- import fs from 'fs' ;
4
- import MD5 from 'md5' ;
5
- const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
6
-
3
+ import fs from "fs";
4
+ import MD5 from "md5";
5
+ const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
7
6
 
8
7
  const { name, version } = packageJson;
9
8
  // MD5
10
9
  const combinedString = name + "-" + version;
11
10
  const md5Str = MD5(combinedString);
12
- packageJson.lsi = md5Str;
11
+ packageJson.lsi = md5Str + "#" + version;
13
12
  //base64
14
13
  // const combinedString = name.substring(0, 16) + "v" + version;
15
14
  // const base64Str = btoa(combinedString);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "visual-buried-point-platform-uni",
3
- "version": "1.0.0-alpha.01",
4
- "lsi": "e640f6dcca0160bfded7c074ba108e9f",
3
+ "version": "1.0.0-alpha.11",
4
+ "lsi": "451804ea84c42e02c91ca4788c40d48c#1.0.0-alpha.11",
5
5
  "description": "To make it easy for you to get started with GitLab, here's a list of recommended next steps.",
6
6
  "main": "index.js",
7
7
  "scripts": {
package/tools.js CHANGED
@@ -64,11 +64,14 @@ export function webH5Info(result) {
64
64
 
65
65
  //微信的appInfo
66
66
  export function wxInfo() {
67
- let accInfo = uni.getAccountInfoSync();
67
+ const accInfo = uni.getAccountInfoSync();
68
+ const bInfo = uni.getAppBaseInfo();
68
69
  return {
69
- name: "",
70
- packageName: accInfo ? accInfo.appId : "",
71
- version: accInfo ? accInfo.envVersion + accInfo.version : "",
70
+ name: bInfo.appName ? bInfo.appName : "",
71
+ packageName: accInfo ? accInfo.miniProgram.appId : "",
72
+ version: accInfo
73
+ ? accInfo.miniProgram.envVersion + accInfo.miniProgram.version
74
+ : "",
72
75
  carrier: "mini",
73
76
  ecology: "wechat",
74
77
  };
@@ -81,6 +84,8 @@ export function getAppInfo(result) {
81
84
  name: result.appName,
82
85
  version: version ? version : "",
83
86
  carrier: "app",
87
+ packageName: "",
88
+ ecology: "",
84
89
  };
85
90
  if (result.platform === "android") {
86
91
  let osName = "android";
@@ -88,47 +93,44 @@ export function getAppInfo(result) {
88
93
  if (result.romName.includes("HarmonyOS")) {
89
94
  osName = "harmonyOS";
90
95
  }
91
- app = {
92
- ...app,
93
- packageName: pkName ? pkName : "",
94
- ecology: osName,
95
- };
96
+ app.packageName = pkName ? pkName : "";
97
+ app.ecology = osName;
96
98
  } else if (result.platform === "ios") {
97
99
  let pkName = plus.ios
98
100
  .importClass("NSBundle")
99
101
  .mainBundle()
100
102
  .bundleIdentifier();
101
- app = {
102
- ...app,
103
- packageName: pkName ? pkName : "",
104
- ecology: "ios",
105
- };
103
+ app.packageName = pkName ? pkName : "";
104
+ app.ecology = "ios";
106
105
  } else {
107
- app = {
108
- ...app,
109
- packageName: "",
110
- version: "",
111
- ecology: "unknown",
112
- };
106
+ app.ecology = "unknown";
113
107
  }
114
108
  return app;
115
109
  }
116
110
 
117
- function getCurDeviceType(ua) {
118
- const userAgent = ua.toLowerCase();
119
- if (/(windows|win32|win64|wow64)/.test(userAgent)) {
120
- return 1;
121
- } else if (/(linux|android)/.test(userAgent)) {
122
- return 2;
123
- } else if (/(macintosh|mac os x|iphone|ipad|ipod)/.test(userAgent)) {
124
- return 2;
111
+ function getCurDeviceType(res, platform) {
112
+ if (platform !== "wx") {
113
+ const userAgent = res.ua.toLowerCase();
114
+ if (/(windows|win32|win64|wow64)/.test(userAgent)) {
115
+ return 1;
116
+ } else if (/(linux|android)/.test(userAgent)) {
117
+ return 2;
118
+ } else if (/(macintosh|mac os x|iphone|ipad|ipod)/.test(userAgent)) {
119
+ return 2;
120
+ } else {
121
+ return 2;
122
+ }
125
123
  } else {
126
- return 2;
124
+ if (res.deviceType === "pc") {
125
+ return 1;
126
+ } else {
127
+ return 2;
128
+ }
127
129
  }
128
130
  }
129
131
 
130
132
  //获取设备信息
131
- export function getDeviceInfo(res) {
133
+ export function getDeviceInfo(res, platform) {
132
134
  let device = {
133
135
  lang: res.osLanguage ? res.osLanguage : res.hostLanguage,
134
136
  brand: res.deviceBrand + " " + res.deviceModel,
@@ -139,7 +141,7 @@ export function getDeviceInfo(res) {
139
141
  browserVersion: res.browserVersion,
140
142
  color: "",
141
143
  deviceId: res.deviceId,
142
- deviceType: getCurDeviceType(res.ua),
144
+ deviceType: getCurDeviceType(res, platform),
143
145
  network: "",
144
146
  };
145
147
  return device;
@@ -162,48 +164,35 @@ export function getDeviceInfoError() {
162
164
  };
163
165
  }
164
166
 
165
- //流量上报H5环境
166
- export function getTrackObj(busObj) {
167
- //uni 通用h5、app、wx拿到当前路由栈
167
+ //uni 通用h5、app、wx拿到当前路由栈
168
+ export function getTrackObj(busObj, platform) {
168
169
  const pages = getCurrentPages();
169
170
  const gcp = pages[pages.length - 1];
170
- //h5
171
- let wl = window.location;
172
- let docTitle = document.title;
173
- let refurl = document.referrer;
174
- let parsedUrl;
175
- if (refurl) {
176
- parsedUrl = new URL(refurl);
177
- } else {
178
- let busRefUrl = busObj.sourceUrl;
179
- if (busRefUrl) {
180
- parsedUrl = new URL(busRefUrl);
181
- } else {
182
- parsedUrl = "";
183
- }
184
- }
185
171
  if (gcp) {
186
- let domain = "";
187
- let url = "";
188
- if (busObj.domain) {
189
- domain = busObj.domain;
190
- } else {
191
- domain = wl.hostname ? wl.hostname : "";
192
- }
193
- if (busObj.url) {
194
- url = busObj.url;
195
- } else {
196
- url = wl.href ? wl.href : "";
197
- }
198
172
  return {
199
173
  path: gcp.route,
200
- domain: domain,
201
- url: url,
202
- circulation: gcp.length > 1 ? 2 : 1,
203
- visitPage: gcp.length,
204
- sourceDomain: parsedUrl ? parsedUrl.hostname : "",
205
- sourceUrl: parsedUrl,
206
- title: docTitle ? docTitle : "",
174
+ domain: busObj.domain ? busObj.domain : "",
175
+ url: busObj.url ? busObj.url : getUrl(platform),
176
+ circulation: pages.length > 1 ? 2 : 1,
177
+ visitPage: pages.length,
178
+ sourceDomain: busObj.sourceDomain ? busObj.sourceDomain : "",
179
+ sourceUrl: busObj.sourceUrl ? busObj.sourceUrl : "",
180
+ title: busObj.title ? busObj.title : "",
207
181
  };
208
182
  }
209
183
  }
184
+
185
+ export function getCurPath() {
186
+ const pages = getCurrentPages();
187
+ const gcp = pages[pages.length - 1];
188
+ return gcp ? gcp.route : "/";
189
+ }
190
+
191
+ export function getUrl(platform) {
192
+ if (!platform) return "";
193
+ if (platform === "web" || platform === "h5") {
194
+ return window.location.href;
195
+ } else {
196
+ return "";
197
+ }
198
+ }