cap-hydra-web-sdk 1.1.0 → 1.7.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 CHANGED
@@ -8,18 +8,35 @@ A comprehensive guide for integrating the Hydra WebSDK into your web application
8
8
 
9
9
  ### 1. Installation
10
10
 
11
+ Choose the release channel that matches your environment:
12
+
13
+ | Channel | When to use | npm tag |
14
+ | -------------------------- | ----------------------------------------------------------------- | ----------- |
15
+ | **Production** (`@latest`) | Live / production apps — stable, fully tested | _(default)_ |
16
+ | **Beta** (`@beta`) | Staging / pre-release testing — latest features before stable cut | `@beta` |
17
+
11
18
  **NPM (recommended):**
12
19
 
13
20
  ```bash
21
+ # Production (stable)
14
22
  npm install cap-hydra-web-sdk
23
+
24
+ # Beta (pre-release / testing)
25
+ npm install cap-hydra-web-sdk@beta
15
26
  ```
16
27
 
17
28
  **CDN / Script tag:**
18
29
 
19
30
  ```html
31
+ <!-- Production -->
20
32
  <script src="https://cdn.jsdelivr.net/npm/cap-hydra-web-sdk@latest/dist/index.umd.min.js"></script>
33
+
34
+ <!-- Beta -->
35
+ <script src="https://cdn.jsdelivr.net/npm/cap-hydra-web-sdk@beta/dist/index.umd.min.js"></script>
21
36
  ```
22
37
 
38
+ > **Beta channel:** Beta builds are published automatically on every merge to `master` and carry a timestamp suffix (e.g. `1.1.0-beta.20250610120000`). Use beta for early testing; switch to production once the release is cut.
39
+
23
40
  ### 2. Importing the SDK
24
41
 
25
42
  **ES6 Modules (TypeScript / JavaScript):**
@@ -113,9 +130,17 @@ For React applications, wrap the SDK in a service and initialize at module load
113
130
  ```typescript
114
131
  // services/sdkService.ts
115
132
  import { WebSDK } from 'cap-hydra-web-sdk';
133
+ import { initializeApp } from 'firebase/app';
134
+ import { getMessaging } from 'firebase/messaging';
116
135
 
117
136
  class SDKService {
118
137
  initialize() {
138
+ // Push notifications require a Firebase project — you create and own the app and
139
+ // Messaging instance; the SDK only calls getToken()/onMessage() against what you give it.
140
+ const firebaseApp = initializeApp({
141
+ /* your Firebase project config */
142
+ });
143
+
119
144
  WebSDK.initialize({
120
145
  firebaseConfig: {
121
146
  /* ... */
@@ -123,6 +148,7 @@ class SDKService {
123
148
  remoteConfigKey: 'your_app_config_key',
124
149
  appVersion: '1.0.0',
125
150
  notifications: {
151
+ messaging: getMessaging(firebaseApp),
126
152
  vapidKey: 'YOUR_VAPID_KEY',
127
153
  },
128
154
  });
@@ -177,9 +203,12 @@ function App() {
177
203
 
178
204
  ## Configuration
179
205
 
180
- SDK configuration values like `accountId`, `baseURL`, `debugLevel`, etc. are automatically fetched from Firebase Remote Config. The host application only needs to provide Firebase credentials and the Remote Config key.
206
+ The SDK supports two bootstrap modes, selected by whether `remoteConfigKey` is present in the config passed to `initialize()`:
181
207
 
182
- ### Required Fields
208
+ - **Remote Config bootstrap** (below): supply `firebaseConfig` + `remoteConfigKey`. Values like `accountId`, `baseURL`, `debugLevel`, etc. are fetched automatically from Firebase Remote Config.
209
+ - **Direct-config bootstrap** (see [Direct-Config Bootstrap (No Firebase)](#direct-config-bootstrap-no-firebase) below): omit `firebaseConfig` and `remoteConfigKey` entirely and pass `accountId`, `baseURL`, and `orgId` directly instead. No Firebase project is required in this mode unless `notifications` (push) is also configured.
210
+
211
+ ### Required Fields (Remote Config bootstrap)
183
212
 
184
213
  | Field | Type | Description |
185
214
  | ----------------- | ---------------- | ------------------------------------------ |
@@ -198,18 +227,21 @@ SDK configuration values like `accountId`, `baseURL`, `debugLevel`, etc. are aut
198
227
 
199
228
  The following fields are fetched from Firebase Remote Config. **Do not set them manually.**
200
229
 
201
- | Field | Required | Default | Description |
202
- | ----------------------- | -------- | --------- | ------------------------------------------------------------ |
203
- | `accountId` | Yes | — | Organization account identifier |
204
- | `baseURL` | Yes | — | API base URL for all SDK requests |
205
- | `orgId` | Yes | — | Organization ID for API authentication |
206
- | `brandId` | No | — | Brand identifier for namespaced database names |
207
- | `applicationId` | No | — | Application identifier for namespaced database names |
208
- | `source` | No | `WEBPUSH` | Source identifier for visitor profiles |
209
- | `channelType` | No | `web` | Communication channel type |
210
- | `sessionTimeoutMinutes` | No | `30` | Session inactivity timeout in minutes |
211
- | `batchSize` | No | `50` | Max events per batch request |
212
- | `debugLevel` | No | `NONE` | Logging level (`NONE` / `ERROR` / `WARN` / `INFO` / `DEBUG`) |
230
+ | Field | Required | Default | Description |
231
+ | ----------------------- | -------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
232
+ | `accountId` | Yes | — | Organization account identifier |
233
+ | `baseURL` | Yes | — | API base URL for all SDK requests |
234
+ | `orgId` | Yes | — | Organization ID for API authentication |
235
+ | `brandId` | No | — | Brand identifier for namespaced database names |
236
+ | `applicationId` | No | — | Application identifier for namespaced database names |
237
+ | `source` | No | `WEBPUSH` | Source identifier for visitor profiles |
238
+ | `channelType` | No | `web` | Communication channel type |
239
+ | `sessionTimeoutMinutes` | No | `30` | Session inactivity timeout in minutes |
240
+ | `batchSize` | No | `50` | Max events per batch request |
241
+ | `debugLevel` | No | `NONE` | Logging level (`NONE` / `ERROR` / `WARN` / `INFO` / `DEBUG`) |
242
+ | `gamificationWriteKey` | No | — | CustomerGlu write key; when present, gamification auto-initializes on user signin |
243
+ | `gamificationRegion` | No | — | CustomerGlu region code (e.g. `'us'`, `'me'`). Absent = default region (`api.customerglu.com`). Derives both the API base URL and stream URL automatically. |
244
+ | `gamificationBaseUrl` | No | — | Explicit override for the gamification API base URL. When present, takes precedence over the `gamificationRegion`-derived URL. Stream URL is still derived from `gamificationRegion`. |
213
245
 
214
246
  **Example Remote Config JSON** (stored as the value of your `remoteConfigKey` in Firebase Console):
215
247
 
@@ -224,10 +256,62 @@ The following fields are fetched from Firebase Remote Config. **Do not set them
224
256
  "channelType": "web",
225
257
  "sessionTimeoutMinutes": 30,
226
258
  "batchSize": 50,
227
- "debugLevel": "NONE"
259
+ "debugLevel": "NONE",
260
+ "gamificationWriteKey": "wk_live_xxxxxxxxxx",
261
+ "gamificationRegion": "us"
228
262
  }
229
263
  ```
230
264
 
265
+ > **Gamification is opt-in.** If `gamificationWriteKey` is absent from Remote Config, the gamification module is silently skipped. When present, campaigns and entry points are fetched and rendered automatically after `identify()`.
266
+ >
267
+ > **Containers** — six display types are supported:
268
+ >
269
+ > | Container | Behavior | Host HTML needed? |
270
+ > | ------------- | ---------------------------------------------------- | ------------------------------------- |
271
+ > | ICON_BUTTON | Fixed button (bottom-left) that opens a CHAT_BOX | No |
272
+ > | CHAT_BOX | Fixed panel above the icon button | No |
273
+ > | POP_UP | Centered full-page modal | No |
274
+ > | BOTTOM_SHEET | Sheet that slides up from the bottom | No |
275
+ > | EMBEDDED | Full-width inline block injected into a hook element | **Yes** — `<div id="{hookId}"></div>` |
276
+ > | NATIVE_BANNER | Block-level inline injection into a hook element | **Yes** — `<div id="{hookId}"></div>` |
277
+ >
278
+ > For EMBEDDED and NATIVE_BANNER, place a `<div id="{hookId}"></div>` at the desired position in your HTML. The `hookId` value is configured in the CustomerGlu dashboard.
279
+ >
280
+ > **Content types** — eleven types control what renders inside a container: `CAMPAIGN`, `WALLET`, `NUDGE`, `PROGRAM`, `REWARD` (all iframe-based), `STATIC` (image), `DYNAMIC`, `DYNAMIC_MULTISTEP_1`, `DYNAMIC_MULTISTEP_2`, `DYNAMIC_MULTISTEP_3` (native DOM progress widgets), and `ACCEPT_GUARD` (streak enrollment modal). All are configured from the CustomerGlu backend — no host-app code required.
281
+ >
282
+ > `gamificationRegion` is optional; omit it to use the default CustomerGlu region. See [DEVELOPER_GUIDE.md §14](DEVELOPER_GUIDE.md#14-gamification-customerglu) for the full container and content type reference.
283
+
284
+ ### Direct-Config Bootstrap (No Firebase)
285
+
286
+ If you don't want to depend on Firebase at all, omit `firebaseConfig` and `remoteConfigKey` from `initialize()` and pass the config directly instead:
287
+
288
+ ```typescript
289
+ WebSDK.initialize({
290
+ accountId: '100458',
291
+ baseURL: 'https://api.example.capillarytech.com',
292
+ orgId: '100458',
293
+ // Everything below is optional — same fields, same defaults as Remote Config bootstrap.
294
+ brandId: '1',
295
+ applicationId: '2',
296
+ sessionTimeoutMinutes: 30,
297
+ batchSize: 50,
298
+ debugLevel: 'NONE',
299
+ gamificationWriteKey: 'wk_live_xxxxxxxxxx',
300
+ gamificationRegion: 'us',
301
+ });
302
+ ```
303
+
304
+ | Field | Required? | Notes |
305
+ | --------------- | --------- | ---------------------------------------------------------------------------- |
306
+ | `accountId` | **Yes** | Same field as the Remote Config JSON's `accountId` |
307
+ | `baseURL` | **Yes** | Same field as the Remote Config JSON's `baseURL` |
308
+ | `orgId` | **Yes** | Same field as the Remote Config JSON's `orgId` |
309
+ | Everything else | No | Same optional fields and defaults as the "Auto-populated Fields" table above |
310
+
311
+ No Firebase project is required in this mode. The one exception is push notifications, which always require Firebase regardless of bootstrap mode — see [Push Notifications](#push-notifications) below for how to create and pass a Messaging instance via `notifications.messaging`.
312
+
313
+ This is also how to use a different config source than this SDK's built-in Remote Config fetch — including Firebase Remote Config used elsewhere in your own app. Fetch your config however you like and pass the values in directly; there's nothing Firebase-specific about this mode.
314
+
231
315
  ---
232
316
 
233
317
  ## Public API Reference
@@ -394,7 +478,20 @@ console.log(userInfo.isFirstSession); // true
394
478
 
395
479
  ---
396
480
 
397
- ### 6. `enableSDK()`
481
+ ### 6. `getAnonymousId()`
482
+
483
+ Returns the backend-assigned anonymous visitor ID, or `null` if visitor tracking is disabled or not yet resolved. Available regardless of whether the user is identified or anonymous.
484
+
485
+ ```javascript
486
+ const visitorId = WebSDK.getAnonymousId();
487
+ // "b3f1a2c4-..." or null
488
+ ```
489
+
490
+ **Returns:** `string | null`
491
+
492
+ ---
493
+
494
+ ### 7. `enableSDK()`
398
495
 
399
496
  Re-enables event tracking after it was disabled. Pending events in the queue are processed.
400
497
 
@@ -404,7 +501,7 @@ WebSDK.enableSDK();
404
501
 
405
502
  ---
406
503
 
407
- ### 7. `disableSDK()`
504
+ ### 8. `disableSDK()`
408
505
 
409
506
  Disables event tracking. New events are dropped. Existing queued events are preserved in IndexedDB and will be sent when the SDK is re-enabled.
410
507
 
@@ -414,7 +511,7 @@ WebSDK.disableSDK();
414
511
 
415
512
  ---
416
513
 
417
- ### 8. `reset()`
514
+ ### 9. `reset()`
418
515
 
419
516
  Signs out the current user and returns the SDK to anonymous tracking mode.
420
517
 
@@ -424,17 +521,57 @@ WebSDK.reset();
424
521
 
425
522
  ---
426
523
 
524
+ ### 10. `setCapToken(token)`
525
+
526
+ Stores a Capillary short-lived token and appends it as `?capToken=<value>` (or `&capToken=<value>` if the URL already has query params) to all loyalty embed URLs rendered by the SDK.
527
+
528
+ ```javascript
529
+ WebSDK.setCapToken('your-capillary-token');
530
+ ```
531
+
532
+ Call this whenever the token is refreshed. The token is persisted in the SDK's internal storage and survives page reloads. It is cleared on `reset()` / sign-out — call `setCapToken()` again after the user signs back in.
533
+
534
+ ---
535
+
536
+ ### 11. `getCapToken()`
537
+
538
+ Returns the currently stored Capillary token, or `null` if none has been set.
539
+
540
+ ```javascript
541
+ const token = WebSDK.getCapToken();
542
+ ```
543
+
544
+ ---
545
+
427
546
  ## Push Notifications
428
547
 
429
- To enable push notifications, add the `notifications` config to `initialize()`:
548
+ The SDK never initializes Firebase itself — **you own Firebase for push notifications**: create the
549
+ Firebase app and a `Messaging` instance yourself (using the modular `firebase/app` and
550
+ `firebase/messaging` packages), and pass the instance to `notifications.messaging`. The SDK only
551
+ calls `getToken()`/`onMessage()` against the instance you provide, registers the service worker,
552
+ persists the token, and syncs it to the backend. This applies regardless of which
553
+ [bootstrap mode](#configuration) you use for the rest of the SDK's configuration.
554
+
555
+ `firebase` is a peer dependency — install it yourself (`npm install firebase`) if you want push
556
+ notifications; it is not required otherwise.
557
+
558
+ `notifications.messaging` is optional — omit it (or omit `notifications` entirely) to disable
559
+ push notifications; the SDK no-ops safely with no compile-time or runtime error.
430
560
 
431
561
  ```javascript
562
+ import { initializeApp } from 'firebase/app';
563
+ import { getMessaging } from 'firebase/messaging';
564
+
565
+ const firebaseApp = initializeApp({
566
+ /* your Firebase project config */
567
+ });
568
+
432
569
  WebSDK.initialize({
433
- firebaseConfig: {
434
- /* ... */
435
- },
436
- remoteConfigKey: 'your_app_config_key',
570
+ accountId: '100458',
571
+ baseURL: 'https://api.example.capillarytech.com',
572
+ orgId: '100458',
437
573
  notifications: {
574
+ messaging: getMessaging(firebaseApp),
438
575
  vapidKey: 'YOUR_VAPID_KEY',
439
576
  serviceWorkerPath: '/firebase-messaging-sw.js',
440
577
  },