@ai-ad-network/frontend-sdk 1.0.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.
- package/README.md +272 -0
- package/dist/index.esm.js +5890 -0
- package/dist/index.js +40 -0
- package/dist/style.css +1 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# AI Ad Network Frontend SDK
|
|
2
|
+
|
|
3
|
+
> React SDK for displaying native, AI-powered ads in your application
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Intent-based Ad Delivery** - Ads matched to user intent
|
|
8
|
+
- 🎨 **Native Ad Formats** - Action Cards, Suffix, Follow-up, Sponsored Sources
|
|
9
|
+
- 📊 **Built-in Analytics** - Impression and click tracking
|
|
10
|
+
- ⚡ **Optimized Performance** - SWR caching, Intersection Observer
|
|
11
|
+
- 🎭 **Headless Design** - Full customization with CSS variables
|
|
12
|
+
- 📱 **Responsive** - Works on all screen sizes
|
|
13
|
+
- 🔐 **Built-in Authentication** - API key support for secure access
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @ai-ad-network/frontend-sdk
|
|
19
|
+
# or
|
|
20
|
+
yarn add @ai-ad-network/frontend-sdk
|
|
21
|
+
# or
|
|
22
|
+
pnpm add @ai-ad-network/frontend-sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { AdProvider, useAiAds, ActionCardAd } from '@ai-ad-network/frontend-sdk';
|
|
29
|
+
|
|
30
|
+
function App() {
|
|
31
|
+
return (
|
|
32
|
+
<AdProvider
|
|
33
|
+
config={{
|
|
34
|
+
apiBaseUrl: 'https://your-ad-network.com/api/v1',
|
|
35
|
+
apiKey: 'ak_your_tenant_your_key', // Get from admin
|
|
36
|
+
debug: process.env.NODE_ENV === 'development',
|
|
37
|
+
}}
|
|
38
|
+
>
|
|
39
|
+
<ChatApp />
|
|
40
|
+
</AdProvider>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function ChatApp() {
|
|
45
|
+
const [query, setQuery] = useState('');
|
|
46
|
+
const [response, setResponse] = useState('');
|
|
47
|
+
const [isFinished, setIsFinished] = useState(false);
|
|
48
|
+
|
|
49
|
+
const { ads, isLoading } = useAiAds(query, response, isFinished, {
|
|
50
|
+
formats: ['action_card', 'suffix'],
|
|
51
|
+
placement: 'post_response',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div>
|
|
56
|
+
{/* Your chat UI */}
|
|
57
|
+
{ads.map(ad => (
|
|
58
|
+
<ActionCardAd key={ad.id} ad={ad} />
|
|
59
|
+
))}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
### AdProvider Props
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
<AdProvider
|
|
71
|
+
config={{
|
|
72
|
+
// API Configuration
|
|
73
|
+
apiBaseUrl?: string; // Default: '/api/v1'
|
|
74
|
+
apiKey?: string; // Your API key (format: ak_tenant_key)
|
|
75
|
+
|
|
76
|
+
// Ad Behavior
|
|
77
|
+
defaultFormats?: string[]; // Default: ['action_card', 'suffix']
|
|
78
|
+
defaultPlacement?: string; // Default: 'post_response'
|
|
79
|
+
enabled?: boolean; // Default: true
|
|
80
|
+
|
|
81
|
+
// Debugging
|
|
82
|
+
debug?: boolean; // Default: false
|
|
83
|
+
headers?: Record<string, string>;
|
|
84
|
+
|
|
85
|
+
// Performance
|
|
86
|
+
timeout?: number; // Default: 5000
|
|
87
|
+
maxRetries?: number; // Default: 2
|
|
88
|
+
|
|
89
|
+
// Tracking
|
|
90
|
+
userId?: string;
|
|
91
|
+
}}
|
|
92
|
+
>
|
|
93
|
+
{children}
|
|
94
|
+
</AdProvider>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Components
|
|
98
|
+
|
|
99
|
+
### ActionCardAd
|
|
100
|
+
|
|
101
|
+
Rich media card for product ads.
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
<ActionCardAd
|
|
105
|
+
ad={ad}
|
|
106
|
+
variant="horizontal" // 'horizontal' | 'vertical' | 'compact'
|
|
107
|
+
onClick={() => console.log('Clicked!')}
|
|
108
|
+
/>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### SuffixAd
|
|
112
|
+
|
|
113
|
+
Text-based ad appended to AI responses.
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
<SuffixAd
|
|
117
|
+
ad={ad}
|
|
118
|
+
aiContext={aiResponse}
|
|
119
|
+
variant="block" // 'block' | 'inline' | 'minimal'
|
|
120
|
+
/>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### FollowUpAd
|
|
124
|
+
|
|
125
|
+
Ad disguised as a follow-up question.
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
<FollowUpAd
|
|
129
|
+
ad={ad}
|
|
130
|
+
variant="bubble" // 'bubble' | 'pill' | 'underline'
|
|
131
|
+
/>
|
|
132
|
+
|
|
133
|
+
// Or mix with regular questions
|
|
134
|
+
<FollowUpAdMixed
|
|
135
|
+
questions={['How does it work?', 'What are the features?']}
|
|
136
|
+
ad={ad}
|
|
137
|
+
adPosition={1}
|
|
138
|
+
onQuestionClick={(q) => console.log(q)}
|
|
139
|
+
/>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### SponsoredSource
|
|
143
|
+
|
|
144
|
+
Ad in citation/source list format.
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
<SponsoredSource
|
|
148
|
+
ad={ad}
|
|
149
|
+
variant="card" // 'card' | 'list-item' | 'minimal'
|
|
150
|
+
/>
|
|
151
|
+
|
|
152
|
+
// Or mix with regular sources
|
|
153
|
+
<SourceListWithAds
|
|
154
|
+
sources={[{ title: 'Wikipedia', url: '...' }]}
|
|
155
|
+
ad={ad}
|
|
156
|
+
adPosition={1}
|
|
157
|
+
/>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Hooks
|
|
161
|
+
|
|
162
|
+
### useAiAds
|
|
163
|
+
|
|
164
|
+
Main hook for fetching ads.
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
const {
|
|
168
|
+
ads, // Array of KoahAd objects
|
|
169
|
+
intent, // Intent analysis result
|
|
170
|
+
routing, // Routing information
|
|
171
|
+
isLoading, // Loading state
|
|
172
|
+
error, // Error object
|
|
173
|
+
refetch, // Manual refetch function
|
|
174
|
+
} = useAiAds(
|
|
175
|
+
userQuery, // User's question
|
|
176
|
+
aiResponse, // AI's response
|
|
177
|
+
isStreamFinished, // Boolean
|
|
178
|
+
{
|
|
179
|
+
enabled: true,
|
|
180
|
+
formats: ['action_card', 'suffix'],
|
|
181
|
+
placement: 'post_response',
|
|
182
|
+
apiBaseUrl: '/api/v1',
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### useAdTracking
|
|
188
|
+
|
|
189
|
+
Tracking hook for impressions and clicks.
|
|
190
|
+
|
|
191
|
+
```tsx
|
|
192
|
+
const {
|
|
193
|
+
impressionRef, // Ref to attach to ad element
|
|
194
|
+
handleClick, // Click handler
|
|
195
|
+
hasImpressed, // Boolean
|
|
196
|
+
isIntersecting, // Boolean
|
|
197
|
+
} = useAdTracking(ad, {
|
|
198
|
+
intersectionThreshold: 0.5,
|
|
199
|
+
minViewTimeMs: 1000,
|
|
200
|
+
onClick: () => console.log('Clicked'),
|
|
201
|
+
onImpression: () => console.log('Viewed'),
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Styling
|
|
206
|
+
|
|
207
|
+
### CSS Variables
|
|
208
|
+
|
|
209
|
+
Customize the appearance with CSS variables:
|
|
210
|
+
|
|
211
|
+
```css
|
|
212
|
+
:root {
|
|
213
|
+
--ad-primary: #3b82f6;
|
|
214
|
+
--ad-secondary: #8b5cf6;
|
|
215
|
+
--ad-background: #f8fafc;
|
|
216
|
+
--ad-border: #e2e8f0;
|
|
217
|
+
--ad-text: #1e293b;
|
|
218
|
+
--ad-sponsored: #94a3b8;
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Utility Function
|
|
223
|
+
|
|
224
|
+
Use the `cn` function for conditional classes:
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import { cn } from '@ai-ad-network/frontend-sdk';
|
|
228
|
+
|
|
229
|
+
<div className={cn(
|
|
230
|
+
'base-class',
|
|
231
|
+
isActive && 'active-class',
|
|
232
|
+
'more-classes'
|
|
233
|
+
)} />
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## TypeScript Support
|
|
237
|
+
|
|
238
|
+
Full TypeScript support with exported types:
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
import type {
|
|
242
|
+
KoahAd,
|
|
243
|
+
IntentResult,
|
|
244
|
+
AdFormat,
|
|
245
|
+
AdSource,
|
|
246
|
+
RoutingInfo,
|
|
247
|
+
} from '@ai-ad-network/frontend-sdk';
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Integration Guide
|
|
251
|
+
|
|
252
|
+
For detailed Chatbox integration instructions, see [CHATBOX_INTEGRATION.md](./CHATBOX_INTEGRATION.md).
|
|
253
|
+
|
|
254
|
+
## Development
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
# Install dependencies
|
|
258
|
+
npm install
|
|
259
|
+
|
|
260
|
+
# Run Storybook
|
|
261
|
+
npm run storybook
|
|
262
|
+
|
|
263
|
+
# Build library
|
|
264
|
+
npm run build
|
|
265
|
+
|
|
266
|
+
# Run demo app
|
|
267
|
+
cd demo-app && npm run dev
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
MIT
|