pb-sxp-ui 1.16.14 → 1.16.15

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/dist/index.cjs CHANGED
@@ -763,8 +763,8 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
763
763
  var _a;
764
764
  const body = {
765
765
  eventName: params === null || params === void 0 ? void 0 : params.eventName,
766
- eventSource: params === null || params === void 0 ? void 0 : params.eventSource,
767
- eventInfo: (params === null || params === void 0 ? void 0 : params.eventInfo) ? JSON.stringify(params === null || params === void 0 ? void 0 : params.eventInfo) : ''
766
+ eventSource: params === null || params === void 0 ? void 0 : params.eventSource
767
+ // eventInfo: params?.eventInfo ? JSON.stringify(params?.eventInfo) : ''
768
768
  };
769
769
  return bffFetch === null || bffFetch === void 0 ? void 0 : bffFetch(`v1/data/collect/${(_a = bffDataSource === null || bffDataSource === void 0 ? void 0 : bffDataSource.headers) === null || _a === void 0 ? void 0 : _a['x-app-id']}/${params === null || params === void 0 ? void 0 : params.eventName}`, {
770
770
  method: 'POST',
@@ -791,8 +791,7 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
791
791
  const params = Object.fromEntries(url.searchParams.entries());
792
792
  bffCollectEvent({
793
793
  eventName: params === null || params === void 0 ? void 0 : params.ev,
794
- eventSource: 'Meta Pixel',
795
- eventInfo: params
794
+ eventSource: 'Meta Pixel'
796
795
  });
797
796
  }
798
797
  };
@@ -805,6 +804,116 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
805
804
  };
806
805
  }
807
806
  }, [bffCollectEvent]);
807
+ React.useEffect(() => {
808
+ // 类型安全声明
809
+ const navigatorWithBeacon = navigator;
810
+ if (!navigatorWithBeacon.sendBeacon) {
811
+ console.warn('navigator.sendBeacon is not supported');
812
+ return;
813
+ }
814
+ // 保存原始方法
815
+ const originalSendBeacon = navigatorWithBeacon.sendBeacon.bind(navigatorWithBeacon);
816
+ // 覆盖方法
817
+ navigatorWithBeacon.sendBeacon = function (url, data) {
818
+ const urlString = url instanceof URL ? url.href : url.toString();
819
+ const success = originalSendBeacon(url, data);
820
+ function parseBeaconData(data) {
821
+ return __awaiter(this, void 0, void 0, function* () {
822
+ if (typeof data === 'string')
823
+ return JSON.parse(data);
824
+ if (data instanceof Blob)
825
+ return JSON.parse(yield data.text());
826
+ if (data instanceof ArrayBuffer)
827
+ return JSON.parse(new TextDecoder().decode(data));
828
+ return null;
829
+ });
830
+ }
831
+ if (success) {
832
+ // 异步处理监控逻辑(不阻塞原始行为)
833
+ Promise.resolve().then(() => __awaiter(this, void 0, void 0, function* () {
834
+ var _a, _b;
835
+ try {
836
+ const parsedData = yield parseBeaconData(data);
837
+ if (success) {
838
+ // 使用你的自定义上报方法
839
+ if (urlString.includes('https://tr.snapchat.com/p')) {
840
+ (_b = (_a = parsedData === null || parsedData === void 0 ? void 0 : parsedData.req) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, (item) => {
841
+ var _a, _b;
842
+ if ((_a = item === null || item === void 0 ? void 0 : item.t) === null || _a === void 0 ? void 0 : _a.ev) {
843
+ bffCollectEvent({
844
+ eventName: (_b = item === null || item === void 0 ? void 0 : item.t) === null || _b === void 0 ? void 0 : _b.ev,
845
+ eventSource: 'Snapchat Pixel'
846
+ });
847
+ }
848
+ });
849
+ }
850
+ else if (urlString.includes('https://analytics.tiktok.com/api/v2/pixel') && (parsedData === null || parsedData === void 0 ? void 0 : parsedData.event)) {
851
+ bffCollectEvent({
852
+ eventName: parsedData.event,
853
+ eventSource: 'TikTok Pixel'
854
+ });
855
+ }
856
+ }
857
+ }
858
+ catch (error) {
859
+ console.error('监控失败:', error);
860
+ }
861
+ }));
862
+ }
863
+ // 非TikTok请求直接放行
864
+ return success;
865
+ };
866
+ // 清理函数:恢复原始方法
867
+ return () => {
868
+ navigatorWithBeacon.sendBeacon = originalSendBeacon;
869
+ };
870
+ }, [bffCollectEvent]);
871
+ React.useEffect(() => {
872
+ const _originalFetch = window.fetch;
873
+ // 覆盖 fetch 方法
874
+ window.fetch = function (input, init) {
875
+ var arguments_1 = arguments;
876
+ return __awaiter(this, void 0, void 0, function* () {
877
+ // 安全获取 URL 字符串
878
+ let url;
879
+ try {
880
+ url = typeof input === 'string'
881
+ ? input
882
+ : input instanceof URL
883
+ ? input.href
884
+ : input.url;
885
+ }
886
+ catch (error) {
887
+ console.error('解析请求URL失败:', error);
888
+ return _originalFetch.apply(this, arguments_1);
889
+ }
890
+ // 非GA4请求直接放行
891
+ if (!url.includes('https://www.google-analytics.com/g/collect')) {
892
+ return _originalFetch.apply(this, arguments_1);
893
+ }
894
+ // 检查是否是 GA4 收集端点
895
+ try {
896
+ // 1. 先执行原始请求
897
+ const response = yield _originalFetch.apply(this, arguments_1);
898
+ // 2. 检查请求是否成功 (HTTP 200-299)
899
+ // 安全解析 URL
900
+ const urlObj = new URL(url);
901
+ const params = Object.fromEntries(urlObj.searchParams.entries());
902
+ if (params === null || params === void 0 ? void 0 : params.en) {
903
+ bffCollectEvent({
904
+ eventName: params === null || params === void 0 ? void 0 : params.en,
905
+ eventSource: 'Google Analytics'
906
+ });
907
+ }
908
+ return response;
909
+ }
910
+ catch (error) {
911
+ console.error('处理 GA4 请求时出错:', error);
912
+ throw error;
913
+ }
914
+ });
915
+ };
916
+ }, [bffCollectEvent]);
808
917
  React.useEffect(() => {
809
918
  if (typeof (window === null || window === void 0 ? void 0 : window.fbq) === 'function') {
810
919
  window === null || window === void 0 ? void 0 : window.fbq('track', 'PageView');
@@ -1065,10 +1174,6 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
1065
1174
  const newUrl = updateQueryStringParameter(currentUrl, 'timestamp', Date.now());
1066
1175
  history.pushState({ path: newUrl }, '', newUrl);
1067
1176
  isPushState = true;
1068
- bffCollectEvent({
1069
- eventName: 'page_view',
1070
- eventSource: 'Google Analytics'
1071
- });
1072
1177
  }
1073
1178
  }
1074
1179
  else {
@@ -1078,31 +1183,17 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
1078
1183
  if (!isPushState) {
1079
1184
  if (eventName === 'PageView' && typeof (window === null || window === void 0 ? void 0 : window.gtag) === 'function') {
1080
1185
  window === null || window === void 0 ? void 0 : window.gtag('event', 'page_view');
1081
- bffCollectEvent({
1082
- eventName: 'page_view',
1083
- eventSource: 'Google Analytics'
1084
- });
1085
1186
  }
1086
1187
  const tiktokPixelEventParamsJson = (_b = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.tiktokPixel) === null || _b === void 0 ? void 0 : _b[eventName];
1087
1188
  if ((tiktokPixelEventParamsJson === null || tiktokPixelEventParamsJson === void 0 ? void 0 : tiktokPixelEventParamsJson.event_name) &&
1088
1189
  typeof (window === null || window === void 0 ? void 0 : window.ttq) === 'object' &&
1089
1190
  typeof ((_c = window === null || window === void 0 ? void 0 : window.ttq) === null || _c === void 0 ? void 0 : _c.track) === 'function') {
1090
1191
  (_d = window === null || window === void 0 ? void 0 : window.ttq) === null || _d === void 0 ? void 0 : _d.track(tiktokPixelEventParamsJson === null || tiktokPixelEventParamsJson === void 0 ? void 0 : tiktokPixelEventParamsJson.event_name, getEventParamsByJson(tiktokPixelEventParamsJson, product));
1091
- bffCollectEvent({
1092
- eventName: tiktokPixelEventParamsJson === null || tiktokPixelEventParamsJson === void 0 ? void 0 : tiktokPixelEventParamsJson.event_name,
1093
- eventSource: 'TikTok Pixel',
1094
- eventInfo: getEventParamsByJson(tiktokPixelEventParamsJson, product)
1095
- });
1096
1192
  }
1097
1193
  }
1098
1194
  const snapchatPixelEventParamsJson = (_e = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.snapchatPixel) === null || _e === void 0 ? void 0 : _e[eventName];
1099
1195
  if ((snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name) && typeof (window === null || window === void 0 ? void 0 : window.snaptr) === 'function') {
1100
1196
  window === null || window === void 0 ? void 0 : window.snaptr('track', snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name, getEventParamsByJson(snapchatPixelEventParamsJson, product));
1101
- bffCollectEvent({
1102
- eventName: snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name,
1103
- eventSource: 'Snapchat Pixel',
1104
- eventInfo: getEventParamsByJson(snapchatPixelEventParamsJson, product)
1105
- });
1106
1197
  }
1107
1198
  const converApiEventParamsJson = (_f = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.converApi) === null || _f === void 0 ? void 0 : _f[eventName];
1108
1199
  if (enabledMetaConversionApi && converApiEventParamsJson) {