@qaiddev/quests-embed 1.6.1 → 1.9.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
@@ -30,7 +30,8 @@ new QaidQuests({
30
30
  <div id="form"></div>
31
31
 
32
32
  <script
33
- src="https://unpkg.com/@qaiddev/quests-embed/dist/qaid-quests.umd.cjs"
33
+ src="https://unpkg.com/@qaiddev/quests-embed/dist/embed.js"
34
+ defer
34
35
  data-endpoint="https://qaid.dev/api/responses"
35
36
  data-api-key="YOUR_API_KEY"
36
37
  data-config-url="/forms/intake.json"
@@ -40,6 +41,8 @@ new QaidQuests({
40
41
 
41
42
  The embed auto-initializes when it detects a `data-endpoint` attribute on its script tag and either `data-config-url` or an inline JSON config.
42
43
 
44
+ `dist/embed.js` is a 0.7 KB loader (gzipped). It loads the quest code (about 16 KB) when the container comes within a screen's height of the viewport, or once the page has loaded and the browser is idle, whichever is first. Without a `data-container` it loads at once. Pointing the tag straight at `dist/qaid-quests.umd.cjs` still works and loads everything up front.
45
+
43
46
  ### JSON Config (Script Tag)
44
47
 
45
48
  For complex configurations or inline questionnaires, use a separate JSON config element:
@@ -69,7 +72,7 @@ For complex configurations or inline questionnaires, use a separate JSON config
69
72
  }
70
73
  }
71
74
  </script>
72
- <script src="https://unpkg.com/@qaiddev/quests-embed/dist/qaid-quests.umd.cjs"></script>
75
+ <script src="https://unpkg.com/@qaiddev/quests-embed/dist/embed.js" defer></script>
73
76
  ```
74
77
 
75
78
  ## How It Works
@@ -252,7 +255,9 @@ For programmatic embedding — e.g. launching a quest from another widget — th
252
255
 
253
256
  | Option | Type | Default | Description |
254
257
  |--------|------|---------|-------------|
255
- | `metadata` | `Record<string, unknown>` | — | Extra key/value pairs sent verbatim in the create-response `POST` body. The server decides which keys it persists; unknown keys are ignored. Used, for example, by `@qaiddev/thumbs-embed` to pass `{ feedbackId }` so the QAid backend joins the quest answers to the feedback record. |
258
+ | `metadata` | `Record<string, unknown>` or a `Promise` of one | — | Extra key/value pairs sent verbatim in the create-response `POST` body. The server decides which keys it persists; unknown keys are ignored. Used, for example, by `@qaiddev/thumbs-embed` to pass `{ feedbackId }` so the QAid backend joins the quest answers to the feedback record. A promise lets you open the quest before you have the ids: the form shows at once and only the create-response call waits for it. Check `QaidQuests.supports.asyncMetadata` first if you load the widget at runtime. |
259
+ | `pageUrl` | `string` | `window.location.href` | The page the response is about. It is stored with the response and checked against the project's Domain Restriction. Set it when the quest runs somewhere else, such as inside a native app's web view. |
260
+ | `visitorId` | `string` | an id kept in this browser | The visitor id sent with the response. Pass your own to tie it to feedback you sent with the same id. |
256
261
  | `onComplete` | `(answers) => void` | — | Called once when the quest is completed and the server accepted the submit (just after the thank-you screen renders), with a copy of the submitted answers — answers to questions hidden by `visibleIf` are left out. Not called while the submit is failing. Fires in both modal and inline modes. |
257
262
  | `onClose` | `() => void` | — | Called once when the embed is torn down — visitor close, host `destroy()`, or teardown after completion. Lets a host that launched the quest drop its reference. |
258
263
 
@@ -332,6 +337,8 @@ Object options take JSON. In HTML, wrap the value in single quotes so the JSON's
332
337
  | `data-theme-url` | `themeUrl` |
333
338
  | `data-theme-document` | `themeDocument`, as a JSON object |
334
339
  | `data-metadata` | `metadata`, as a JSON object |
340
+ | `data-page-url` | `pageUrl` |
341
+ | `data-visitor-id` | `visitorId` |
335
342
 
336
343
  `onComplete` and `onClose` are functions, so they can only be passed from code (`new QaidQuests({ ... })`).
337
344
 
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Single-tag embed loader (`dist/embed.js`).
3
+ *
4
+ * A tiny classic `<script>` that holds back the quest code (the UMD bundle,
5
+ * `qaid-quests.umd.cjs`) until it is needed, so a quest further down the page
6
+ * costs the page nothing while it loads. The bundle is injected when the first
7
+ * of these happens:
8
+ *
9
+ * - the quest's container comes within a screen's height of the viewport;
10
+ * - the page has finished loading and the browser is idle, so the quest is
11
+ * ready before the visitor scrolls to it;
12
+ * - right away, when there is no container to watch (the modal install) or
13
+ * the browser has no IntersectionObserver.
14
+ *
15
+ * The injected bundle initializes itself from the `data-*` attributes copied
16
+ * across from this tag (or from a `<script type="application/json">` config
17
+ * block already on the page), exactly as if the page had loaded it directly.
18
+ *
19
+ * Usage:
20
+ * <div id="quest-mount"></div>
21
+ * <script src="https://unpkg.com/@qaiddev/quests-embed/dist/embed.js" defer
22
+ * data-endpoint="/api/responses" data-config-url="/forms/intake.json"
23
+ * data-container="#quest-mount"></script>
24
+ *
25
+ * IMPORTANT: this file must stay ES5-clean and dependency-free, like the
26
+ * thumbs-embed loader it mirrors: it is the first thing to run on every
27
+ * browser. Written with var/function on purpose.
28
+ */
package/dist/embed.d.ts CHANGED
@@ -10,6 +10,13 @@ import type { Answers, Questionnaire, QuestsConfig } from "./types";
10
10
  */
11
11
  export declare function randomId(): string;
12
12
  export declare class QaidQuests {
13
+ /**
14
+ * What this build accepts, for hosts that load it at runtime and may get
15
+ * an older copy. asyncMetadata: `metadata` may be a promise.
16
+ */
17
+ static readonly supports: {
18
+ readonly asyncMetadata: true;
19
+ };
13
20
  private config;
14
21
  private questionnaire;
15
22
  private inlineQuestionnaire;
package/dist/embed.js ADDED
@@ -0,0 +1,2 @@
1
+ (function(){"use strict";(function(){if(typeof document=="undefined")return;function s(){var e=document.currentScript;if(e&&e.src)return e;for(var t=document.getElementsByTagName("script"),n=t.length-1;n>=0;n--){var r=t[n].src||"";if(r.indexOf("quests-embed")!==-1&&/\/embed(\.min)?\.js(\?|#|$)/.test(r))return t[n]}return null}var i=s();if(!i||!i.src)return;var a=i,f=a.src.substring(0,a.src.lastIndexOf("/")+1),u=!1;function d(){if(!u){u=!0;for(var e=document.createElement("script"),t=a.attributes,n=0;n<t.length;n++){var r=t[n];r.name.indexOf("data-")===0&&e.setAttribute(r.name,r.value)}e.src=f+"qaid-quests.umd.cjs",(document.head||document.documentElement).appendChild(e)}}function l(){function e(){var t=window;typeof t.requestIdleCallback=="function"?t.requestIdleCallback(d,{timeout:3e3}):setTimeout(d,200)}document.readyState==="complete"?e():window.addEventListener("load",e)}function c(){var e=a.getAttribute("data-container"),t=e?document.querySelector(e):null;if(!t||typeof IntersectionObserver=="undefined"){d();return}var n=new IntersectionObserver(function(r){for(var o=0;o<r.length;o++)if(r[o].isIntersecting){n.disconnect(),d();return}},{rootMargin:"100% 0px"});n.observe(t),l()}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",c):c()})()})();
2
+ //# sourceMappingURL=embed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embed.js","sources":["../src/embed-loader.ts"],"sourcesContent":["/**\n * Single-tag embed loader (`dist/embed.js`).\n *\n * A tiny classic `<script>` that holds back the quest code (the UMD bundle,\n * `qaid-quests.umd.cjs`) until it is needed, so a quest further down the page\n * costs the page nothing while it loads. The bundle is injected when the first\n * of these happens:\n *\n * - the quest's container comes within a screen's height of the viewport;\n * - the page has finished loading and the browser is idle, so the quest is\n * ready before the visitor scrolls to it;\n * - right away, when there is no container to watch (the modal install) or\n * the browser has no IntersectionObserver.\n *\n * The injected bundle initializes itself from the `data-*` attributes copied\n * across from this tag (or from a `<script type=\"application/json\">` config\n * block already on the page), exactly as if the page had loaded it directly.\n *\n * Usage:\n * <div id=\"quest-mount\"></div>\n * <script src=\"https://unpkg.com/@qaiddev/quests-embed/dist/embed.js\" defer\n * data-endpoint=\"/api/responses\" data-config-url=\"/forms/intake.json\"\n * data-container=\"#quest-mount\"></script>\n *\n * IMPORTANT: this file must stay ES5-clean and dependency-free, like the\n * thumbs-embed loader it mirrors: it is the first thing to run on every\n * browser. Written with var/function on purpose.\n */\n\n(function () {\n if (typeof document === \"undefined\") return;\n\n // Locate this script tag: prefer document.currentScript (valid for a plain\n // classic script), else find the last <script> whose src looks like embed.js.\n function findSelf(): HTMLScriptElement | null {\n var cur = document.currentScript as HTMLScriptElement | null;\n if (cur && cur.src) return cur;\n var scripts = document.getElementsByTagName(\"script\");\n for (var i = scripts.length - 1; i >= 0; i--) {\n var src = scripts[i].src || \"\";\n if (src.indexOf(\"quests-embed\") !== -1 && /\\/embed(\\.min)?\\.js(\\?|#|$)/.test(src)) {\n return scripts[i];\n }\n }\n return null;\n }\n\n var self = findSelf();\n if (!self || !self.src) return;\n var tag = self;\n\n // Base = the directory that holds embed.js, e.g.\n // \"https://unpkg.com/@qaiddev/quests-embed@1.9.0/dist/\", so the bundle comes\n // from the same (CDN-versioned) place.\n var base = tag.src.substring(0, tag.src.lastIndexOf(\"/\") + 1);\n\n var loaded = false;\n function load(): void {\n if (loaded) return;\n loaded = true;\n var entry = document.createElement(\"script\");\n // Carry the config across so the bundle's auto-init sees it.\n var attrs = tag.attributes;\n for (var j = 0; j < attrs.length; j++) {\n var a = attrs[j];\n if (a.name.indexOf(\"data-\") === 0) entry.setAttribute(a.name, a.value);\n }\n entry.src = base + \"qaid-quests.umd.cjs\";\n (document.head || document.documentElement).appendChild(entry);\n }\n\n function whenIdleAfterLoad(): void {\n function idle(): void {\n var w = window as Window & {\n requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => number;\n };\n if (typeof w.requestIdleCallback === \"function\") w.requestIdleCallback(load, { timeout: 3000 });\n else setTimeout(load, 200);\n }\n if (document.readyState === \"complete\") idle();\n else window.addEventListener(\"load\", idle);\n }\n\n function start(): void {\n var selector = tag.getAttribute(\"data-container\");\n var container = selector ? document.querySelector(selector) : null;\n if (!container || typeof IntersectionObserver === \"undefined\") {\n load();\n return;\n }\n var observer = new IntersectionObserver(\n function (entries) {\n for (var k = 0; k < entries.length; k++) {\n if (entries[k].isIntersecting) {\n observer.disconnect();\n load();\n return;\n }\n }\n },\n { rootMargin: \"100% 0px\" },\n );\n observer.observe(container);\n whenIdleAfterLoad();\n }\n\n // The container may come after the tag in the page, so look once it's parsed.\n if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", start);\n else start();\n})();\n"],"names":["findSelf","cur","scripts","i","src","self","tag","base","loaded","load","entry","attrs","j","a","whenIdleAfterLoad","idle","w","start","selector","container","observer","entries","k"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