flowgrid-sdk 1.1.0 → 1.2.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.
Files changed (65) hide show
  1. package/README.md +320 -8
  2. package/dist/browser.d.ts +6 -7
  3. package/dist/flowgrid.min.js +1 -1
  4. package/dist/flowgrid.min.js.map +1 -1
  5. package/dist/index.d.ts +11 -4
  6. package/dist/index.js +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/lib/client/transport.d.ts +175 -0
  9. package/dist/lib/consent/index.d.ts +37 -0
  10. package/dist/lib/consent/manager.d.ts +197 -0
  11. package/dist/lib/consent/types.d.ts +162 -0
  12. package/dist/lib/features/analytics/attribution.d.ts +3 -3
  13. package/dist/lib/features/analytics/events.d.ts +3 -3
  14. package/dist/lib/features/analytics/funnels.d.ts +3 -3
  15. package/dist/lib/features/analytics/heatmaps.d.ts +9 -3
  16. package/dist/lib/features/analytics/identify.d.ts +3 -3
  17. package/dist/lib/features/analytics/page-views.d.ts +3 -3
  18. package/dist/lib/features/analytics/performance.d.ts +3 -3
  19. package/dist/lib/features/analytics/retention.d.ts +3 -3
  20. package/dist/lib/features/analytics/sessions.d.ts +3 -3
  21. package/dist/lib/features/core/activation.d.ts +3 -3
  22. package/dist/lib/features/core/experiment.d.ts +86 -70
  23. package/dist/lib/features/core/index.d.ts +1 -3
  24. package/dist/lib/features/core/track-feature.d.ts +3 -3
  25. package/dist/lib/features/core/track-prompt.d.ts +3 -3
  26. package/dist/lib/features/ecommerce/cart.d.ts +3 -3
  27. package/dist/lib/features/ecommerce/checkout.d.ts +3 -3
  28. package/dist/lib/features/ecommerce/inventory.d.ts +3 -3
  29. package/dist/lib/features/ecommerce/ltv.d.ts +3 -3
  30. package/dist/lib/features/ecommerce/products.d.ts +3 -3
  31. package/dist/lib/features/ecommerce/promotions.d.ts +3 -3
  32. package/dist/lib/features/ecommerce/purchases.d.ts +3 -3
  33. package/dist/lib/features/ecommerce/refunds.d.ts +3 -3
  34. package/dist/lib/features/ecommerce/search.d.ts +3 -3
  35. package/dist/lib/features/ecommerce/subscriptions.d.ts +3 -3
  36. package/dist/lib/features/ecommerce/wishlist.d.ts +3 -3
  37. package/dist/lib/features/enterprise/acquisition.d.ts +2 -2
  38. package/dist/lib/features/enterprise/alerts.d.ts +2 -2
  39. package/dist/lib/features/enterprise/churn.d.ts +2 -2
  40. package/dist/lib/features/enterprise/cohorts.d.ts +2 -2
  41. package/dist/lib/features/enterprise/engagement.d.ts +24 -3
  42. package/dist/lib/features/enterprise/forecasting.d.ts +2 -2
  43. package/dist/lib/features/enterprise/monetization.d.ts +2 -2
  44. package/dist/lib/features/enterprise/multi-path-funnels.d.ts +2 -2
  45. package/dist/lib/features/enterprise/paths.d.ts +2 -2
  46. package/dist/lib/features/enterprise/security.d.ts +2 -2
  47. package/dist/lib/features/enterprise/support.d.ts +2 -2
  48. package/dist/lib/frameworks/nextjs/client.d.ts +7 -13
  49. package/dist/lib/frameworks/nextjs/server.d.ts +5 -15
  50. package/dist/lib/frameworks/node/index.d.ts +2 -6
  51. package/dist/lib/frameworks/nuxt/index.d.ts +7 -3
  52. package/dist/lib/frameworks/react/CookieBanner.d.ts +91 -0
  53. package/dist/lib/frameworks/react/index.d.ts +98 -136
  54. package/dist/lib/frameworks/vue/CookieBanner.d.ts +82 -0
  55. package/dist/lib/frameworks/vue/index.d.ts +30 -27
  56. package/dist/lib/types/analytics.d.ts +62 -0
  57. package/dist/lib/types/common.d.ts +2 -0
  58. package/dist/lib/utils/identity.d.ts +93 -0
  59. package/dist/lib/utils/storage.d.ts +69 -0
  60. package/package.json +13 -2
  61. package/dist/lib/client/enterprise-ftrpc.d.ts +0 -51
  62. package/dist/lib/client/ftrpc.d.ts +0 -132
  63. package/dist/lib/client/modules.d.ts +0 -104
  64. package/dist/lib/features/core/feature-flag.d.ts +0 -249
  65. package/dist/lib/utils/functions.d.ts +0 -1
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @fileoverview FlowGrid Identity Manager.
3
+ * @module utils/identity
4
+ *
5
+ * @description
6
+ * Owns the visitor/session identity lifecycle.
7
+ * Writes its own cookies/storage so the SDK can self-bootstrap
8
+ * without depending on an external tracking script (track.js).
9
+ *
10
+ * Backward-compatible: checks for existing track.js `visitor_id`
11
+ * before falling back to its own `fg_visitor_id` key.
12
+ */
13
+ export interface IdentityConfig {
14
+ /** Explicit visitor ID (overrides auto-resolution) */
15
+ visitorId?: string;
16
+ /** Cookie domain (defaults to current hostname) */
17
+ cookieDomain?: string;
18
+ /** Visitor cookie max-age in days (default: 365) */
19
+ visitorMaxAgeDays?: number;
20
+ /** Session timeout in minutes (default: 30) */
21
+ sessionTimeoutMinutes?: number;
22
+ /** Storage strategy */
23
+ storage?: 'cookie' | 'localStorage' | 'memory';
24
+ /** Server-side cookie reader (e.g. Next.js `cookies()`) */
25
+ serverCookies?: ServerCookieReader;
26
+ /** Write to `visitor_id` (track.js compat) instead of `fg_visitor_id` (default true) */
27
+ compat?: boolean;
28
+ }
29
+ /**
30
+ * Minimal interface for reading cookies server-side.
31
+ * Compatible with Next.js `cookies()` and Nuxt `useCookie()`.
32
+ */
33
+ export interface ServerCookieReader {
34
+ get(name: string): {
35
+ value: string;
36
+ } | string | undefined | null;
37
+ }
38
+ export declare class IdentityManager {
39
+ private visitorId;
40
+ private sessionId;
41
+ private config;
42
+ /** In-memory store for SSR / Node environments */
43
+ private memoryStore;
44
+ constructor(config?: IdentityConfig);
45
+ /**
46
+ * Get or create a persistent visitor ID.
47
+ *
48
+ * Resolution order:
49
+ * 1. Explicit config.visitorId
50
+ * 2. track.js `visitor_id` cookie/localStorage (backward compat)
51
+ * 3. `fg_visitor_id` cookie
52
+ * 4. `fg_visitor_id` localStorage
53
+ * 5. Server cookie reader (if provided)
54
+ * 6. Generate new UUID v4, persist to cookie + localStorage
55
+ */
56
+ getVisitorId(): string;
57
+ /**
58
+ * Get or create a session ID.
59
+ *
60
+ * - Reads from sessionStorage `fg_session_id`
61
+ * - If absent or expired (>sessionTimeoutMinutes since last activity), creates new
62
+ * - Persists to sessionStorage
63
+ * - Touches timestamp on every call
64
+ */
65
+ getSessionId(): string;
66
+ /**
67
+ * Update the visitor ID (e.g. after identify() links anonymous → known user).
68
+ * Persists to cookie + localStorage.
69
+ */
70
+ setVisitorId(id: string): void;
71
+ /** Clear session (e.g. on logout). */
72
+ clearSession(): void;
73
+ /** Clear all identity (GDPR). Removes visitor + session + experiment assignments. */
74
+ clearAll(): void;
75
+ /** Check if running in a browser environment. */
76
+ isBrowser(): boolean;
77
+ /** Returns the primary cookie name based on compat mode */
78
+ private get visitorCookieName();
79
+ private resolveVisitorId;
80
+ private readLocalStorage;
81
+ private removeLegacyCookie;
82
+ private persistVisitorId;
83
+ private getMemorySessionId;
84
+ private readCookie;
85
+ private readServerCookie;
86
+ private writeCookie;
87
+ private deleteCookie;
88
+ private generateVisitorId;
89
+ private generateSessionId;
90
+ private uuidV4;
91
+ private randomHex;
92
+ private detectBrowser;
93
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @fileoverview FlowGrid storage utilities.
3
+ * @module utils/storage
4
+ *
5
+ * @description
6
+ * Reads visitor identity and UTM attribution data from the same
7
+ * cookie / localStorage / sessionStorage keys used by the FlowGrid
8
+ * tracking scripts (track.js & flowgrid_oss_client_sdk.js).
9
+ *
10
+ * These functions are **read-only** — the tracking scripts remain the
11
+ * single writer. The SDK only consumes these values.
12
+ */
13
+ /**
14
+ * Read a cookie value by name.
15
+ * Returns `null` when the cookie is absent or document.cookie is unavailable.
16
+ */
17
+ export declare function getCookie(name: string): string | null;
18
+ /**
19
+ * Resolve the visitor ID written by track.js / flowgrid_oss_client_sdk.js.
20
+ *
21
+ * Storage locations (checked in order):
22
+ * 1. cookie `visitor_id`
23
+ * 2. localStorage `visitor_id`
24
+ *
25
+ * Returns `null` when neither source contains a value (e.g. tracking
26
+ * script hasn't run yet, or running server-side).
27
+ */
28
+ export declare function getVisitorId(): string | null;
29
+ /**
30
+ * Resolve the session ID written by track.js.
31
+ *
32
+ * Storage: `sessionStorage("session_id")`
33
+ */
34
+ export declare function getSessionId(): string | null;
35
+ /**
36
+ * Resolve the web_id written by track.js.
37
+ *
38
+ * Storage: `localStorage("web_id")`
39
+ */
40
+ export declare function getWebId(): string | null;
41
+ export interface FlowGridUTM {
42
+ utmSource?: string;
43
+ utmMedium?: string;
44
+ utmCampaign?: string;
45
+ utmTerm?: string;
46
+ utmContent?: string;
47
+ ref?: string;
48
+ source?: string;
49
+ via?: string;
50
+ }
51
+ /**
52
+ * Read UTM & attribution parameters persisted by
53
+ * flowgrid_oss_client_sdk.js as `__flowgrid_*` cookies.
54
+ */
55
+ export declare function getUTM(): FlowGridUTM;
56
+ export interface FlowGridUTMHistoryEntry {
57
+ timestamp: string;
58
+ utm_source?: string;
59
+ utm_medium?: string;
60
+ utm_campaign?: string;
61
+ utm_content?: string;
62
+ utm_term?: string;
63
+ }
64
+ /**
65
+ * Read the full UTM history array persisted by flowgrid_oss_client_sdk.js.
66
+ *
67
+ * Checks localStorage first, then the `utm_history` cookie.
68
+ */
69
+ export declare function getUTMHistory(): FlowGridUTMHistoryEntry[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flowgrid-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "A TypeScript SDK for tracking user events, feature usage, experiments, and feature flags with Flowgrid.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -65,6 +65,11 @@
65
65
  "types": "./dist/lib/types/index.d.ts",
66
66
  "import": "./dist/lib/types/index.mjs",
67
67
  "require": "./dist/lib/types/index.js"
68
+ },
69
+ "./consent": {
70
+ "types": "./dist/lib/consent/index.d.ts",
71
+ "import": "./dist/lib/consent/index.mjs",
72
+ "require": "./dist/lib/consent/index.js"
68
73
  }
69
74
  },
70
75
  "scripts": {
@@ -72,7 +77,12 @@
72
77
  "build:browser": "webpack --config webpack.browser.config.js",
73
78
  "build:all": "npm run build && npm run build:browser",
74
79
  "build:types": "tsc --emitDeclarationOnly",
75
- "test": "echo \"No tests specified\" && exit 0",
80
+ "test": "npx tsx tests/featureFlags.test.ts && npx tsx tests/core.test.ts && npx tsx tests/analytics.test.ts && npx tsx tests/ecommerce.test.ts && npx tsx tests/enterprise.test.ts",
81
+ "test:featureFlags": "npx tsx tests/featureFlags.test.ts",
82
+ "test:core": "npx tsx tests/core.test.ts",
83
+ "test:analytics": "npx tsx tests/analytics.test.ts",
84
+ "test:ecommerce": "npx tsx tests/ecommerce.test.ts",
85
+ "test:enterprise": "npx tsx tests/enterprise.test.ts",
76
86
  "lint": "echo \"No linting configured\" && exit 0",
77
87
  "prepublishOnly": "npm run build:all"
78
88
  },
@@ -128,6 +138,7 @@
128
138
  "@types/react": "^18.0.0",
129
139
  "react": "^18.3.1",
130
140
  "ts-loader": "^9.5.1",
141
+ "tsx": "^4.21.0",
131
142
  "typescript": "^5.9.2",
132
143
  "vue": "^3.5.27",
133
144
  "webpack": "^5.105.0",
@@ -1,51 +0,0 @@
1
- /**
2
- * Enterprise SDK Types
3
- */
4
- type EnterpriseType = "engagement" | "cohorts" | "churn" | "monetization" | "multi_path_funnels" | "support" | "acquisition" | "paths" | "alerts" | "security" | "forecasting";
5
- /**
6
- * Enterprise SDK Base Class
7
- * - Flowgrid TypeScript Remote Procedure Call for Enterprise Features
8
- * - Routes to separate enterprise API endpoints
9
- * - Requires enterprise-tier API key
10
- *
11
- * PRIVACY NOTICE:
12
- * This SDK collects event data for analytics purposes. Ensure compliance with
13
- * applicable privacy laws (GDPR, CCPA, etc.) when collecting user data.
14
- * Do not send personally identifiable information without proper consent.
15
- *
16
- * ```typescript
17
- * await this.init('eventName', { type: 'engagement', ... });
18
- * ```
19
- */
20
- export declare class EnterpriseFTRPC {
21
- /**
22
- * Web ID for the specific site or application.
23
- * Used to identify the event source.
24
- */
25
- protected webId: string;
26
- /**
27
- * API endpoint for sending events.
28
- * Example: "https://api.flowgrid.com"
29
- */
30
- protected endpoint: string;
31
- /**
32
- * Enterprise API Key for authenticating requests.
33
- */
34
- private apiKey;
35
- constructor(webId: string, endpoint: string, apiKey: string);
36
- /**
37
- * Sends an event to the Enterprise API.
38
- * Used internally by subclasses to track enterprise events.
39
- *
40
- * @param eventName - Name of the event to track.
41
- * @param properties - Additional properties to send with the event (must include type).
42
- */
43
- protected init(eventName: string, properties: Record<string, any> & {
44
- type: EnterpriseType;
45
- }): Promise<any>;
46
- /**
47
- * Validates input parameters for security and correctness.
48
- */
49
- private validateInput;
50
- }
51
- export type { EnterpriseType };
@@ -1,132 +0,0 @@
1
- /**
2
- * Base SDK Class
3
- * - Flowgrid TypeScript Remote Procedure Call
4
- * - Handles common logic for sending events to the API.
5
- *
6
- * PRIVACY NOTICE:
7
- * This SDK collects event data for analytics purposes. Ensure compliance with
8
- * applicable privacy laws (GDPR, CCPA, etc.) when collecting user data.
9
- * Do not send personally identifiable information without proper consent.
10
- *
11
- * ```typescript
12
- * await this.init('eventName', { key: 'value' });
13
- * ```
14
- */
15
- /**
16
- * FTRPC - Core transport layer for Flowgrid SDK.
17
- *
18
- * @description
19
- * Provides a secure, validated RPC-style transport layer used by all SDK modules.
20
- * Handles:
21
- * - Input validation
22
- * - Payload size enforcement
23
- * - Timeout handling
24
- * - Retry logic (network failures only)
25
- * - Authorization headers
26
- *
27
- * This class is designed to be extended by higher-level modules
28
- * (e.g., FeatureFlag, Analytics, Experiments).
29
- *
30
- * @example
31
- * ```ts
32
- * const client = new FTRPC("web_123", "api.flowgrid.com", "sk_live_xxx");
33
- * ```
34
- */
35
- export declare class FTRPC {
36
- /**
37
- * Unique Web ID identifying the site or application.
38
- * Used by the backend to associate incoming events.
39
- */
40
- protected webId: string;
41
- /**
42
- * Base API endpoint.
43
- * Example values:
44
- * - "api.flowgrid.com"
45
- * - "https://api.flowgrid.com"
46
- *
47
- * Protocol is normalized automatically.
48
- */
49
- protected endpoint: string;
50
- /**
51
- * Private API key used for authenticating requests.
52
- * Sent as a Bearer token in the Authorization header.
53
- */
54
- private apiKey;
55
- /**
56
- * Maximum allowed payload size (in bytes).
57
- * Prevents oversized event submissions.
58
- */
59
- private static readonly MAX_PAYLOAD_SIZE;
60
- /**
61
- * Maximum time (ms) before request is aborted.
62
- */
63
- private static readonly TIMEOUT_MS;
64
- /**
65
- * Maximum retry attempts for network-level failures.
66
- * Does NOT retry HTTP 4xx/5xx responses.
67
- */
68
- private static readonly MAX_RETRIES;
69
- /**
70
- * Creates a new FTRPC transport instance.
71
- *
72
- * @param webId - Unique identifier for the web property.
73
- * @param endpoint - API base endpoint.
74
- * @param apiKey - Private API key for authentication.
75
- *
76
- * @throws {Error} If required parameters are missing.
77
- */
78
- constructor(webId: string, endpoint: string, apiKey: string);
79
- /**
80
- * Core RPC transport method.
81
- *
82
- * @template T Expected JSON response type.
83
- *
84
- * @param eventName - Logical event or function name.
85
- * @param properties - Arbitrary event payload properties.
86
- * @param method - HTTP method ("GET", "POST", "PATCH").
87
- *
88
- * @returns Promise resolving to typed response.
89
- *
90
- * @remarks
91
- * - GET requests are treated as read-only operations.
92
- * - POST/PATCH requests may mutate backend state.
93
- * - Retries occur only for network failures or timeouts.
94
- */
95
- protected init<T>(eventName: string, properties?: Record<string, any>, method?: "GET" | "POST" | "PATCH"): Promise<T>;
96
- /**
97
- * Executes HTTP request with timeout and retry handling.
98
- *
99
- * @template T Expected response type.
100
- *
101
- * @param url - Fully constructed request URL.
102
- * @param method - HTTP method.
103
- * @param body - Optional JSON body (POST/PATCH only).
104
- *
105
- * @throws {Error} On timeout, network failure, or non-OK HTTP status.
106
- */
107
- private executeRequest;
108
- /**
109
- * Validates event name and payload.
110
- *
111
- * @param eventName - Event identifier.
112
- * @param properties - Associated event properties.
113
- *
114
- * @throws {Error} If validation fails.
115
- */
116
- private validateInput;
117
- /**
118
- * Normalizes endpoint input.
119
- * Ensures protocol is present and removes trailing slashes.
120
- *
121
- * @param endpoint - Raw endpoint string.
122
- * @returns Normalized endpoint URL.
123
- */
124
- private normalizeEndpoint;
125
- /**
126
- * Safely reads response text without throwing.
127
- *
128
- * @param res - Fetch Response object.
129
- * @returns Response text or empty string.
130
- */
131
- private safeRead;
132
- }
@@ -1,104 +0,0 @@
1
- import { FTRPC } from "./ftrpc";
2
- type Config = {
3
- eventName: string;
4
- properties: Record<string, any>;
5
- };
6
- type ConfigResponse = {
7
- eventName: string;
8
- properties: Record<string, any>;
9
- response: string | number | boolean;
10
- };
11
- /**
12
- * Flowgrid SDK
13
- * For user activation events (onboarding, signup, etc.)
14
- */
15
- export declare class Activation extends FTRPC {
16
- /**
17
- * Creates and tracks an activation event (onboarding, signup, etc.)
18
- * Adds a "cool" tag to the event for extra flair.
19
- *
20
- * @param eventName - The name of the activation event to track.
21
- * @param properties - Additional properties to send with the event.
22
- * @returns A promise resolving to the API response.
23
- */
24
- create(config: Config): Promise<ConfigResponse>;
25
- }
26
- /**
27
- * TrackFeature
28
- * Effortlessly track feature usage events (button clicks, feature access, etc.)
29
- */
30
- export declare class TrackFeature extends FTRPC {
31
- /**
32
- * Records a feature usage event with optional custom properties.
33
- *
34
- * @param eventName - The name of the feature event to track.
35
- * @param properties - Additional metadata to include with the event.
36
- * @returns A promise resolving to the API response.
37
- */
38
- create(config: Config): Promise<ConfigResponse>;
39
- }
40
- /**
41
- * Represents a specialized event tracker for prompt-related events.
42
- *
43
- * @extends FTRPC
44
- */
45
- export declare class TrackPrompt extends FTRPC {
46
- /**
47
- * Creates and initializes a new prompt event with the specified name and properties.
48
- *
49
- * @param eventName - The name of the event to track.
50
- * @param properties - Optional additional properties to associate with the event.
51
- * These will be merged with default properties such as `type` and `timestamp`.
52
- * @returns A promise that resolves with the result of the initialization.
53
- */
54
- create(config: Config): Promise<ConfigResponse>;
55
- }
56
- /**
57
- * AB TESTING
58
- * For tracking A/B test experiment events.
59
- */
60
- export declare class Experiment extends FTRPC {
61
- /**
62
- * Tracks an A/B test experiment event.
63
- *
64
- * @param experimentName - The name of the experiment.
65
- * @param variant - The variant assigned to the user.
66
- * @param properties - Additional properties to send with the event.
67
- * @returns A promise resolving to the API response.
68
- */
69
- track(config: Config & {
70
- variant: string;
71
- }): Promise<ConfigResponse>;
72
- }
73
- /**
74
- * Binded Experiment
75
- * For binding users to specific experiment variants.
76
- */
77
- export declare class BindedExperiment extends FTRPC {
78
- /**
79
- * Binds a user to a specific experiment variant.
80
- *
81
- * @param experimentName - The name of the experiment.
82
- * @param variant - The variant to bind the user to.
83
- * @param properties - Additional properties to send with the binding.
84
- * @returns A promise resolving to the API response.
85
- */
86
- create(config: Config & {
87
- variant: string;
88
- }): Promise<ConfigResponse>;
89
- }
90
- /**
91
- * Feature Flags
92
- * For evaluating feature flags.
93
- */
94
- export declare class FeatureFlag extends FTRPC {
95
- /**
96
- * Evaluates a feature flag for a given user.
97
- *
98
- * @param flagName - The name of the feature flag to evaluate.
99
- * @param properties - Additional properties to send with the evaluation.
100
- * @returns A promise resolving to the API response.
101
- */
102
- evaluate(config: Config): Promise<ConfigResponse>;
103
- }
104
- export {};