@perspective-ai/sdk 1.0.1 → 1.1.2

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/dist/index.d.ts CHANGED
@@ -8,6 +8,18 @@ export { BRAND_KEYS, BrandKey, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_T
8
8
  */
9
9
 
10
10
  type EmbedType = "widget" | "popup" | "slider" | "float" | "fullpage" | "chat";
11
+ type TriggerConfig = {
12
+ type: "timeout";
13
+ delay: number;
14
+ } | {
15
+ type: "exit-intent";
16
+ };
17
+ type TriggerType = TriggerConfig["type"];
18
+ type ShowOnce = "session" | "visitor" | false;
19
+ interface AutoOpenConfig {
20
+ trigger: TriggerConfig;
21
+ showOnce?: ShowOnce;
22
+ }
11
23
  /** Brand colors that can be passed via embed code */
12
24
  interface BrandColors {
13
25
  /** Primary accent color (buttons, links, focus states) */
@@ -35,6 +47,8 @@ interface EmbedConfig {
35
47
  theme?: ThemeValue;
36
48
  /** Override the default host (defaults to https://getperspective.ai) */
37
49
  host?: string;
50
+ /** Auto-open trigger configuration (popup only) */
51
+ autoOpen?: AutoOpenConfig;
38
52
  /** Callback when embed is ready */
39
53
  onReady?: () => void;
40
54
  /** Callback when interview is submitted/completed */
@@ -137,6 +151,51 @@ declare const createChatBubble: typeof createFloatBubble;
137
151
 
138
152
  declare function createFullpage(config: EmbedConfig): EmbedHandle;
139
153
 
154
+ /**
155
+ * Auto-open trigger system for popup embeds.
156
+ *
157
+ * Supports:
158
+ * - Timeout: Open after a delay (ms)
159
+ * - Exit intent: Open when user moves cursor above viewport
160
+ *
161
+ * Show-once dedup:
162
+ * - "session" -> sessionStorage
163
+ * - "visitor" -> localStorage
164
+ * - false -> always show
165
+ */
166
+
167
+ /**
168
+ * Parse a trigger attribute value into a TriggerConfig.
169
+ *
170
+ * Formats:
171
+ * - "timeout:5000" -> { type: "timeout", delay: 5000 }
172
+ * - "exit-intent" -> { type: "exit-intent" }
173
+ */
174
+ declare function parseTriggerAttr(value: string): TriggerConfig;
175
+ /**
176
+ * Set up a trigger that calls `callback` when fired.
177
+ * Returns a cleanup function to teardown the trigger.
178
+ */
179
+ declare function setupTrigger(config: TriggerConfig, callback: () => void): () => void;
180
+ /**
181
+ * Parse a show-once attribute value into a ShowOnce.
182
+ *
183
+ * Formats:
184
+ * - "session" -> "session"
185
+ * - "visitor" -> "visitor"
186
+ * - "false" -> false
187
+ * - anything else -> defaults to "session"
188
+ */
189
+ declare function parseShowOnceAttr(value: string | null): ShowOnce;
190
+ /**
191
+ * Check if the popup should be shown based on show-once dedup.
192
+ */
193
+ declare function shouldShow(researchId: string, showOnce: ShowOnce): boolean;
194
+ /**
195
+ * Mark the popup as shown for dedup purposes.
196
+ */
197
+ declare function markShown(researchId: string, showOnce: ShowOnce): void;
198
+
140
199
  /**
141
200
  * Embed SDK configuration
142
201
  * SSR-safe - DOM access is guarded and lazy
@@ -152,4 +211,4 @@ declare function configure(config: SDKConfig): void;
152
211
  */
153
212
  declare function getConfig(): SDKConfig;
154
213
 
155
- export { type BrandColors, type EmbedConfig, type EmbedError, type EmbedHandle, type EmbedInstance, type EmbedType, type FloatHandle, type ModalHandle, type SDKConfig, type ThemeConfig, ThemeValue, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, openPopup, openSlider };
214
+ export { type AutoOpenConfig, type BrandColors, type EmbedConfig, type EmbedError, type EmbedHandle, type EmbedInstance, type EmbedType, type FloatHandle, type ModalHandle, type SDKConfig, type ShowOnce, type ThemeConfig, ThemeValue, type TriggerConfig, type TriggerType, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, markShown, openPopup, openSlider, parseShowOnceAttr, parseTriggerAttr, setupTrigger, shouldShow };
package/dist/index.js CHANGED
@@ -56,7 +56,7 @@ if (hasDom()) {
56
56
  }
57
57
 
58
58
  // src/constants.ts
59
- var SDK_VERSION = "1.0.0";
59
+ var SDK_VERSION = "1.1.2";
60
60
  var FEATURES = {
61
61
  RESIZE: 1 << 0,
62
62
  // 0b0001
@@ -85,26 +85,9 @@ var BRAND_KEYS = {
85
85
  darkBg: "brand.dark.bg",
86
86
  darkText: "brand.dark.text"
87
87
  };
88
- var UTM_PARAMS = [
89
- "utm_source",
90
- "utm_medium",
91
- "utm_campaign",
92
- "utm_term",
93
- "utm_content"
94
- ];
95
88
  var RESERVED_PARAMS = /* @__PURE__ */ new Set([
96
- PARAM_KEYS.embed,
97
- PARAM_KEYS.embedType,
98
- PARAM_KEYS.theme,
99
- BRAND_KEYS.primary,
100
- BRAND_KEYS.secondary,
101
- BRAND_KEYS.bg,
102
- BRAND_KEYS.text,
103
- BRAND_KEYS.darkPrimary,
104
- BRAND_KEYS.darkSecondary,
105
- BRAND_KEYS.darkBg,
106
- BRAND_KEYS.darkText,
107
- ...UTM_PARAMS
89
+ ...Object.values(PARAM_KEYS),
90
+ ...Object.values(BRAND_KEYS)
108
91
  ]);
109
92
  var DATA_ATTRS = {
110
93
  widget: "data-perspective-widget",
@@ -119,7 +102,9 @@ var DATA_ATTRS = {
119
102
  brand: "data-perspective-brand",
120
103
  brandDark: "data-perspective-brand-dark",
121
104
  theme: "data-perspective-theme",
122
- noStyle: "data-perspective-no-style"
105
+ noStyle: "data-perspective-no-style",
106
+ autoOpen: "data-perspective-auto-open",
107
+ showOnce: "data-perspective-show-once"
123
108
  };
124
109
  var MESSAGE_TYPES = {
125
110
  // SDK -> Iframe (initialization)
@@ -148,7 +133,8 @@ var THEME_VALUES = {
148
133
  system: "system"
149
134
  };
150
135
  var STORAGE_KEYS = {
151
- anonId: "perspective-anon-id"
136
+ anonId: "perspective-anon-id",
137
+ triggerShown: "perspective-trigger-shown"
152
138
  };
153
139
 
154
140
  // src/utils.ts
@@ -218,13 +204,12 @@ function getOrCreateAnonId() {
218
204
  return crypto.randomUUID();
219
205
  }
220
206
  }
221
- function getUtmParams() {
207
+ function getParentSearchParams() {
222
208
  if (!hasDom()) return {};
223
209
  const params = {};
224
210
  const searchParams = new URLSearchParams(window.location.search);
225
- for (const key of UTM_PARAMS) {
226
- const value = searchParams.get(key);
227
- if (value) {
211
+ for (const [key, value] of searchParams.entries()) {
212
+ if (!RESERVED_PARAMS.has(key)) {
228
213
  params[key] = value;
229
214
  }
230
215
  }
@@ -247,8 +232,8 @@ function buildIframeUrl(researchId, type, host, customParams, brand, themeOverri
247
232
  } else {
248
233
  url.searchParams.set(PARAM_KEYS.theme, themeOverride || THEME_VALUES.light);
249
234
  }
250
- const utmParams = getUtmParams();
251
- for (const [key, value] of Object.entries(utmParams)) {
235
+ const parentParams = getParentSearchParams();
236
+ for (const [key, value] of Object.entries(parentParams)) {
252
237
  url.searchParams.set(key, value);
253
238
  }
254
239
  const setColor = (key, color) => {
@@ -1570,6 +1555,74 @@ function createFullpage(config) {
1570
1555
  };
1571
1556
  }
1572
1557
 
1573
- export { BRAND_KEYS, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_TYPES, PARAM_KEYS, SDK_VERSION, THEME_VALUES, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, openPopup, openSlider };
1558
+ // src/triggers.ts
1559
+ function parseTriggerAttr(value) {
1560
+ const trimmed = value.trim();
1561
+ if (trimmed.startsWith("timeout:")) {
1562
+ const delay = parseInt(trimmed.slice("timeout:".length), 10);
1563
+ if (isNaN(delay) || delay < 0) {
1564
+ throw new Error(`Invalid timeout delay: "${value}"`);
1565
+ }
1566
+ return { type: "timeout", delay };
1567
+ }
1568
+ if (trimmed === "timeout") {
1569
+ return { type: "timeout", delay: 5e3 };
1570
+ }
1571
+ if (trimmed === "exit-intent") {
1572
+ return { type: "exit-intent" };
1573
+ }
1574
+ throw new Error(
1575
+ `Unknown trigger type: "${value}". Expected "timeout:<ms>" or "exit-intent".`
1576
+ );
1577
+ }
1578
+ function setupTrigger(config, callback) {
1579
+ if (config.type === "timeout") {
1580
+ const timer = setTimeout(callback, config.delay);
1581
+ return () => clearTimeout(timer);
1582
+ }
1583
+ if (config.type === "exit-intent") {
1584
+ const handler = (e) => {
1585
+ if (e.clientY <= 0) {
1586
+ callback();
1587
+ document.removeEventListener("mouseleave", handler);
1588
+ }
1589
+ };
1590
+ document.addEventListener("mouseleave", handler);
1591
+ return () => document.removeEventListener("mouseleave", handler);
1592
+ }
1593
+ const _exhaustive = config;
1594
+ throw new Error(
1595
+ `Unknown trigger type: ${_exhaustive.type}`
1596
+ );
1597
+ }
1598
+ function storageKey(researchId) {
1599
+ return `${STORAGE_KEYS.triggerShown}:${researchId}`;
1600
+ }
1601
+ function parseShowOnceAttr(value) {
1602
+ if (!value) return "session";
1603
+ const trimmed = value.trim();
1604
+ if (trimmed === "visitor") return "visitor";
1605
+ if (trimmed === "false") return false;
1606
+ return "session";
1607
+ }
1608
+ function shouldShow(researchId, showOnce) {
1609
+ if (showOnce === false) return true;
1610
+ try {
1611
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
1612
+ return storage.getItem(storageKey(researchId)) === null;
1613
+ } catch {
1614
+ return true;
1615
+ }
1616
+ }
1617
+ function markShown(researchId, showOnce) {
1618
+ if (showOnce === false) return;
1619
+ try {
1620
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
1621
+ storage.setItem(storageKey(researchId), "1");
1622
+ } catch {
1623
+ }
1624
+ }
1625
+
1626
+ export { BRAND_KEYS, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_TYPES, PARAM_KEYS, SDK_VERSION, THEME_VALUES, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, markShown, openPopup, openSlider, parseShowOnceAttr, parseTriggerAttr, setupTrigger, shouldShow };
1574
1627
  //# sourceMappingURL=index.js.map
1575
1628
  //# sourceMappingURL=index.js.map