@coframe/sdk 1.1.0 → 1.1.1
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 +218 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# @coframe/sdk
|
|
2
|
+
|
|
3
|
+
Bring your UX to life with AI-powered optimization and personalization.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@coframe/sdk)
|
|
6
|
+
|
|
7
|
+
Coframe helps you personalize and optimize your site with experiments, targeting, anti-flicker rendering, analytics callbacks, and conversion tracking.
|
|
8
|
+
|
|
9
|
+
This package exposes three entry points:
|
|
10
|
+
|
|
11
|
+
- `@coframe/sdk` for the browser runtime
|
|
12
|
+
- `@coframe/sdk/server` for SSR and edge prefetching
|
|
13
|
+
- `@coframe/sdk/react` for React apps
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @coframe/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If you use the React entry point, install React as well:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install react@">=17"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
### Browser
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { init } from '@coframe/sdk'
|
|
33
|
+
|
|
34
|
+
await init({
|
|
35
|
+
config: {
|
|
36
|
+
projectId: 'your-project-id',
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`init()` accepts `config` and optional `projectData`, so npm consumers do not need to manually populate `window.CFQ` first.
|
|
42
|
+
|
|
43
|
+
### React
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { CoframeProvider } from '@coframe/sdk/react'
|
|
47
|
+
import type { CoframeConfig } from '@coframe/sdk'
|
|
48
|
+
|
|
49
|
+
const projectConfig: CoframeConfig = {
|
|
50
|
+
projectId: 'your-project-id',
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default function App({ children }) {
|
|
54
|
+
return (
|
|
55
|
+
<CoframeProvider projectConfig={projectConfig}>
|
|
56
|
+
{children}
|
|
57
|
+
</CoframeProvider>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Mount the provider once at the root of your app. It initializes the SDK on mount and keeps treatment state available to child components.
|
|
63
|
+
|
|
64
|
+
### Server prefetch
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { getProjectData } from '@coframe/sdk/server'
|
|
68
|
+
|
|
69
|
+
const serverData = await getProjectData({
|
|
70
|
+
projectId: 'your-project-id',
|
|
71
|
+
currentUrl: request.url,
|
|
72
|
+
headers: request.headers,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const projectData = serverData?.project
|
|
76
|
+
? {
|
|
77
|
+
project: serverData.project,
|
|
78
|
+
personalizationHeaders: serverData.personalizationHeaders,
|
|
79
|
+
}
|
|
80
|
+
: undefined
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Pass `projectData` into `<CoframeProvider>` to avoid the extra client-side project fetch and personalize on first paint.
|
|
84
|
+
|
|
85
|
+
## Common React usage
|
|
86
|
+
|
|
87
|
+
### Read variants in components
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
'use client'
|
|
91
|
+
|
|
92
|
+
import { useCoframeVariant } from '@coframe/sdk/react'
|
|
93
|
+
|
|
94
|
+
export function CheckoutPage() {
|
|
95
|
+
const variant = useCoframeVariant('checkout_flow_v2')
|
|
96
|
+
|
|
97
|
+
if (variant === undefined) return <CheckoutSkeleton />
|
|
98
|
+
if (variant === 'treatment') return <NewCheckout />
|
|
99
|
+
return <OldCheckout />
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Other hooks:
|
|
104
|
+
|
|
105
|
+
- `useCoframeTreatments()` returns all treatments or `null` while init is in flight
|
|
106
|
+
- `useCoframeTreatment(experimentId)` returns one treatment payload
|
|
107
|
+
|
|
108
|
+
### Track conversions
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
'use client'
|
|
112
|
+
|
|
113
|
+
import { trackConversion } from '@coframe/sdk'
|
|
114
|
+
|
|
115
|
+
export function CheckoutButton() {
|
|
116
|
+
return (
|
|
117
|
+
<button
|
|
118
|
+
onClick={() =>
|
|
119
|
+
trackConversion({
|
|
120
|
+
metricName: 'checkout_complete',
|
|
121
|
+
value: 99.95,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
>
|
|
125
|
+
Complete checkout
|
|
126
|
+
</button>
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Analytics and log callbacks
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
import { CoframeProvider } from '@coframe/sdk/react'
|
|
135
|
+
|
|
136
|
+
<CoframeProvider
|
|
137
|
+
projectConfig={projectConfig}
|
|
138
|
+
onTreatmentAppliedBulkV2={(data) => {
|
|
139
|
+
for (const treatment of data) {
|
|
140
|
+
analytics.track('Experiment Viewed', treatment)
|
|
141
|
+
}
|
|
142
|
+
}}
|
|
143
|
+
onLog={(payload) => {
|
|
144
|
+
console.error('[coframe]', payload.monitoring_type, payload)
|
|
145
|
+
}}
|
|
146
|
+
>
|
|
147
|
+
{children}
|
|
148
|
+
</CoframeProvider>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Outside React, you can also register callbacks from the main entry:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { onLog, onTreatmentApplied } from '@coframe/sdk'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Next.js / Remix / SSR notes
|
|
158
|
+
|
|
159
|
+
- `getProjectData()` is intended for SSR and edge runtimes that can provide a full request URL and request headers.
|
|
160
|
+
- In Pages Router or Remix, per-request server loaders are the most straightforward place to call it.
|
|
161
|
+
- In App Router, keep the provider mounted in a persistent shell, but be careful about request URL handling in `layout.tsx` if you need query-sensitive SSR targeting.
|
|
162
|
+
|
|
163
|
+
## Package layout
|
|
164
|
+
|
|
165
|
+
The published tarball contains only `dist/pkg/`:
|
|
166
|
+
|
|
167
|
+
```text
|
|
168
|
+
dist/pkg/
|
|
169
|
+
├── cf.esm.mjs cf.cjs.js types/index.d.ts ← @coframe/sdk
|
|
170
|
+
├── server.esm.mjs server.cjs.js types/server.d.ts ← @coframe/sdk/server
|
|
171
|
+
└── react.esm.mjs react.cjs.js types/react.d.ts ← @coframe/sdk/react
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## TypeScript compatibility
|
|
175
|
+
|
|
176
|
+
The package publishes typed subpath exports for:
|
|
177
|
+
|
|
178
|
+
- `@coframe/sdk`
|
|
179
|
+
- `@coframe/sdk/server`
|
|
180
|
+
- `@coframe/sdk/react`
|
|
181
|
+
|
|
182
|
+
If your app uses an older TypeScript module resolution mode, prefer moving to one of:
|
|
183
|
+
|
|
184
|
+
- `moduleResolution: "bundler"`
|
|
185
|
+
- `moduleResolution: "node16"`
|
|
186
|
+
- `moduleResolution: "nodenext"`
|
|
187
|
+
|
|
188
|
+
Modern resolution modes handle package subpath exports more reliably.
|
|
189
|
+
|
|
190
|
+
## API overview
|
|
191
|
+
|
|
192
|
+
### `@coframe/sdk`
|
|
193
|
+
|
|
194
|
+
- `init(options)`
|
|
195
|
+
- `trackConversion(payload)`
|
|
196
|
+
- `setConsent(enabled)`
|
|
197
|
+
- `setUserToken(token)`
|
|
198
|
+
- `setFeatureFlag(enabled)`
|
|
199
|
+
- `setPageHydrated()`
|
|
200
|
+
- `onTreatmentApplied(...)`
|
|
201
|
+
- `onLog(...)`
|
|
202
|
+
|
|
203
|
+
### `@coframe/sdk/server`
|
|
204
|
+
|
|
205
|
+
- `getProjectData(options)`
|
|
206
|
+
|
|
207
|
+
Returns a payload that may include:
|
|
208
|
+
|
|
209
|
+
- `project`
|
|
210
|
+
- `personalizationHeaders`
|
|
211
|
+
- `antiflickerCSS`
|
|
212
|
+
|
|
213
|
+
### `@coframe/sdk/react`
|
|
214
|
+
|
|
215
|
+
- `CoframeProvider`
|
|
216
|
+
- `useCoframeTreatments()`
|
|
217
|
+
- `useCoframeTreatment(experimentId)`
|
|
218
|
+
- `useCoframeVariant(experimentId)`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coframe/sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Coframe optimizes your website using generative AI to deliver maximum impact to your audience and allow your site to improve itself as you sleep. Learn more at coframe.com.",
|
|
5
5
|
"main": "dist/pkg/cf.cjs.js",
|
|
6
6
|
"module": "dist/pkg/cf.esm.mjs",
|