@rk911/tracker-sdk 1.7.15 → 1.7.17

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/index.js CHANGED
@@ -253,17 +253,19 @@ function _ensureUserId(tracker) {
253
253
  } catch (e) {}
254
254
  }
255
255
 
256
- // 2. URL query 跨端注入参数兜底
257
- if (!uid && typeof window !== 'undefined' && window.location && window.location.search) {
256
+ // 2. URL query 跨端注入参数兜底(全面兼容标准 search 与 SPA Hash 路由后的 query)
257
+ if (typeof window !== 'undefined' && window.location) {
258
258
  try {
259
- var search = window.location.search
259
+ var search = (window.location.search || '') + '&' + (window.location.hash || '')
260
260
  var matchSeq = search.match(/[?&]_trk_seq=([^&]+)/)
261
261
  if (matchSeq) {
262
262
  session.setInitialSequence(Number(decodeURIComponent(matchSeq[1])))
263
263
  }
264
- var matchUid = search.match(/[?&]_trk_userId=([^&]+)/)
265
- if (matchUid) {
266
- uid = decodeURIComponent(matchUid[1])
264
+ if (!uid) {
265
+ var matchUid = search.match(/[?&]_trk_userId=([^&]+)/)
266
+ if (matchUid) {
267
+ uid = decodeURIComponent(matchUid[1])
268
+ }
267
269
  }
268
270
  } catch (e) {}
269
271
  }
@@ -74,6 +74,7 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
74
74
  private volatile String userId;
75
75
  private volatile String phone;
76
76
  private volatile String storeName;
77
+ private volatile String userName;
77
78
  private volatile int sequence = 0;
78
79
  private volatile int pflag = -1;
79
80
  private volatile String platformName = "";
@@ -189,6 +190,10 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
189
190
  * 初始化 SDK
190
191
  */
191
192
  public void init(Application app, String serverUrl, String deviceId, String userId, String phone, String storeName) {
193
+ init(app, serverUrl, deviceId, userId, phone, storeName, "");
194
+ }
195
+
196
+ public void init(Application app, String serverUrl, String deviceId, String userId, String phone, String storeName, String userName) {
192
197
  this.application = app;
193
198
  if (serverUrl != null && !serverUrl.contains("/api/point/task/trackBatch")) {
194
199
  if (serverUrl.endsWith("/")) {
@@ -203,6 +208,7 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
203
208
  this.userId = userId != null ? userId : "";
204
209
  this.phone = phone != null ? phone : "";
205
210
  this.storeName = storeName != null ? storeName : "";
211
+ this.userName = userName != null ? userName : "";
206
212
  this.sessionId = "android_" + System.currentTimeMillis() + "_" + (int) (Math.random() * 10000);
207
213
 
208
214
  // 初始化 SQLite 本地持久化数据库 (S1 防丢保障)
@@ -233,10 +239,15 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
233
239
  }
234
240
 
235
241
  public void updateUser(String userId, String phone, String storeName) {
242
+ updateUser(userId, phone, storeName, null);
243
+ }
244
+
245
+ public void updateUser(String userId, String phone, String storeName, String userName) {
236
246
  if (userId != null) this.userId = userId;
237
247
  if (phone != null) this.phone = phone;
238
248
  if (storeName != null) this.storeName = storeName;
239
- Log.i(TAG, "===> [Native Tracker User Updated] UserId: " + this.userId + ", Phone: " + this.phone + ", Store: " + this.storeName);
249
+ if (userName != null) this.userName = userName;
250
+ Log.i(TAG, "===> [Native Tracker User Updated] UserId: " + this.userId + ", Phone: " + this.phone + ", Store: " + this.storeName + ", User: " + this.userName);
240
251
  }
241
252
 
242
253
  public void setPlatform(int pflag, String platformName) {
@@ -273,6 +284,10 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
273
284
  return storeName != null ? storeName : "";
274
285
  }
275
286
 
287
+ public String getUserName() {
288
+ return userName != null ? userName : "";
289
+ }
290
+
276
291
  public void setUserId(String userId) {
277
292
  updateUser(userId, null, null);
278
293
  }
@@ -424,7 +439,19 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
424
439
  event.put("elementText", elementText);
425
440
  }
426
441
  if (customProperties != null) {
427
- event.put("extraData", customProperties.toString());
442
+ JSONObject extra = new JSONObject();
443
+ String baseExtraStr = event.optString("extraData");
444
+ if (!TextUtils.isEmpty(baseExtraStr)) {
445
+ try {
446
+ extra = new JSONObject(baseExtraStr);
447
+ } catch (Exception ignored) {}
448
+ }
449
+ Iterator<String> keys = customProperties.keys();
450
+ while (keys.hasNext()) {
451
+ String k = keys.next();
452
+ extra.put(k, customProperties.opt(k));
453
+ }
454
+ event.put("extraData", extra.toString());
428
455
  }
429
456
  enqueueEvent(event);
430
457
  flushAsync();
@@ -442,7 +469,19 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
442
469
  try {
443
470
  JSONObject event = createBaseEvent("custom", eventName);
444
471
  if (customProperties != null) {
445
- event.put("extraData", customProperties.toString());
472
+ JSONObject extra = new JSONObject();
473
+ String baseExtraStr = event.optString("extraData");
474
+ if (!TextUtils.isEmpty(baseExtraStr)) {
475
+ try {
476
+ extra = new JSONObject(baseExtraStr);
477
+ } catch (Exception ignored) {}
478
+ }
479
+ Iterator<String> keys = customProperties.keys();
480
+ while (keys.hasNext()) {
481
+ String k = keys.next();
482
+ extra.put(k, customProperties.opt(k));
483
+ }
484
+ event.put("extraData", extra.toString());
446
485
  }
447
486
  enqueueEvent(event);
448
487
  flushAsync(); // 自定义点位立即上报
@@ -457,26 +496,40 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
457
496
  * 旧 $dadian 点位兼容方法 (自动匹配后端双写 point.log)
458
497
  */
459
498
  public void trackFrontUrl(final String frontUrl, final String elementId, final String postParam) {
460
- final String key = (frontUrl != null ? frontUrl : "") + "|" + (elementId != null ? elementId : "");
461
- final long now = System.currentTimeMillis();
462
- if (key.equals(lastLegacyClickKey) && (now - lastLegacyClickTime < 500)) {
463
- return;
499
+ boolean isExposure = elementId != null && (elementId.startsWith("look") || elementId.startsWith("show") || elementId.contains("Exposure") || elementId.contains("Show") || elementId.contains("Bubble"));
500
+ if (!isExposure) {
501
+ final String key = (frontUrl != null ? frontUrl : "") + "|" + (elementId != null ? elementId : "") + "|" + (postParam != null ? postParam : "");
502
+ final long now = System.currentTimeMillis();
503
+ if (key.equals(lastLegacyClickKey) && (now - lastLegacyClickTime < 500)) {
504
+ return;
505
+ }
506
+ lastLegacyClickKey = key;
507
+ lastLegacyClickTime = now;
464
508
  }
465
- lastLegacyClickKey = key;
466
- lastLegacyClickTime = now;
467
509
 
468
510
  postBgTask(new Runnable() {
469
511
  @Override
470
512
  public void run() {
471
513
  try {
472
- String eventType = "click";
473
- if (elementId != null && (elementId.startsWith("look") || elementId.contains("Exposure") || elementId.contains("Show") || elementId.contains("Bubble"))) {
474
- eventType = "exposure";
514
+ // 对齐 tracker-sdk 规范:业务手动精准埋点统一使用 custom 事件类型,令两端一致触发后端双写 (SDK_DESIGN 2.2)
515
+ String eventType = "custom";
516
+ String targetId = !TextUtils.isEmpty(frontUrl) ? frontUrl : elementId;
517
+ JSONObject event = createBaseEvent(eventType, targetId);
518
+ event.put("frontUrl", targetId);
519
+ event.put("elementId", targetId);
520
+ if (postParam != null) {
521
+ event.put("postParam", postParam);
522
+ event.put("extraKey", "param");
523
+ event.put("extraValue", postParam);
524
+ try {
525
+ String baseExtraStr = event.optString("extraData");
526
+ JSONObject extra = TextUtils.isEmpty(baseExtraStr) ? new JSONObject() : new JSONObject(baseExtraStr);
527
+ extra.put("param", postParam);
528
+ extra.put("getParam", postParam);
529
+ extra.put("postParam", postParam);
530
+ event.put("extraData", extra.toString());
531
+ } catch (Exception ignored) {}
475
532
  }
476
- JSONObject event = createBaseEvent(eventType, "legacy_dadian");
477
- event.put("frontUrl", frontUrl);
478
- event.put("elementId", elementId);
479
- if (postParam != null) event.put("postParam", postParam);
480
533
  enqueueEvent(event);
481
534
  flushAsync();
482
535
  } catch (Exception e) {
@@ -523,8 +576,13 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
523
576
  if (!TextUtils.isEmpty(error)) {
524
577
  extra.put("error", error.length() > 512 ? error.substring(0, 512) : error);
525
578
  }
579
+ String reqId = extractRequestIdSafe(reqParam, respData);
580
+ if (!TextUtils.isEmpty(reqId)) {
581
+ extra.put("requestId", reqId);
582
+ }
526
583
  if (!TextUtils.isEmpty(phone)) extra.put("phone", phone);
527
584
  if (!TextUtils.isEmpty(storeName)) extra.put("storeName", storeName);
585
+ if (!TextUtils.isEmpty(userName)) extra.put("userName", userName);
528
586
  if (!TextUtils.isEmpty(userId)) extra.put("userId", userId);
529
587
  if (pflag != -1) extra.put("pflag", pflag);
530
588
  if (!TextUtils.isEmpty(platformName)) extra.put("platformName", platformName);
@@ -562,6 +620,38 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
562
620
  }
563
621
  }
564
622
 
623
+ private static String extractRequestIdSafe(String reqParam, String respData) {
624
+ try {
625
+ if (!TextUtils.isEmpty(reqParam) && reqParam.contains("requestId")) {
626
+ int idx = reqParam.indexOf("\"requestId\"");
627
+ if (idx >= 0) {
628
+ int colon = reqParam.indexOf(':', idx);
629
+ if (colon >= 0) {
630
+ int q1 = reqParam.indexOf('"', colon);
631
+ if (q1 >= 0) {
632
+ int q2 = reqParam.indexOf('"', q1 + 1);
633
+ if (q2 > q1) return reqParam.substring(q1 + 1, q2);
634
+ }
635
+ }
636
+ }
637
+ }
638
+ if (!TextUtils.isEmpty(respData) && respData.contains("requestId")) {
639
+ int idx = respData.indexOf("\"requestId\"");
640
+ if (idx >= 0) {
641
+ int colon = respData.indexOf(':', idx);
642
+ if (colon >= 0) {
643
+ int q1 = respData.indexOf('"', colon);
644
+ if (q1 >= 0) {
645
+ int q2 = respData.indexOf('"', q1 + 1);
646
+ if (q2 > q1) return respData.substring(q1 + 1, q2);
647
+ }
648
+ }
649
+ }
650
+ }
651
+ } catch (Exception ignored) {}
652
+ return "";
653
+ }
654
+
565
655
  public String getNetworkType() {
566
656
  try {
567
657
  if (application == null) return "";
@@ -620,6 +710,7 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
620
710
  JSONObject extra = new JSONObject();
621
711
  if (!TextUtils.isEmpty(phone)) extra.put("phone", phone);
622
712
  if (!TextUtils.isEmpty(storeName)) extra.put("storeName", storeName);
713
+ if (!TextUtils.isEmpty(userName)) extra.put("userName", userName);
623
714
  if (!TextUtils.isEmpty(userId)) extra.put("userId", userId);
624
715
  if (pflag != -1) extra.put("pflag", pflag);
625
716
  if (!TextUtils.isEmpty(platformName)) extra.put("platformName", platformName);
@@ -758,6 +849,7 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
758
849
  body.put("userId", batchUserId);
759
850
  body.put("phone", phone != null ? phone : "");
760
851
  body.put("storeName", storeName != null ? storeName : "");
852
+ body.put("userName", userName != null ? userName : "");
761
853
  body.put("platform", "android");
762
854
  if (pflag != -1) body.put("pflag", pflag);
763
855
  if (!TextUtils.isEmpty(platformName)) body.put("platformName", platformName);
@@ -996,7 +1088,7 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
996
1088
  if (TextUtils.isEmpty(elementId)) return "";
997
1089
  String lower = elementId.toLowerCase();
998
1090
  if (lower.equals("ll_page_one")) return "首页";
999
- if (lower.equals("ll_page_two")) return "银友圈";
1091
+ if (lower.equals("ll_page_two")) return "发光者";
1000
1092
  if (lower.equals("ll_page_three")) return "小视频";
1001
1093
  if (lower.equals("ll_page_four")) return "商城";
1002
1094
  if (lower.equals("ll_page_five")) return "我的";
@@ -1066,6 +1158,12 @@ public class AndroidTracker implements Application.ActivityLifecycleCallbacks {
1066
1158
  event.put("elementText", elementText.length() > 30 ? elementText.substring(0, 30) : elementText);
1067
1159
  }
1068
1160
  JSONObject extra = new JSONObject();
1161
+ String baseExtraStr = event.optString("extraData");
1162
+ if (!TextUtils.isEmpty(baseExtraStr)) {
1163
+ try {
1164
+ extra = new JSONObject(baseExtraStr);
1165
+ } catch (Exception ignored) {}
1166
+ }
1069
1167
  extra.put("viewType", target.getClass().getName());
1070
1168
  extra.put("pageX", rawX);
1071
1169
  extra.put("pageY", rawY);