@ai-ad-network/frontend-sdk 1.0.10 → 1.1.0

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.
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Analytics API - 框架无关的核心模块
3
+ *
4
+ * React 和 Universal SDK 共享此模块
5
+ * 用于发送广告展示和点击事件到 Backend Analytics API
6
+ */
7
+ import type { ImpressionEventData, ClickEventData, ImpressionResponse, ClickResponse, SessionConfig, AnalyticsSendConfig } from '../types/analytics';
8
+ /**
9
+ * Session 管理器
10
+ */
11
+ export declare class SessionManager {
12
+ private sessionKey;
13
+ private storage;
14
+ private memoryCache;
15
+ constructor(config?: SessionConfig);
16
+ /**
17
+ * 获取 Session ID
18
+ */
19
+ getSessionId(): string;
20
+ /**
21
+ * 重新生成 Session ID
22
+ */
23
+ regenerateSessionId(): string;
24
+ /**
25
+ * 生成新的 Session ID
26
+ */
27
+ private generateSessionId;
28
+ /**
29
+ * 清除 Session ID
30
+ */
31
+ clearSessionId(): void;
32
+ }
33
+ /**
34
+ * 获取 Session ID(便捷方法)
35
+ */
36
+ export declare const getSessionId: () => string;
37
+ /**
38
+ * Analytics 发送器类
39
+ */
40
+ export declare class AnalyticsSender {
41
+ private baseUrl;
42
+ private timeout;
43
+ private retryAttempts;
44
+ private retryDelay;
45
+ private debug;
46
+ constructor(config?: AnalyticsSendConfig);
47
+ /**
48
+ * 发送广告展示事件
49
+ */
50
+ trackImpression(data: ImpressionEventData): Promise<ImpressionResponse | null>;
51
+ /**
52
+ * 发送广告点击事件
53
+ */
54
+ trackClick(data: ClickEventData): Promise<ClickResponse | null>;
55
+ /**
56
+ * 通用请求发送方法(带重试)
57
+ */
58
+ private sendRequest;
59
+ /**
60
+ * 延迟辅助方法
61
+ */
62
+ private delay;
63
+ }
64
+ /**
65
+ * 发送广告展示事件(便捷方法)
66
+ */
67
+ export declare const trackAdImpression: (data: ImpressionEventData) => Promise<ImpressionResponse | null>;
68
+ /**
69
+ * 发送广告点击事件(便捷方法)
70
+ */
71
+ export declare const trackAdClick: (data: ClickEventData) => Promise<ClickResponse | null>;
72
+ /**
73
+ * 生成 View Token(用于展示去重)
74
+ */
75
+ export declare function generateViewToken(adId: string, sessionId: string): string;
76
+ /**
77
+ * 批量发送展示事件
78
+ */
79
+ export declare function trackImpressionsBatch(events: ImpressionEventData[]): Promise<(ImpressionResponse | null)[]>;
80
+ /**
81
+ * 批量发送点击事件
82
+ */
83
+ export declare function trackClicksBatch(events: ClickEventData[]): Promise<(ClickResponse | null)[]>;
84
+ /**
85
+ * 创建带配置的 Analytics 实例
86
+ */
87
+ export declare function createAnalytics(config: AnalyticsSendConfig): {
88
+ trackImpression: (data: ImpressionEventData) => Promise<ImpressionResponse | null>;
89
+ trackClick: (data: ClickEventData) => Promise<ClickResponse | null>;
90
+ };
91
+ /**
92
+ * 创建带配置的 Session 实例
93
+ */
94
+ export declare function createSession(config?: SessionConfig): {
95
+ getSessionId: () => string;
96
+ regenerateSessionId: () => string;
97
+ clearSessionId: () => void;
98
+ };
99
+ export type { ImpressionEventData, ClickEventData, ImpressionResponse, ClickResponse, SessionConfig, AnalyticsSendConfig, };
@@ -24,3 +24,37 @@ export interface ActionCardAdProps {
24
24
  * ```
25
25
  */
26
26
  export declare function ActionCardAd({ ad, variant, className, isLoading, onClick, onImpression, }: ActionCardAdProps): import("react/jsx-runtime").JSX.Element | null;
27
+ /**
28
+ * ActionCardAdV2 Props
29
+ */
30
+ export interface ActionCardAdV2Props {
31
+ ad: KoahAd;
32
+ variant?: ActionCardVariant;
33
+ className?: string;
34
+ isLoading?: boolean;
35
+ requestId: string;
36
+ slotId: string;
37
+ position: number;
38
+ totalAds: number;
39
+ onClick?: () => void;
40
+ onImpression?: () => void;
41
+ }
42
+ /**
43
+ * ActionCardAdV2 - Action Card component with Analytics API tracking
44
+ *
45
+ * Uses the new Analytics API (POST /api/v1/ads/impression, POST /api/v1/ads/click)
46
+ * instead of pixel tracking.
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * <ActionCardAdV2
51
+ * ad={ad}
52
+ * requestId="req-123"
53
+ * slotId="slot-action-card"
54
+ * position={0}
55
+ * totalAds={3}
56
+ * variant="horizontal"
57
+ * />
58
+ * ```
59
+ */
60
+ export declare function ActionCardAdV2({ ad, variant, className, isLoading, requestId, slotId, position, totalAds, onClick, onImpression, }: ActionCardAdV2Props): import("react/jsx-runtime").JSX.Element | null;
@@ -57,3 +57,41 @@ export declare function useClickTracking(ad: KoahAd | null, options?: Pick<AdTra
57
57
  handleClick: (e: React.MouseEvent | MouseEvent) => void;
58
58
  isClicking: boolean;
59
59
  };
60
+ /**
61
+ * useAdTrackingV2 - Hook for ad impression and click tracking with Analytics API
62
+ *
63
+ * Uses the new Analytics API (POST /api/v1/ads/impression, POST /api/v1/ads/click)
64
+ * instead of pixel tracking.
65
+ *
66
+ * @example
67
+ * ```tsx
68
+ * const { impressionRef, handleClick, hasImpressed } = useAdTrackingV2<HTMLDivElement>({
69
+ * ad,
70
+ * requestId: 'req-123',
71
+ * slotId: 'slot-action-card',
72
+ * position: 0,
73
+ * totalAds: 3,
74
+ * onImpression: () => console.log('Ad viewed!'),
75
+ * onClick: () => console.log('Ad clicked!'),
76
+ * });
77
+ * ```
78
+ */
79
+ export interface UseAdTrackingV2Params {
80
+ ad: KoahAd;
81
+ requestId: string;
82
+ slotId: string;
83
+ position: number;
84
+ totalAds: number;
85
+ intersectionThreshold?: number;
86
+ minViewTimeMs?: number;
87
+ onClick?: () => void;
88
+ onImpression?: () => void;
89
+ }
90
+ export interface UseAdTrackingV2Result {
91
+ impressionRef: React.RefObject<HTMLElement>;
92
+ handleClick: (e: React.MouseEvent) => void;
93
+ hasImpressed: boolean;
94
+ isIntersecting: boolean;
95
+ impressionTrackingSent: boolean;
96
+ }
97
+ export declare function useAdTrackingV2<T extends HTMLElement = HTMLDivElement>(params: UseAdTrackingV2Params): UseAdTrackingV2Result;
package/dist/index.d.ts CHANGED
@@ -27,10 +27,12 @@ export * from './types';
27
27
  export type { ConversationMessage, UserProfile, ResponseMetadata, SlotDecisionMetadata, } from './types';
28
28
  export { useAiAds, useAiAdsLazy, useAiAdsInfinite } from './hooks/useAiAds';
29
29
  export { useAdSlots, useAdSlot, useChatWithAds, adaptAdToKoahAd, fetchAds } from './hooks/useAdSlots';
30
- export { useAdTracking, useAdTrackingMultiple, useClickTracking, } from './hooks/useAdTracking';
30
+ export { useAdTracking, useAdTrackingV2, useAdTrackingMultiple, useClickTracking, } from './hooks/useAdTracking';
31
+ export type { UseAdTrackingV2Params, UseAdTrackingV2Result, } from './hooks/useAdTracking';
31
32
  export { useAdConfig } from './components/AdProvider';
32
33
  export { AdLogger, createLogger } from './components/AdProvider';
33
- export { ActionCardAd } from './components/ActionCardAd';
34
+ export { ActionCardAd, ActionCardAdV2 } from './components/ActionCardAd';
35
+ export type { ActionCardAdV2Props } from './components/ActionCardAd';
34
36
  export { EnhancedContent, EnhancedContentInline, EnhancedContentBlock, useEntityStats, } from './components/EnhancedContent';
35
37
  export { SuffixAd, SuffixAdInline, SuffixAdMinimal, } from './components/SuffixAd';
36
38
  export { FollowUpAd, FollowUpAdMixed, } from './components/FollowUpAd';
@@ -44,6 +46,8 @@ export { AdProvider } from './components/AdProvider';
44
46
  export { AdErrorBoundary, withAdErrorBoundary, AdErrorHandler, } from './components/AdErrorBoundary';
45
47
  export { AdSkeleton, InlineLoading, DotsLoading, FullPageLoading, } from './components/AdSkeleton';
46
48
  export { trackImpression, trackClick, hashUserId, checkFrequencyControl, recordImpression, getSessionId, } from './utils/tracking';
49
+ export { trackAdImpression, trackAdClick, trackImpressionsBatch, trackClicksBatch, generateViewToken, getSessionId as getAnalyticsSessionId, SessionManager, AnalyticsSender, createAnalytics, createSession, } from './analytics';
50
+ export type { ImpressionEventData, ClickEventData, ImpressionResponse, ClickResponse, AdAnalyticsInfo, AdSlotInfo, SessionConfig, AnalyticsSendConfig, } from './types/analytics';
47
51
  export { textWeaver, TextWeaver } from './utils/text-weaver';
48
52
  export { cn } from './lib/cn';
49
53
  export { ClientInfoCollector, getClientInfoCollector, getClientInfo, getDeviceInfo, getUserInfo, getAppInfo, getGeoInfo, getUserId, clearClientInfoCache, getClientInfoJSON, getClientInfoSummary, } from './client-info';