pb-sxp-ui 1.16.14 → 1.16.16

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.js CHANGED
@@ -741,8 +741,8 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
741
741
  var _a;
742
742
  const body = {
743
743
  eventName: params === null || params === void 0 ? void 0 : params.eventName,
744
- eventSource: params === null || params === void 0 ? void 0 : params.eventSource,
745
- eventInfo: (params === null || params === void 0 ? void 0 : params.eventInfo) ? JSON.stringify(params === null || params === void 0 ? void 0 : params.eventInfo) : ''
744
+ eventSource: params === null || params === void 0 ? void 0 : params.eventSource
745
+ // eventInfo: params?.eventInfo ? JSON.stringify(params?.eventInfo) : ''
746
746
  };
747
747
  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}`, {
748
748
  method: 'POST',
@@ -761,19 +761,18 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
761
761
  set(value) {
762
762
  // 继续正常设置 src
763
763
  originalSrcDescriptor.set.call(this, value);
764
- img.onload = function () {
765
- // 检查是否是目标 URL
766
- if (value.includes('https://www.facebook.com/privacy_sandbox/pixel/register/trigger/')) {
767
- // 解析 URL 参数
768
- const url = new URL(value);
769
- const params = Object.fromEntries(url.searchParams.entries());
770
- bffCollectEvent({
771
- eventName: params === null || params === void 0 ? void 0 : params.ev,
772
- eventSource: 'Meta Pixel',
773
- eventInfo: params
774
- });
775
- }
776
- };
764
+ // img.onload = function () {
765
+ // 检查是否是目标 URL
766
+ if (value.includes('https://www.facebook.com/privacy_sandbox/pixel/register/trigger/')) {
767
+ // 解析 URL 参数
768
+ const url = new URL(value);
769
+ const params = Object.fromEntries(url.searchParams.entries());
770
+ bffCollectEvent({
771
+ eventName: params === null || params === void 0 ? void 0 : params.ev,
772
+ eventSource: 'Meta Pixel'
773
+ });
774
+ // }
775
+ }
777
776
  },
778
777
  get() {
779
778
  return originalSrcDescriptor.get.call(this);
@@ -783,6 +782,137 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
783
782
  };
784
783
  }
785
784
  }, [bffCollectEvent]);
785
+ useEffect(() => {
786
+ // 类型安全声明
787
+ const navigatorWithBeacon = navigator;
788
+ if (!navigatorWithBeacon.sendBeacon) {
789
+ console.warn('navigator.sendBeacon is not supported');
790
+ return;
791
+ }
792
+ // 保存原始方法
793
+ const originalSendBeacon = navigatorWithBeacon.sendBeacon.bind(navigatorWithBeacon);
794
+ // 覆盖方法
795
+ navigatorWithBeacon.sendBeacon = function (url, data) {
796
+ const urlString = url instanceof URL ? url.href : url.toString();
797
+ const success = originalSendBeacon(url, data);
798
+ function parseBeaconData(data) {
799
+ return __awaiter(this, void 0, void 0, function* () {
800
+ if (typeof data === 'string')
801
+ return JSON.parse(data);
802
+ if (data instanceof Blob)
803
+ return JSON.parse(yield data.text());
804
+ if (data instanceof ArrayBuffer)
805
+ return JSON.parse(new TextDecoder().decode(data));
806
+ return null;
807
+ });
808
+ }
809
+ if (success) {
810
+ // 异步处理监控逻辑(不阻塞原始行为)
811
+ Promise.resolve().then(() => __awaiter(this, void 0, void 0, function* () {
812
+ var _a, _b;
813
+ try {
814
+ const parsedData = yield parseBeaconData(data);
815
+ // 使用你的自定义上报方法
816
+ if (urlString.includes('https://tr.snapchat.com/p')) {
817
+ (_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) => {
818
+ var _a, _b;
819
+ if ((_a = item === null || item === void 0 ? void 0 : item.t) === null || _a === void 0 ? void 0 : _a.ev) {
820
+ bffCollectEvent({
821
+ eventName: (_b = item === null || item === void 0 ? void 0 : item.t) === null || _b === void 0 ? void 0 : _b.ev,
822
+ eventSource: 'Snapchat Pixel'
823
+ });
824
+ }
825
+ });
826
+ }
827
+ else if (urlString.includes('https://analytics.tiktok.com/api/v2/pixel') && (parsedData === null || parsedData === void 0 ? void 0 : parsedData.event)) {
828
+ bffCollectEvent({
829
+ eventName: parsedData.event,
830
+ eventSource: 'TikTok Pixel'
831
+ });
832
+ }
833
+ }
834
+ catch (error) {
835
+ console.error('监控失败:', error);
836
+ }
837
+ }));
838
+ }
839
+ // 非TikTok请求直接放行
840
+ return success;
841
+ };
842
+ // 清理函数:恢复原始方法
843
+ return () => {
844
+ navigatorWithBeacon.sendBeacon = originalSendBeacon;
845
+ };
846
+ }, [bffCollectEvent]);
847
+ useEffect(() => {
848
+ const _originalFetch = window.fetch;
849
+ // 覆盖 fetch 方法
850
+ window.fetch = function (input, init) {
851
+ var arguments_1 = arguments;
852
+ return __awaiter(this, void 0, void 0, function* () {
853
+ var _a;
854
+ // 安全获取 URL 字符串
855
+ let url;
856
+ try {
857
+ url = typeof input === 'string'
858
+ ? input
859
+ : input instanceof URL
860
+ ? input.href
861
+ : input.url;
862
+ }
863
+ catch (error) {
864
+ console.error('解析请求URL失败:', error);
865
+ return _originalFetch.apply(this, arguments_1);
866
+ }
867
+ // 非GA4请求直接放行
868
+ if (!url.includes('https://www.google-analytics.com/g/collect')) {
869
+ return _originalFetch.apply(this, arguments_1);
870
+ }
871
+ // 检查是否是 GA4 收集端点
872
+ try {
873
+ // 1. 先执行原始请求
874
+ const response = _originalFetch.apply(this, arguments_1);
875
+ // 2. 检查请求是否成功 (HTTP 200-299)
876
+ // 安全解析 URL
877
+ const urlObj = new URL(url);
878
+ const params = Object.fromEntries(urlObj.searchParams.entries());
879
+ const body = (_a = arguments_1[1]) === null || _a === void 0 ? void 0 : _a.body;
880
+ if (params === null || params === void 0 ? void 0 : params.en) {
881
+ bffCollectEvent({
882
+ eventName: params === null || params === void 0 ? void 0 : params.en,
883
+ eventSource: 'Google Analytics'
884
+ });
885
+ }
886
+ else if (body) {
887
+ function extractAllEnValuesSplit(queryString) {
888
+ if (typeof queryString !== 'string') {
889
+ return [];
890
+ }
891
+ const matches = [];
892
+ const regex = /en=([^&\r\n]*)/g;
893
+ let match;
894
+ while ((match = regex.exec(queryString)) !== null) {
895
+ matches.push(decodeURIComponent(match[1]));
896
+ }
897
+ return matches;
898
+ }
899
+ const enValues = extractAllEnValuesSplit(body);
900
+ enValues === null || enValues === void 0 ? void 0 : enValues.forEach((en) => {
901
+ bffCollectEvent({
902
+ eventName: en,
903
+ eventSource: 'Google Analytics'
904
+ });
905
+ });
906
+ }
907
+ return response;
908
+ }
909
+ catch (error) {
910
+ console.error('处理 GA4 请求时出错:', error);
911
+ throw error;
912
+ }
913
+ });
914
+ };
915
+ }, [bffCollectEvent]);
786
916
  useEffect(() => {
787
917
  if (typeof (window === null || window === void 0 ? void 0 : window.fbq) === 'function') {
788
918
  window === null || window === void 0 ? void 0 : window.fbq('track', 'PageView');
@@ -1043,10 +1173,6 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
1043
1173
  const newUrl = updateQueryStringParameter(currentUrl, 'timestamp', Date.now());
1044
1174
  history.pushState({ path: newUrl }, '', newUrl);
1045
1175
  isPushState = true;
1046
- bffCollectEvent({
1047
- eventName: 'page_view',
1048
- eventSource: 'Google Analytics'
1049
- });
1050
1176
  }
1051
1177
  }
1052
1178
  else {
@@ -1056,31 +1182,17 @@ const SxpDataSourceProvider = ({ render, dataSources, utmVal, enableReportEvent
1056
1182
  if (!isPushState) {
1057
1183
  if (eventName === 'PageView' && typeof (window === null || window === void 0 ? void 0 : window.gtag) === 'function') {
1058
1184
  window === null || window === void 0 ? void 0 : window.gtag('event', 'page_view');
1059
- bffCollectEvent({
1060
- eventName: 'page_view',
1061
- eventSource: 'Google Analytics'
1062
- });
1063
1185
  }
1064
1186
  const tiktokPixelEventParamsJson = (_b = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.tiktokPixel) === null || _b === void 0 ? void 0 : _b[eventName];
1065
1187
  if ((tiktokPixelEventParamsJson === null || tiktokPixelEventParamsJson === void 0 ? void 0 : tiktokPixelEventParamsJson.event_name) &&
1066
1188
  typeof (window === null || window === void 0 ? void 0 : window.ttq) === 'object' &&
1067
1189
  typeof ((_c = window === null || window === void 0 ? void 0 : window.ttq) === null || _c === void 0 ? void 0 : _c.track) === 'function') {
1068
1190
  (_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));
1069
- bffCollectEvent({
1070
- eventName: tiktokPixelEventParamsJson === null || tiktokPixelEventParamsJson === void 0 ? void 0 : tiktokPixelEventParamsJson.event_name,
1071
- eventSource: 'TikTok Pixel',
1072
- eventInfo: getEventParamsByJson(tiktokPixelEventParamsJson, product)
1073
- });
1074
1191
  }
1075
1192
  }
1076
1193
  const snapchatPixelEventParamsJson = (_e = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.snapchatPixel) === null || _e === void 0 ? void 0 : _e[eventName];
1077
1194
  if ((snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name) && typeof (window === null || window === void 0 ? void 0 : window.snaptr) === 'function') {
1078
1195
  window === null || window === void 0 ? void 0 : window.snaptr('track', snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name, getEventParamsByJson(snapchatPixelEventParamsJson, product));
1079
- bffCollectEvent({
1080
- eventName: snapchatPixelEventParamsJson === null || snapchatPixelEventParamsJson === void 0 ? void 0 : snapchatPixelEventParamsJson.event_name,
1081
- eventSource: 'Snapchat Pixel',
1082
- eventInfo: getEventParamsByJson(snapchatPixelEventParamsJson, product)
1083
- });
1084
1196
  }
1085
1197
  const converApiEventParamsJson = (_f = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.converApi) === null || _f === void 0 ? void 0 : _f[eventName];
1086
1198
  if (enabledMetaConversionApi && converApiEventParamsJson) {