@encatch/web-sdk 1.0.0 → 1.1.0-beta.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 +4 -239
- package/dist/index.d.ts +12 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
# @encatch/web-sdk
|
|
2
2
|
|
|
3
|
-
A lightweight, type-safe JavaScript SDK for integrating Encatch
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Lightweight**: Minimal stub that loads the full implementation asynchronously
|
|
8
|
-
- **Type-safe**: Full TypeScript support with comprehensive type definitions
|
|
9
|
-
- **Queue-based**: Commands are queued until the SDK is ready, ensuring no calls are lost
|
|
10
|
-
- **Framework agnostic**: Works with any JavaScript framework or vanilla JS
|
|
3
|
+
A lightweight, type-safe JavaScript SDK for integrating Encatch platform into your web application.
|
|
11
4
|
|
|
12
5
|
## Installation
|
|
13
6
|
|
|
14
|
-
### NPM / Yarn / PNPM
|
|
15
|
-
|
|
16
7
|
```bash
|
|
17
8
|
npm install @encatch/web-sdk
|
|
18
9
|
# or
|
|
@@ -21,237 +12,11 @@ yarn add @encatch/web-sdk
|
|
|
21
12
|
pnpm add @encatch/web-sdk
|
|
22
13
|
```
|
|
23
14
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
```html
|
|
27
|
-
<script src="https://unpkg.com/@encatch/web-sdk/dist/encatch.iife.js"></script>
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Quick Start
|
|
31
|
-
|
|
32
|
-
### ES Modules
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
import { _encatch } from '@encatch/web-sdk';
|
|
36
|
-
|
|
37
|
-
// Initialize with your API key
|
|
38
|
-
_encatch.init('YOUR_API_KEY');
|
|
39
|
-
|
|
40
|
-
// Identify the user
|
|
41
|
-
_encatch.identifyUser('user-123', {
|
|
42
|
-
email: 'user@example.com',
|
|
43
|
-
name: 'John Doe',
|
|
44
|
-
});
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### Script Tag
|
|
48
|
-
|
|
49
|
-
```html
|
|
50
|
-
<script src="https://unpkg.com/@encatch/web-sdk/dist/encatch.iife.js"></script>
|
|
51
|
-
<script>
|
|
52
|
-
// The SDK is available as window._encatch
|
|
53
|
-
_encatch.init('YOUR_API_KEY');
|
|
54
|
-
|
|
55
|
-
_encatch.identifyUser('user-123', {
|
|
56
|
-
email: 'user@example.com',
|
|
57
|
-
name: 'John Doe',
|
|
58
|
-
});
|
|
59
|
-
</script>
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
## API Reference
|
|
63
|
-
|
|
64
|
-
### `init(apiKey, config?)`
|
|
65
|
-
|
|
66
|
-
Initialize the SDK with your API key. This must be called before any other methods. It triggers the loading of the remote implementation script.
|
|
67
|
-
|
|
68
|
-
```javascript
|
|
69
|
-
_encatch.init('YOUR_API_KEY', {
|
|
70
|
-
webHost: 'https://custom-cdn.example.com', // Optional: override default web host (script will be loaded from webHost + '/s/sdk/v1/encatch.js')
|
|
71
|
-
theme: 'dark', // Optional: 'light', 'dark', or 'system' (default: 'system')
|
|
72
|
-
});
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### `identifyUser(id, traits?, options?)`
|
|
76
|
-
|
|
77
|
-
Identify the current user. The `id` is required, traits and options are optional.
|
|
78
|
-
|
|
79
|
-
```javascript
|
|
80
|
-
// Simple - just ID
|
|
81
|
-
_encatch.identifyUser('user-123');
|
|
82
|
-
|
|
83
|
-
// With traits
|
|
84
|
-
_encatch.identifyUser('user-123', {
|
|
85
|
-
email: 'user@example.com', // Optional: user's email
|
|
86
|
-
name: 'John Doe', // Optional: user's full name
|
|
87
|
-
plan: 'premium', // Optional: custom traits
|
|
88
|
-
signed_up_at: new Date(), // Optional: date fields (must end with _at or _date)
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// With traits and options
|
|
92
|
-
_encatch.identifyUser('user-123', {
|
|
93
|
-
email: 'user@example.com',
|
|
94
|
-
name: 'John Doe',
|
|
95
|
-
}, {
|
|
96
|
-
locale: 'en', // Optional: user's locale
|
|
97
|
-
country: 'US', // Optional: user's country
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### `setLocale(locale)`
|
|
102
|
-
|
|
103
|
-
Set the user's preferred language.
|
|
104
|
-
|
|
105
|
-
```javascript
|
|
106
|
-
_encatch.setLocale('fr,en,es'); // Comma-separated ISO 639-1 codes
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### `setCountry(country)`
|
|
110
|
-
|
|
111
|
-
Set the user's country.
|
|
112
|
-
|
|
113
|
-
```javascript
|
|
114
|
-
_encatch.setCountry('FR'); // ISO 3166 country code
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### `setTheme(theme)`
|
|
118
|
-
|
|
119
|
-
Set the theme. Default is 'system'.
|
|
120
|
-
|
|
121
|
-
```javascript
|
|
122
|
-
_encatch.setTheme('dark'); // 'light', 'dark', or 'system'
|
|
123
|
-
_encatch.setTheme('light');
|
|
124
|
-
_encatch.setTheme('system'); // Follows system preference
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### `trackEvent(eventName)`
|
|
128
|
-
|
|
129
|
-
Track a custom event.
|
|
130
|
-
|
|
131
|
-
```javascript
|
|
132
|
-
_encatch.trackEvent('button_clicked');
|
|
133
|
-
_encatch.trackEvent('purchase_completed');
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
### `startSession()`
|
|
137
|
-
|
|
138
|
-
Manually start a new session. Useful after user login.
|
|
139
|
-
|
|
140
|
-
```javascript
|
|
141
|
-
_encatch.startSession();
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### `resetUser()`
|
|
145
|
-
|
|
146
|
-
Reset the current user (logout). Reverts the SDK to anonymous mode.
|
|
147
|
-
|
|
148
|
-
```javascript
|
|
149
|
-
_encatch.resetUser();
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### `showForm(formId, force?)`
|
|
153
|
-
|
|
154
|
-
Show a specific form by ID.
|
|
155
|
-
|
|
156
|
-
```javascript
|
|
157
|
-
_encatch.showForm('form-abc-123');
|
|
158
|
-
_encatch.showForm('form-abc-123', true); // Force show even if already shown
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
### `addToResponse(questionId, value)`
|
|
162
|
-
|
|
163
|
-
Prepopulate a form response.
|
|
164
|
-
|
|
165
|
-
```javascript
|
|
166
|
-
_encatch.addToResponse('question-1', 'Pre-filled answer');
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### `on(event, callback)`
|
|
170
|
-
|
|
171
|
-
Subscribe to SDK events. Returns an unsubscribe function.
|
|
172
|
-
|
|
173
|
-
```javascript
|
|
174
|
-
const unsubscribe = _encatch.on('form:submit', (payload) => {
|
|
175
|
-
console.log('Form submitted:', payload);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
// Later, to unsubscribe:
|
|
179
|
-
unsubscribe();
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### `off(event, callback)`
|
|
183
|
-
|
|
184
|
-
Unsubscribe from SDK events.
|
|
185
|
-
|
|
186
|
-
```javascript
|
|
187
|
-
function handleSubmit(payload) {
|
|
188
|
-
console.log('Form submitted:', payload);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
_encatch.on('form:submit', handleSubmit);
|
|
192
|
-
|
|
193
|
-
// Later:
|
|
194
|
-
_encatch.off('form:submit', handleSubmit);
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
## Event Types
|
|
198
|
-
|
|
199
|
-
| Event | Description |
|
|
200
|
-
|-------|-------------|
|
|
201
|
-
| `form:show` | Fired when a form is displayed |
|
|
202
|
-
| `form:submit` | Fired when a form is submitted |
|
|
203
|
-
| `form:close` | Fired when a form is closed |
|
|
204
|
-
| `form:complete` | Fired when a form is completed |
|
|
205
|
-
| `form:error` | Fired when an error occurs |
|
|
206
|
-
|
|
207
|
-
## TypeScript Support
|
|
208
|
-
|
|
209
|
-
The SDK is written in TypeScript and includes full type definitions.
|
|
210
|
-
|
|
211
|
-
```typescript
|
|
212
|
-
import { _encatch, UserTraits, EncatchConfig, EventType } from '@encatch/web-sdk';
|
|
213
|
-
|
|
214
|
-
// Example with $set operation
|
|
215
|
-
const userTraits: UserTraits = {
|
|
216
|
-
$set: {
|
|
217
|
-
email: 'user@example.com',
|
|
218
|
-
name: 'John Doe',
|
|
219
|
-
customField: 'value',
|
|
220
|
-
},
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
_encatch.identifyUser('user-123', userTraits);
|
|
224
|
-
|
|
225
|
-
// Example with multiple operations
|
|
226
|
-
const userTraitsAdvanced: UserTraits = {
|
|
227
|
-
$set: { email: 'user@example.com', name: 'John Doe' },
|
|
228
|
-
$setOnce: { firstSeenAt: new Date() },
|
|
229
|
-
$increment: { loginCount: 1 },
|
|
230
|
-
$unset: ['oldField'],
|
|
231
|
-
};
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
## How It Works
|
|
235
|
-
|
|
236
|
-
The SDK uses a "stub + async loader" pattern:
|
|
237
|
-
|
|
238
|
-
1. The lightweight stub is loaded immediately and exposes the `_encatch` object
|
|
239
|
-
2. All method calls are queued in an internal array
|
|
240
|
-
3. When `init()` is called, the full implementation script is loaded from the CDN
|
|
241
|
-
4. Once loaded, the queued commands are processed in order
|
|
242
|
-
5. Future calls go directly to the real implementation
|
|
243
|
-
|
|
244
|
-
This pattern ensures:
|
|
245
|
-
- Fast initial page load (tiny stub)
|
|
246
|
-
- No lost commands (queue preserves all calls)
|
|
247
|
-
- Type safety (full TypeScript definitions)
|
|
15
|
+
## Documentation
|
|
248
16
|
|
|
249
|
-
|
|
17
|
+
For the full API reference, configuration options, and usage examples, visit the official documentation:
|
|
250
18
|
|
|
251
|
-
-
|
|
252
|
-
- Firefox 75+
|
|
253
|
-
- Safari 13.1+
|
|
254
|
-
- Edge 80+
|
|
19
|
+
**[https://encatch.com/docs/sdk-reference/web](https://encatch.com/docs/sdk-reference/web)**
|
|
255
20
|
|
|
256
21
|
## License
|
|
257
22
|
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
declare type Command = [string, ...unknown[]];
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Context values that can be attached to a form submission.
|
|
8
|
+
* Date values are automatically serialized to ISO strings before being sent.
|
|
9
|
+
*/
|
|
10
|
+
declare type ContextValue = string | number | Date | boolean;
|
|
11
|
+
|
|
6
12
|
declare const _encatch: EncatchSDK;
|
|
7
13
|
export { _encatch }
|
|
8
14
|
export default _encatch;
|
|
@@ -174,6 +180,12 @@ export declare interface ShowFormOptions {
|
|
|
174
180
|
* @default 'always'
|
|
175
181
|
*/
|
|
176
182
|
reset?: ResetMode;
|
|
183
|
+
/**
|
|
184
|
+
* Arbitrary key-value pairs attached to the form submission.
|
|
185
|
+
* Useful for passing caller-side metadata (e.g. plan tier, feature flag states).
|
|
186
|
+
* Date values are automatically serialized to ISO 8601 strings.
|
|
187
|
+
*/
|
|
188
|
+
context?: Record<string, ContextValue>;
|
|
177
189
|
}
|
|
178
190
|
|
|
179
191
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@encatch/web-sdk",
|
|
3
|
-
"version": "1.0.0",
|
|
3
|
+
"version": "1.1.0-beta.0",
|
|
4
4
|
"description": "A lightweight, type-safe JavaScript SDK for integrating Encatch forms and surveys",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/encatch.es.js",
|
|
@@ -40,8 +40,9 @@
|
|
|
40
40
|
},
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
|
43
|
-
"url": "https://github.com/encatch/web-sdk"
|
|
43
|
+
"url": "https://github.com/get-encatch/web-sdk"
|
|
44
44
|
},
|
|
45
|
+
"homepage": "https://encatch.com",
|
|
45
46
|
"license": "MIT",
|
|
46
47
|
"engines": {
|
|
47
48
|
"node": ">=18"
|