@cognior/iap-sdk 0.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.
- package/.github/copilot-instructions.md +95 -0
- package/README.md +79 -0
- package/TRACKING.md +105 -0
- package/USER_CONTEXT_README.md +284 -0
- package/package.json +154 -0
- package/src/config.ts +25 -0
- package/src/core/flowEngine.ts +1833 -0
- package/src/core/triggerManager.ts +1011 -0
- package/src/experiences/banner.ts +366 -0
- package/src/experiences/beacon.ts +668 -0
- package/src/experiences/hotspotTour.ts +654 -0
- package/src/experiences/hotspots.ts +566 -0
- package/src/experiences/modal.ts +1337 -0
- package/src/experiences/modalSequence.ts +1247 -0
- package/src/experiences/popover.ts +652 -0
- package/src/experiences/registry.ts +21 -0
- package/src/experiences/survey.ts +1639 -0
- package/src/experiences/taskList.ts +625 -0
- package/src/experiences/tooltip.ts +740 -0
- package/src/experiences/types.ts +395 -0
- package/src/experiences/walkthrough.ts +670 -0
- package/src/flow-sequence.ts +177 -0
- package/src/flows.ts +512 -0
- package/src/http.ts +61 -0
- package/src/index.ts +355 -0
- package/src/services/flowManager.ts +905 -0
- package/src/services/flowNormalizer.ts +74 -0
- package/src/services/locationContextService.ts +189 -0
- package/src/services/pageContextService.ts +221 -0
- package/src/services/userContextService.ts +286 -0
- package/src/state/appState.ts +0 -0
- package/src/state/hooks.ts +0 -0
- package/src/state/index.ts +0 -0
- package/src/state/migration.ts +0 -0
- package/src/state/store.ts +0 -0
- package/src/styles/banner.css.ts +0 -0
- package/src/styles/hotspot.css.ts +0 -0
- package/src/styles/hotspotTour.css.ts +0 -0
- package/src/styles/modal.css.ts +564 -0
- package/src/styles/survey.css.ts +1013 -0
- package/src/styles/taskList.css.ts +0 -0
- package/src/styles/tooltip.css.ts +149 -0
- package/src/styles/walkthrough.css.ts +0 -0
- package/src/tourUtils.ts +0 -0
- package/src/tracking.ts +223 -0
- package/src/utils/debounce.ts +66 -0
- package/src/utils/eventSequenceValidator.ts +124 -0
- package/src/utils/flowTrackingSystem.ts +524 -0
- package/src/utils/idGenerator.ts +155 -0
- package/src/utils/immediateValidationPrevention.ts +184 -0
- package/src/utils/normalize.ts +50 -0
- package/src/utils/privacyManager.ts +166 -0
- package/src/utils/ruleEvaluator.ts +199 -0
- package/src/utils/sanitize.ts +79 -0
- package/src/utils/selectors.ts +107 -0
- package/src/utils/stepExecutor.ts +345 -0
- package/src/utils/triggerNormalizer.ts +149 -0
- package/src/utils/validationInterceptor.ts +650 -0
- package/tsconfig.json +13 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Copilot Instructions — DAP SDK (TypeScript)
|
|
2
|
+
|
|
3
|
+
## Summary
|
|
4
|
+
This repository is a **TypeScript SDK** for rendering and managing Digital Adoption Platform (DAP) UX experiences (modals, tooltips, popovers, surveys, hotspots, walkthroughs) in the browser. It builds to a browser-ready bundle via **tsup** and exposes a global `DAP` entry point.
|
|
5
|
+
|
|
6
|
+
## Repo Profile
|
|
7
|
+
- **Project type:** TypeScript SDK (browser runtime)
|
|
8
|
+
- **Languages:** TypeScript
|
|
9
|
+
- **Build tool:** `tsup` (bundles ESM + IIFE)
|
|
10
|
+
- **Target runtime:** modern browsers (ES2020)
|
|
11
|
+
- **Output:** `public/dist`
|
|
12
|
+
- **Repo size:** small/medium SDK repo (single package)
|
|
13
|
+
|
|
14
|
+
## Build & Validation (always run in this order)
|
|
15
|
+
> **Note:** These commands have not been validated in this environment; run them locally to verify.
|
|
16
|
+
|
|
17
|
+
1. **Bootstrap**
|
|
18
|
+
- **Always run**: `npm install`
|
|
19
|
+
- Preconditions: Node.js installed (version not specified in repo)
|
|
20
|
+
2. **Build**
|
|
21
|
+
- `npm run build`
|
|
22
|
+
- Uses `tsup` per [tsup.config.ts](tsup.config.ts)
|
|
23
|
+
3. **Dev watch**
|
|
24
|
+
- `npm run dev`
|
|
25
|
+
4. **Run local static server**
|
|
26
|
+
- `npm run serve` (serves `public` on port 5173)
|
|
27
|
+
5. **Tests**
|
|
28
|
+
- No test script present in [package.json](package.json)
|
|
29
|
+
6. **Lint**
|
|
30
|
+
- No lint script/config present
|
|
31
|
+
|
|
32
|
+
### Observed/expected behaviors
|
|
33
|
+
- Build output goes to `public/dist` (cleaned before build).
|
|
34
|
+
- No explicit CI workflows are present in the repo root.
|
|
35
|
+
- If build fails due to missing deps, re-run `npm install`.
|
|
36
|
+
|
|
37
|
+
## Project Layout & Architecture
|
|
38
|
+
### Core entry points
|
|
39
|
+
- **SDK entry:** [src/index.ts](src/index.ts) — registers experiences and exposes API
|
|
40
|
+
- **Flow normalization:** [src/flows.ts](src/flows.ts)
|
|
41
|
+
- **Execution engine:** [src/core/flowEngine.ts](src/core/flowEngine.ts)
|
|
42
|
+
|
|
43
|
+
### Experiences
|
|
44
|
+
- [src/experiences/](src/experiences/) — modal, tooltip, popover, survey, banner, beacon, hotspots, hotspotTour, taskList, walkthrough
|
|
45
|
+
|
|
46
|
+
### Utilities & services
|
|
47
|
+
- [src/utils/](src/utils/) — selectors, sanitize, rule evaluation, triggers, tracking
|
|
48
|
+
- [src/services/](src/services/) — flow/user/page context services
|
|
49
|
+
- [src/styles/](src/styles/) — CSS-in-TS for experiences
|
|
50
|
+
|
|
51
|
+
### Config & build
|
|
52
|
+
- [package.json](package.json) — scripts: `build`, `dev`, `serve`
|
|
53
|
+
- [tsconfig.json](tsconfig.json) — TS compile options (ES2020, strict)
|
|
54
|
+
- [tsup.config.ts](tsup.config.ts) — bundling config
|
|
55
|
+
- Root docs: [TRACKING.md](TRACKING.md), [USER_CONTEXT_README.md](USER_CONTEXT_README.md)
|
|
56
|
+
|
|
57
|
+
### Root files (current)
|
|
58
|
+
- .gitignore
|
|
59
|
+
- package.json
|
|
60
|
+
- TRACKING.md
|
|
61
|
+
- tsconfig.json
|
|
62
|
+
- tsup.config.ts
|
|
63
|
+
- USER_CONTEXT_README.md
|
|
64
|
+
- public/
|
|
65
|
+
- src/
|
|
66
|
+
|
|
67
|
+
### Next-level directories
|
|
68
|
+
- `src/`
|
|
69
|
+
- config.ts
|
|
70
|
+
- flow-sequence.ts
|
|
71
|
+
- flows.ts
|
|
72
|
+
- http.ts
|
|
73
|
+
- index.ts
|
|
74
|
+
- tourUtils.ts
|
|
75
|
+
- tracking.ts
|
|
76
|
+
- core/
|
|
77
|
+
- experiences/
|
|
78
|
+
- services/
|
|
79
|
+
- state/
|
|
80
|
+
- styles/
|
|
81
|
+
- utils/
|
|
82
|
+
|
|
83
|
+
## Validation & PR Safety
|
|
84
|
+
- **Always run** `npm install` before build/watch.
|
|
85
|
+
- **Always run** `npm run build` before completing changes.
|
|
86
|
+
- There are no repo-defined test or lint scripts; treat `npm run build` as the primary validation step.
|
|
87
|
+
- If adding new dependencies, update [package.json](package.json) and verify build output in `public/dist`.
|
|
88
|
+
|
|
89
|
+
## Notes for efficient changes
|
|
90
|
+
- Experience rendering logic lives under [src/experiences/](src/experiences/).
|
|
91
|
+
- Flow execution and rule handling are centralized in [src/core/flowEngine.ts](src/core/flowEngine.ts) and [src/utils/](src/utils/).
|
|
92
|
+
- Avoid changing the public API surface in [src/index.ts](src/index.ts) unless required.
|
|
93
|
+
|
|
94
|
+
## Instruction Priority
|
|
95
|
+
Follow this document first. Only search the repo if this file is incomplete or incorrect.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Cognior IAP SDK
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for rendering Intelligent Adoption Platform (IAP) experiences (modals, tooltips, popovers, surveys, hotspots, walkthroughs) in the browser. Bundled with `tsup` and exposes a global `DAP` entry point for browser usage.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cognior/iap-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Browser (Global `DAP`)
|
|
14
|
+
If you serve the bundled IIFE, it exposes `window.DAP`.
|
|
15
|
+
|
|
16
|
+
```html
|
|
17
|
+
<script src="./dist/index.global.js"></script>
|
|
18
|
+
<script>
|
|
19
|
+
DAP.init({
|
|
20
|
+
configUrl: "/dap-config.json",
|
|
21
|
+
debug: true
|
|
22
|
+
});
|
|
23
|
+
</script>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### ESM (Bundler)
|
|
27
|
+
```ts
|
|
28
|
+
import * as DAP from "@cognior/iap-sdk";
|
|
29
|
+
|
|
30
|
+
await DAP.init({
|
|
31
|
+
configUrl: "/dap-config.json",
|
|
32
|
+
debug: true
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Initialization
|
|
37
|
+
`DAP.init` accepts a config URL and optional debug/user parameters. See [`init`](src/index.ts) and [`DapConfig`](src/config.ts).
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
await DAP.init({
|
|
41
|
+
configUrl: "https://your-api.com/config",
|
|
42
|
+
debug: true,
|
|
43
|
+
user: {
|
|
44
|
+
id: "user-123",
|
|
45
|
+
role: "Admin",
|
|
46
|
+
email: "admin@company.com",
|
|
47
|
+
attributes: { plan: "Premium" }
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Required Config Fields
|
|
53
|
+
The config returned by `configUrl` must include:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"organizationid": "string",
|
|
58
|
+
"siteid": "string",
|
|
59
|
+
"apikey": "string",
|
|
60
|
+
"apiurl": "string"
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
See [`DapConfig`](src/config.ts).
|
|
65
|
+
|
|
66
|
+
## Tracking
|
|
67
|
+
Automatic tracking is enabled by default.
|
|
68
|
+
|
|
69
|
+
## User Context
|
|
70
|
+
User context is optional but enables personalization.
|
|
71
|
+
|
|
72
|
+
## Public API (high-level)
|
|
73
|
+
- `DAP.init(...)` — initialize SDK
|
|
74
|
+
- `DAP.executeFlow(...)` — execute a flow object
|
|
75
|
+
- `DAP.registerFlow(...)` — register flow by ID
|
|
76
|
+
- `DAP.startFlow(flowId)` — start a flow by ID
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
UNLICENSED
|
package/TRACKING.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# DAP Flow Activity Tracking
|
|
2
|
+
|
|
3
|
+
The DAP SDK now includes functionality for tracking user interactions with DAP flows and experiences, sending this activity data to the DAP backend. This document explains how to use this feature focused on tracking DAP-related user activities.
|
|
4
|
+
|
|
5
|
+
## Tracking Endpoint
|
|
6
|
+
|
|
7
|
+
The SDK uses the following endpoint for tracking user actions:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
POST /api/iap-experience/{organizationId}/{siteCollectionId}/track-action/{flowId}
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The request body includes:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"stepId": "string",
|
|
18
|
+
"actionType": "string",
|
|
19
|
+
"actionData": "string",
|
|
20
|
+
"sessionId": "string",
|
|
21
|
+
"occurredAt": "2025-09-24T15:05:57.569Z",
|
|
22
|
+
"clientInfo": {
|
|
23
|
+
"userId": "string",
|
|
24
|
+
"clientIP": "string",
|
|
25
|
+
"userAgent": "string",
|
|
26
|
+
"locale": "string"
|
|
27
|
+
},
|
|
28
|
+
"elementSelector": "string",
|
|
29
|
+
"elementContext": "string"
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Automatic Tracking
|
|
36
|
+
|
|
37
|
+
The SDK can automatically track common user interactions like clicks, form submissions, and input changes. This is enabled by default when you initialize the DAP SDK:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
DAP.init({
|
|
41
|
+
configUrl: '../dap-config.json',
|
|
42
|
+
debug: true,
|
|
43
|
+
enableTracking: true // This is the default
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
To disable automatic tracking at initialization:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
DAP.init({
|
|
51
|
+
configUrl: '../dap-config.json',
|
|
52
|
+
debug: true,
|
|
53
|
+
enableTracking: false
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also enable or disable tracking after initialization:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
// Enable tracking
|
|
61
|
+
DAP.enableActionTracking();
|
|
62
|
+
|
|
63
|
+
// Disable tracking
|
|
64
|
+
DAP.disableActionTracking();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Manual Tracking
|
|
68
|
+
|
|
69
|
+
You can manually track specific DAP flow activities:
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
DAP.trackFlowActivity({
|
|
73
|
+
actionType: 'flow_interaction',
|
|
74
|
+
actionData: JSON.stringify({
|
|
75
|
+
flowType: 'tutorial',
|
|
76
|
+
interactionType: 'step_complete',
|
|
77
|
+
stepName: 'feature_introduction'
|
|
78
|
+
}),
|
|
79
|
+
stepId: 'tutorial-step-1',
|
|
80
|
+
elementSelector: '.dap-tutorial-container'
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Tracked Flow Activities
|
|
85
|
+
|
|
86
|
+
The tracking system focuses on DAP flow activities and captures:
|
|
87
|
+
|
|
88
|
+
1. **Flow Impressions** - When a DAP flow is presented to the user
|
|
89
|
+
2. **Flow Interactions** - When users interact with DAP components (modals, tooltips, etc.)
|
|
90
|
+
3. **Survey Interactions** - When users engage with DAP surveys
|
|
91
|
+
4. **Flow Completions** - When users complete a DAP flow sequence
|
|
92
|
+
5. **Step Transitions** - When users navigate between steps in a flow
|
|
93
|
+
|
|
94
|
+
## Data Privacy
|
|
95
|
+
|
|
96
|
+
The tracking system is designed with privacy in mind:
|
|
97
|
+
|
|
98
|
+
- Password fields are automatically redacted
|
|
99
|
+
- Credit card fields are automatically redacted
|
|
100
|
+
- No personally identifiable information (PII) is tracked unless explicitly provided
|
|
101
|
+
- Tracking can be disabled at any time
|
|
102
|
+
|
|
103
|
+
## Session Management
|
|
104
|
+
|
|
105
|
+
The SDK automatically creates and maintains a session ID stored in the browser's session storage. This helps correlate actions within the same browsing session.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# DAP SDK User Context Implementation
|
|
2
|
+
|
|
3
|
+
## 🎯 Overview
|
|
4
|
+
|
|
5
|
+
The DAP SDK now includes **Standard User Context Handling** that allows host applications to provide user data explicitly for:
|
|
6
|
+
- Flow targeting and execution
|
|
7
|
+
- Rule evaluation based on user properties
|
|
8
|
+
- Analytics tracking with user context
|
|
9
|
+
- Safe fallback for anonymous users
|
|
10
|
+
|
|
11
|
+
## 🔒 Security Principles
|
|
12
|
+
|
|
13
|
+
✅ **NEVER infers user identity** - only accepts explicitly provided data
|
|
14
|
+
✅ **Host application is source of truth** for user data
|
|
15
|
+
✅ **Defensive programming** - works safely when user data is missing
|
|
16
|
+
✅ **No DOM scraping, cookie reading, or JWT parsing**
|
|
17
|
+
|
|
18
|
+
## 📋 User Model
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
interface DapUser {
|
|
22
|
+
id: string; // Required: unique user identifier
|
|
23
|
+
role?: string; // Optional: user role/type
|
|
24
|
+
email?: string; // Optional: user email
|
|
25
|
+
attributes?: Record<string, string>; // Optional: custom user attributes
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 🚀 Quick Start
|
|
30
|
+
|
|
31
|
+
### 1. SDK Initialization
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
// Initialize without user (anonymous mode)
|
|
35
|
+
await DAP.init({
|
|
36
|
+
configUrl: "https://your-api.com/config",
|
|
37
|
+
debug: true
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Initialize with user context
|
|
41
|
+
await DAP.init({
|
|
42
|
+
configUrl: "https://your-api.com/config",
|
|
43
|
+
debug: true,
|
|
44
|
+
user: {
|
|
45
|
+
id: "user-12345",
|
|
46
|
+
role: "Admin",
|
|
47
|
+
email: "admin@company.com",
|
|
48
|
+
attributes: {
|
|
49
|
+
department: "Engineering",
|
|
50
|
+
plan: "Premium"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 2. User Context Management
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
// Set user context
|
|
60
|
+
DAP.setUser({
|
|
61
|
+
id: "user-67890",
|
|
62
|
+
role: "Manager",
|
|
63
|
+
email: "manager@company.com",
|
|
64
|
+
attributes: {
|
|
65
|
+
team: "Sales",
|
|
66
|
+
region: "US-West"
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Update existing user
|
|
71
|
+
DAP.updateUser({
|
|
72
|
+
role: "Senior Manager", // Updated role
|
|
73
|
+
attributes: {
|
|
74
|
+
team: "Sales",
|
|
75
|
+
region: "US-West",
|
|
76
|
+
certification: "Advanced" // New attribute
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Get current user
|
|
81
|
+
const user = DAP.getUser();
|
|
82
|
+
console.log(user);
|
|
83
|
+
|
|
84
|
+
// Clear user context
|
|
85
|
+
DAP.clearUser();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 🎛️ Flow Execution Logic
|
|
89
|
+
|
|
90
|
+
### Without User Context
|
|
91
|
+
- SDK generates anonymous fallback user ID: `dap-anon-<timestamp>-<random>`
|
|
92
|
+
- Stored in `sessionStorage` as `dap_anonymous_user_id`
|
|
93
|
+
- Flows execute unless they explicitly require user properties
|
|
94
|
+
- Analytics track with `isAnonymous: true`
|
|
95
|
+
|
|
96
|
+
### With User Context
|
|
97
|
+
- Real user data used for flow targeting
|
|
98
|
+
- Rules can evaluate user properties
|
|
99
|
+
- Analytics include full user context
|
|
100
|
+
- Flows execute based on user-specific rules
|
|
101
|
+
|
|
102
|
+
### Flow Initialization Timing
|
|
103
|
+
- Waits for `DOMContentLoaded` event
|
|
104
|
+
- Blocks flow execution if user context is required but missing
|
|
105
|
+
- Re-evaluates flows when `setUser()` is called
|
|
106
|
+
|
|
107
|
+
## 📏 Rule Engine Integration
|
|
108
|
+
|
|
109
|
+
Rules can now reference user properties safely:
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
// Example rule conditions
|
|
113
|
+
{
|
|
114
|
+
"property": "user.role",
|
|
115
|
+
"operator": "Equals",
|
|
116
|
+
"value": "Admin"
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
{
|
|
120
|
+
"property": "user.attributes.plan",
|
|
121
|
+
"operator": "Equals",
|
|
122
|
+
"value": "Premium"
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Supported Properties
|
|
127
|
+
- `user.id` - User identifier
|
|
128
|
+
- `user.role` - User role/type
|
|
129
|
+
- `user.email` - User email address
|
|
130
|
+
- `user.attributes.<key>` - Custom user attributes
|
|
131
|
+
|
|
132
|
+
### Rule Safety
|
|
133
|
+
- Missing user → property evaluates to `null`
|
|
134
|
+
- Missing property → evaluates to `null`
|
|
135
|
+
- Rules fail gracefully without throwing errors
|
|
136
|
+
- Defensive logging for debugging
|
|
137
|
+
|
|
138
|
+
## 📊 Analytics Integration
|
|
139
|
+
|
|
140
|
+
All tracking payloads automatically include:
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
{
|
|
144
|
+
userId: "user-12345", // User ID or anonymous ID
|
|
145
|
+
userRole: "Admin", // User role (if available)
|
|
146
|
+
userAttributes: { // User attributes (if available)
|
|
147
|
+
department: "Engineering",
|
|
148
|
+
plan: "Premium"
|
|
149
|
+
},
|
|
150
|
+
isAnonymous: false, // Whether user is anonymous
|
|
151
|
+
// ... other tracking fields
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Anonymous User Analytics
|
|
156
|
+
```javascript
|
|
157
|
+
{
|
|
158
|
+
userId: "dap-anon-1641234567-abc123",
|
|
159
|
+
isAnonymous: true
|
|
160
|
+
// role and attributes will be undefined
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 🛠️ API Reference
|
|
165
|
+
|
|
166
|
+
### Initialization
|
|
167
|
+
```typescript
|
|
168
|
+
DAP.init(options: {
|
|
169
|
+
configUrl: string;
|
|
170
|
+
debug?: boolean;
|
|
171
|
+
screenId?: string;
|
|
172
|
+
user?: DapUser; // 🆕 Optional user context
|
|
173
|
+
}): Promise<void>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### User Management
|
|
177
|
+
```typescript
|
|
178
|
+
DAP.setUser(user: DapUser): void
|
|
179
|
+
DAP.updateUser(partialUser: Partial<DapUser>): void
|
|
180
|
+
DAP.getUser(): DapUser | null
|
|
181
|
+
DAP.clearUser(): void
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Debug Utilities
|
|
185
|
+
```typescript
|
|
186
|
+
DAP.getUserState(): {
|
|
187
|
+
hasUser: boolean;
|
|
188
|
+
userId: string;
|
|
189
|
+
userRole?: string;
|
|
190
|
+
isAnonymous: boolean;
|
|
191
|
+
attributeCount: number;
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## 🔧 Implementation Details
|
|
196
|
+
|
|
197
|
+
### UserContextService
|
|
198
|
+
- **Singleton service** managing user state
|
|
199
|
+
- **In-memory storage** as source of truth
|
|
200
|
+
- **sessionStorage persistence** for page reloads
|
|
201
|
+
- **Event system** for user context changes
|
|
202
|
+
- **Defensive property access** for rule evaluation
|
|
203
|
+
|
|
204
|
+
### Flow Engine Integration
|
|
205
|
+
- **User context validation** before flow execution
|
|
206
|
+
- **Automatic re-evaluation** when user context changes
|
|
207
|
+
- **Graceful handling** of missing user data
|
|
208
|
+
|
|
209
|
+
### Analytics Enhancement
|
|
210
|
+
- **Automatic user context injection** in all tracking payloads
|
|
211
|
+
- **Anonymous user handling** with fallback IDs
|
|
212
|
+
- **Privacy-safe data sanitization**
|
|
213
|
+
|
|
214
|
+
## ⚡ Performance Considerations
|
|
215
|
+
|
|
216
|
+
- **Lightweight singleton** - minimal memory footprint
|
|
217
|
+
- **Efficient sessionStorage** operations with error handling
|
|
218
|
+
- **Event-driven updates** - no polling or watchers
|
|
219
|
+
- **Lazy initialization** - services created on demand
|
|
220
|
+
|
|
221
|
+
## 🧪 Testing
|
|
222
|
+
|
|
223
|
+
See `test-user-context.js` for comprehensive test examples covering:
|
|
224
|
+
- SDK initialization with/without user
|
|
225
|
+
- User context management lifecycle
|
|
226
|
+
- Rule evaluation with user properties
|
|
227
|
+
- Analytics context generation
|
|
228
|
+
- Anonymous user fallback behavior
|
|
229
|
+
|
|
230
|
+
## 🔄 Migration Guide
|
|
231
|
+
|
|
232
|
+
### For Existing SDK Users
|
|
233
|
+
- **Zero breaking changes** - all existing functionality preserved
|
|
234
|
+
- **Optional user parameter** in `init()` method
|
|
235
|
+
- **Backward compatible** - works without user context
|
|
236
|
+
|
|
237
|
+
### For New Implementations
|
|
238
|
+
- **Start with anonymous mode** for immediate functionality
|
|
239
|
+
- **Add user context** when authentication is available
|
|
240
|
+
- **Use rule targeting** for personalized experiences
|
|
241
|
+
|
|
242
|
+
## 📝 Examples
|
|
243
|
+
|
|
244
|
+
### E-commerce Platform
|
|
245
|
+
```javascript
|
|
246
|
+
// After user login
|
|
247
|
+
DAP.setUser({
|
|
248
|
+
id: customer.id,
|
|
249
|
+
role: customer.tier, // "Bronze", "Silver", "Gold"
|
|
250
|
+
email: customer.email,
|
|
251
|
+
attributes: {
|
|
252
|
+
country: customer.address.country,
|
|
253
|
+
purchaseHistory: customer.totalOrders.toString(),
|
|
254
|
+
lastPurchase: customer.lastOrderDate
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// Target premium features to Gold customers
|
|
259
|
+
// Rule: user.role === "Gold"
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### SaaS Application
|
|
263
|
+
```javascript
|
|
264
|
+
// After authentication
|
|
265
|
+
DAP.setUser({
|
|
266
|
+
id: user.id,
|
|
267
|
+
role: user.role, // "Admin", "User", "Viewer"
|
|
268
|
+
email: user.email,
|
|
269
|
+
attributes: {
|
|
270
|
+
organization: user.orgId,
|
|
271
|
+
plan: subscription.planType,
|
|
272
|
+
features: user.enabledFeatures.join(",")
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// Show admin onboarding only to admins
|
|
277
|
+
// Rule: user.role === "Admin"
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 🎉 Ready to Use!
|
|
283
|
+
|
|
284
|
+
The User Context system is now fully integrated and ready for production use. All components work together seamlessly to provide a robust, secure, and flexible user context management solution for the DAP SDK.
|