@jctrans-materials/shared 1.0.37-beta.0 → 1.0.37-beta.10

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.d.ts CHANGED
@@ -35,6 +35,11 @@ export declare interface BaseResponse<T = any> {
35
35
  [k: string]: any;
36
36
  }
37
37
 
38
+ /**
39
+ * 事件拦截回调
40
+ */
41
+ export declare type BeforeSendCallback = (event: GioEvent) => GioEvent;
42
+
38
43
  /**
39
44
  * 验证验证码绑定第三方应用
40
45
  * @param data
@@ -120,6 +125,11 @@ export declare function completeCompRedirectApi(data: any): Promise<any>;
120
125
  */
121
126
  export declare function completeJoinCompanyRedirectApi(data: any): Promise<any>;
122
127
 
128
+ /**
129
+ * 配置值回调
130
+ */
131
+ export declare type ConfigCallback<T = GioConfigValue> = (value: T) => void;
132
+
123
133
  export declare function createRequest(driver: RequestDriver, options?: RequestOptions): RequestAdapter;
124
134
 
125
135
  export declare const currentConfig: {
@@ -138,6 +148,11 @@ export declare const currentConfig: {
138
148
  */
139
149
  export declare function Decrypt(word: string): string;
140
150
 
151
+ /**
152
+ * 设备 ID 回调
153
+ */
154
+ export declare type DeviceIdCallback = (deviceId: string) => void;
155
+
141
156
  export declare type DisplayInfo = "Continent" | "Country" | "Province" | "City" | "Seaport" | "Airport";
142
157
 
143
158
  export declare const emitter: Emitter<ModalEvents>;
@@ -368,6 +383,25 @@ export declare const getSharedConfig: () => {
368
383
  */
369
384
  export declare function getToken(): string | undefined;
370
385
 
386
+ /**
387
+ * 安全的 tracker 访问函数
388
+ * 在组件中使用时,可以避免 SSR 阶段解构报错
389
+ *
390
+ * @example
391
+ * ```typescript
392
+ * // ❌ 错误:可能在 SSR 阶段解构报错
393
+ * const { $gio } = useNuxtApp();
394
+ *
395
+ * // ✅ 正确:使用 getTracker 安全访问
396
+ * const { $gio } = useNuxtApp();
397
+ * const gio = getTracker($gio);
398
+ * if (gio) {
399
+ * gio.track('event');
400
+ * }
401
+ * ```
402
+ */
403
+ export declare function getTracker<T extends GioTracker>(trackerInstance?: T): T | null;
404
+
371
405
  /**
372
406
  * 获取用户手机号邮箱信息
373
407
  * @param data
@@ -375,6 +409,341 @@ export declare function getToken(): string | undefined;
375
409
  */
376
410
  export declare function getUserInfoApi(data: any): Promise<any>;
377
411
 
412
+ /**
413
+ * GIO 属性对象
414
+ */
415
+ export declare interface GioAttributes {
416
+ [key: string]: GioAttributeValue;
417
+ }
418
+
419
+ /**
420
+ * GrowingIO SDK 类型定义
421
+ * 严格约束传参,防止业务侧传入深层嵌套对象导致埋点失效
422
+ */
423
+ /**
424
+ * GIO 属性值类型
425
+ * 只允许 string, number 或一维数组
426
+ * 文档限制:key ≤ 100 字符,value ≤ 1000 字符,数组元素 ≤ 100 个
427
+ */
428
+ export declare type GioAttributeValue = string | number | Array<string | number>;
429
+
430
+ /**
431
+ * 回调函数类型
432
+ */
433
+ export declare type GioCallback = () => void;
434
+
435
+ /**
436
+ * SDK 配置项类型
437
+ */
438
+ export declare type GioConfigKey = 'dataCollect' | 'debug' | 'serverUrl' | 'trackPage' | 'forceLogin' | 'idMapping' | 'version';
439
+
440
+ /**
441
+ * SDK 配置值类型
442
+ */
443
+ export declare type GioConfigValue = boolean | string | number | object;
444
+
445
+ /**
446
+ * GIO 事件对象(用于拦截回调)
447
+ */
448
+ export declare interface GioEvent {
449
+ eventType: 'CUSTOM' | 'PAGE' | 'VISIT' | string;
450
+ eventName: string;
451
+ attributes: GioAttributes;
452
+ path: string;
453
+ query: string;
454
+ title: string;
455
+ [key: string]: any;
456
+ }
457
+
458
+ /**
459
+ * 通用属性对象(支持静态值和动态函数)
460
+ * 动态函数支持返回:string, number, boolean, Array<string | number>
461
+ */
462
+ export declare interface GioGeneralProps {
463
+ [key: string]: GioAttributeValue | (() => string | number | boolean | Array<string | number>);
464
+ }
465
+
466
+ /**
467
+ * GrowingIO SDK 初始化配置项
468
+ * 参考官方文档:https://help-center.growingio.com/@sdk-v4/docs/webjs/initSettings.html
469
+ */
470
+ export declare interface GioInitOptions {
471
+ /** 应用版本号,默认 '1.0.0' */
472
+ version?: string;
473
+ /** 数据上报服务端地址,SaaS 客户默认 https://napi.growingio.com,OP 私有部署需填写 */
474
+ serverUrl?: string;
475
+ /** 是否开启调试模式,默认 false。开启后在浏览器控制台输出日志 */
476
+ debug?: boolean;
477
+ /** 是否开启数据采集,默认 true。关闭后不会采集和上报事件 */
478
+ dataCollect?: boolean;
479
+ /** 是否自动采集页面访问事件,默认 true。关闭后需手动 sendPage */
480
+ trackPage?: boolean;
481
+ /** 是否开启强制登录模式,默认 false。微信公众号 H5 需要使用 openId/unionId 时设为 true */
482
+ forceLogin?: boolean;
483
+ /** 是否开启多用户身份上报(ID-MAPPING),默认 false。需要设置 userKey 时开启 */
484
+ idMapping?: boolean;
485
+ /** 指定打通的移动端包名(APP 内嵌页面场景) */
486
+ packageName?: string;
487
+ /** 平台类型,默认 'Web'。其他场景:wxwv(微信公众号), alip(支付宝小程序), iOS, Android 等 */
488
+ platform?: 'Web' | 'wxwv' | 'alip' | 'baidup' | 'qq' | 'bytedance' | 'kuaishoup' | 'jdp' | 'Android' | 'iOS' | 'Minp';
489
+ /** 自定义 cookie 存储的域,默认为当前站点的一级域名 */
490
+ cookieDomain?: string;
491
+ /** SDK 信息持久化存储类型,默认 'cookie'。可选 'localStorage' */
492
+ storageType?: 'cookie' | 'localStorage';
493
+ /** 是否开启增强型 UA,默认 true。开启后能更准确识别 Windows 11,但会增加上报数据大小 */
494
+ extraUA?: boolean;
495
+ /** 是否开启 hash 解析,默认 false。SPA 使用 hash 路由时设为 true */
496
+ hashtag?: boolean;
497
+ /** 是否采集爬虫环境数据,默认 true */
498
+ trackBot?: boolean;
499
+ /** 访问事件是否使用原始进入数据,默认 true。关闭可解决跳出率异常问题 */
500
+ originalSource?: boolean;
501
+ /** 与小程序打通时是否自动删除 gioInfo 参数,默认 true */
502
+ rewriteQuery?: boolean;
503
+ /** 数据上报优先方式,默认 'beacon'。可选 'xhr', 'image' */
504
+ sendType?: 'beacon' | 'xhr' | 'image';
505
+ /** 请求超时时长(毫秒),默认 5000。仅对 XHR 和 image 生效 */
506
+ requestTimeout?: number;
507
+ /** 上报忽略字段,如 ['screenHeight', 'screenWidth'] */
508
+ ignoreFields?: string[];
509
+ /** 曝光比例(0-1),与曝光插件结合使用,默认 0 */
510
+ impressionScale?: number;
511
+ /** 与小程序打通时的配置项,SDK >= 4.3.0 支持 */
512
+ embeddedAdapter?: {
513
+ /** 圈选服务器地址,SaaS 客户不需要配置 */
514
+ circleServerUrl?: string;
515
+ };
516
+ /** 允许传入其他未列出的配置项 */
517
+ [key: string]: any;
518
+ }
519
+
520
+ /**
521
+ * 页面属性对象
522
+ */
523
+ export declare interface GioPageProps {
524
+ path: string;
525
+ query: string;
526
+ title: string;
527
+ }
528
+
529
+ /**
530
+ * 事件计时器 ID 类型
531
+ */
532
+ export declare type GioTimerId = string;
533
+
534
+ declare class GioTracker {
535
+ private hasInitCalled;
536
+ private dispatcher;
537
+ private dispatcherLoading;
538
+ private pendingActions;
539
+ private get shouldValidateAttributes();
540
+ private flushPendingActions;
541
+ private ensureDispatcher;
542
+ private dispatch;
543
+ /**
544
+ * 检查 SDK 是否已初始化
545
+ * 在 SSR 环境下返回 false
546
+ */
547
+ isInitialized(): boolean;
548
+ /**
549
+ * 检查当前是否在浏览器环境中
550
+ * 可用于组件中判断是否执行 GIO 相关逻辑
551
+ */
552
+ isBrowser(): boolean;
553
+ /**
554
+ * 初始化 GrowingIO SDK
555
+ * 使用官方 gio-webjs-sdk npm 包进行初始化
556
+ *
557
+ * @param accountId - 账户 ID(从 GrowingIO 平台获取)
558
+ * @param dataSourceId - 数据源 ID(从 GrowingIO 平台获取)
559
+ * @param options - 初始化配置项
560
+ * @param callback - 可选回调函数
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * // 基础初始化
565
+ * tracker.init('your-account-id', 'your-datasource-id', {
566
+ * debug: true,
567
+ * trackPage: false,
568
+ * });
569
+ *
570
+ * // 微信公众号 H5(需要 forceLogin)
571
+ * tracker.init('accountId', 'dataSourceId', 'appId', {
572
+ * forceLogin: true,
573
+ * debug: true,
574
+ * });
575
+ *
576
+ * // 私有化部署
577
+ * tracker.init('accountId', 'dataSourceId', {
578
+ * serverUrl: 'https://your-growingio-server.com',
579
+ * debug: true,
580
+ * });
581
+ * ```
582
+ */
583
+ init(accountId: string, dataSourceId: string, options?: GioInitOptions, callback?: GioCallback): void;
584
+ /**
585
+ * 初始化 GrowingIO SDK(微信公众号 H5 场景,需要 appId)
586
+ */
587
+ init(accountId: string, dataSourceId: string, appId: string, options: GioInitOptions, callback?: GioCallback): void;
588
+ /**
589
+ * 动态修改配置
590
+ */
591
+ setOption<T extends GioConfigValue>(key: GioConfigKey, value: T, callback?: GioCallback): void;
592
+ /**
593
+ * 获取 SDK 当前配置
594
+ */
595
+ getOption<T extends GioConfigValue>(key: GioConfigKey | "", callback: ConfigCallback<T>): void;
596
+ /**
597
+ * 获取 SDK 版本号
598
+ */
599
+ getVersion(callback: ConfigCallback<string>): void;
600
+ /**
601
+ * 获取增强型 UserAgent
602
+ */
603
+ getUserAgent(callback: (userAgent: string) => void): void;
604
+ /**
605
+ * 设置访问用户 ID(匿名用户 ID/设备 ID)
606
+ * 仅可合法调用一次,多次调用无效
607
+ * 需要在初始化时将 forceLogin 设置为 true
608
+ */
609
+ identify(deviceId: string, callback?: GioCallback): void;
610
+ /**
611
+ * 获取访问用户 ID(设备 ID)
612
+ */
613
+ getDeviceId(callback: DeviceIdCallback): void;
614
+ /**
615
+ * 设置登录用户 ID
616
+ * 需要在初始化时设置 idMapping: true
617
+ * 调用方式:
618
+ * - setUserId(userId, callback?)
619
+ * - setUserId(userId, userKey, callback?)
620
+ * @param userId 用户 ID,长度 0 < length ≤ 1000
621
+ */
622
+ /**
623
+ * 设置登录用户 ID(无 userKey 场景)
624
+ */
625
+ setUserId(userId: string | number, callback?: GioCallback): void;
626
+ /**
627
+ * 设置登录用户 ID(ID-MAPPING 场景)
628
+ * @param userKey 用户 ID 类型标识(用于 ID-MAPPING)
629
+ */
630
+ setUserId(userId: string | number, userKey: string | number, callback?: GioCallback): void;
631
+ /**
632
+ * 清除登录用户 ID(登出时调用)
633
+ */
634
+ clearUserId(callback?: GioCallback): void;
635
+ /**
636
+ * 发送埋点事件
637
+ * @param eventName 事件名称(需在平台预先配置)
638
+ * @param attributes 事件属性
639
+ * @param callback 回调函数
640
+ */
641
+ track(eventName: string, attributes?: GioAttributes, callback?: GioCallback): void;
642
+ /**
643
+ * 设置用户属性(登录用户身份)
644
+ * @param userAttributes 用户属性对象
645
+ * @param callback 回调函数
646
+ */
647
+ setUserAttributes(userAttributes: GioUserAttributes, callback?: GioCallback): void;
648
+ /**
649
+ * 设置全局通用属性(对所有事件生效)
650
+ * 支持静态值和动态函数
651
+ * 可多次调用,同名属性会被覆盖(动态值优先级更高)
652
+ */
653
+ setGeneralProps(props: GioGeneralProps, callback?: GioCallback): void;
654
+ /**
655
+ * 清除全局通用属性
656
+ * @param propertyNames 要清除的属性名数组,传空数组清空所有
657
+ */
658
+ clearGeneralProps(propertyNames?: string[], callback?: GioCallback): void;
659
+ /**
660
+ * 设置页面变更回调
661
+ * SDK 触发页面变更时会调用该回调
662
+ * 建议配合关闭 trackPage 后手动 sendPage 使用
663
+ */
664
+ setPageListener(callback: PageListenerCallback): void;
665
+ /**
666
+ * 设置页面属性
667
+ * 仅生效于当前页面的页面浏览事件和无埋点事件
668
+ * 仅在关闭 trackPage 时可用
669
+ */
670
+ setPageAttributes(props: GioAttributes, callback?: GioCallback): void;
671
+ /**
672
+ * 清除页面属性
673
+ * @param propertyNames 要清除的属性名数组,传空数组清空所有
674
+ */
675
+ clearPageAttributes(propertyNames?: string[], callback?: GioCallback): void;
676
+ /**
677
+ * 手动发送页面访问事件
678
+ * 仅在关闭 trackPage 时可用
679
+ * ⚠️ 危险操作,容易导致页面访问时长、跳出率等数据异常
680
+ * 强烈建议配合 setPageListener 使用
681
+ */
682
+ sendPage(options?: {
683
+ title?: string;
684
+ }, callback?: GioCallback): void;
685
+ /**
686
+ * 设置事件发送前拦截回调
687
+ * 可修改 path、query、title、attributes
688
+ * SDK 版本 >= 4.2.2 支持
689
+ */
690
+ setBeforeSendListener(callback: BeforeSendCallback): void;
691
+ /**
692
+ * 初始化事件计时器
693
+ * @param eventName 事件名称
694
+ * @returns timerId 计时器唯一标识
695
+ */
696
+ trackTimerStart(eventName: string, callback?: (timerId: GioTimerId) => void): void;
697
+ /**
698
+ * 暂停事件计时器
699
+ */
700
+ trackTimerPause(timerId: GioTimerId, callback?: GioCallback): void;
701
+ /**
702
+ * 恢复事件计时器
703
+ */
704
+ trackTimerResume(timerId: GioTimerId, callback?: GioCallback): void;
705
+ /**
706
+ * 停止事件计时器
707
+ * 会自动发送事件并删除计时器
708
+ * @param timerId 计时器 ID
709
+ * @param attributes 事件属性(event_duration 会自动添加)
710
+ */
711
+ trackTimerEnd(timerId: GioTimerId, attributes?: GioAttributes, callback?: GioCallback): void;
712
+ /**
713
+ * 删除单个事件计时器
714
+ * 不会发送事件
715
+ */
716
+ removeTimer(timerId: GioTimerId, callback?: GioCallback): void;
717
+ /**
718
+ * 清除所有事件计时器
719
+ * 不会发送事件
720
+ * 注意:官方文档中此方法不接受参数
721
+ */
722
+ clearTrackTimer(callback?: GioCallback): void;
723
+ /**
724
+ * 注册插件
725
+ * 用于扩展功能(如性能监控、曝光追踪等)
726
+ * @param plugins 插件数组
727
+ * @param callback 可选回调函数
728
+ */
729
+ registerPlugins(plugins: any[], callback?: (registeredPlugins: any[]) => void): void;
730
+ /**
731
+ * 校验属性对象(开发环境使用)
732
+ * - 检查 key 长度 ≤ 100
733
+ * - 检查 value 长度 ≤ 1000
734
+ * - 检查数组长度 ≤ 100
735
+ * - 检查是否包含嵌套对象
736
+ */
737
+ private _validateAttributes;
738
+ }
739
+
740
+ /**
741
+ * 用户属性对象
742
+ */
743
+ export declare interface GioUserAttributes {
744
+ [key: string]: GioAttributeValue;
745
+ }
746
+
378
747
  export declare const HasLoginKey = "hasLogin";
379
748
 
380
749
  /**
@@ -393,6 +762,18 @@ declare interface IResponse<T> {
393
762
  data: T;
394
763
  }
395
764
 
765
+ /**
766
+ * 检查 GIO 是否可用(浏览器环境且已初始化)
767
+ *
768
+ * @example
769
+ * ```typescript
770
+ * if (isGioAvailable($gio)) {
771
+ * $gio.track('event');
772
+ * }
773
+ * ```
774
+ */
775
+ export declare function isGioAvailable(trackerInstance?: typeof tracker): boolean;
776
+
396
777
  /**
397
778
  * 判断域名是否为 IP
398
779
  */
@@ -752,6 +1133,11 @@ export declare type ModalEvents = Record<ActionKeys, any> & {
752
1133
 
753
1134
  export declare type ModalOpenCallback = () => void;
754
1135
 
1136
+ /**
1137
+ * 页面变更回调
1138
+ */
1139
+ export declare type PageListenerCallback = (pageProps: GioPageProps) => void;
1140
+
755
1141
  export declare interface PageParams {
756
1142
  current?: number;
757
1143
  size?: number;
@@ -1073,6 +1459,8 @@ export declare interface TokenData {
1073
1459
  */
1074
1460
  export declare const TokenKey = "JC-JAVA-Token-Root";
1075
1461
 
1462
+ export declare const tracker: GioTracker;
1463
+
1076
1464
  /** 归一化之后的实体 */
1077
1465
  export declare interface UnifiedItem {
1078
1466
  id: number | string;