@axceera/playstep-analytics 0.0.0-stage → 0.11.3

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 PlayStep
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+
18
+ Icons: Lucide (https://lucide.dev), ISC License. Copyright (c) for portions of Lucide are held by Cole Bemis 2013-2022
19
+ as part of Feather (MIT). All other copyright (c) for Lucide are held by Lucide Contributors 2022.
package/README.md CHANGED
@@ -1,3 +1,53 @@
1
- # Temporary Holding Version
1
+ # @axceera/playstep-analytics
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Insights, PlayStep's product analytics, as an add-on to [`@axceera/playstep`](https://www.npmjs.com/package/@axceera/playstep):
4
+ pages, clicks, rage clicks, form submits and your own events, for the people using your app. Apps that don't add it
5
+ carry none of it.
6
+
7
+ ```bash
8
+ npm install @axceera/playstep @axceera/playstep-analytics
9
+ ```
10
+
11
+ ```ts
12
+ import { playstep } from '@axceera/playstep'
13
+ import { analytics } from '@axceera/playstep-analytics'
14
+
15
+ const insights = analytics(playstep) // before or after playstep.init()
16
+ playstep.identify(user.id, { plan: user.plan })
17
+ playstep.track('invoice_sent', { amount: 49 })
18
+ insights.group('company', account.id, { seats: 12 })
19
+ ```
20
+
21
+ It captures only once Insights is turned on for the app in the dashboard, so it can ship before then. With the script
22
+ tag there is nothing to install: the loader fetches the same code (`insights.js`, checked against its hash) for an app
23
+ with Insights on. React apps on `@axceera/playstep-react` call `useInsights()` from `@axceera/playstep-analytics/react`.
24
+
25
+ - **Signed-in users only, by default.** Until `identify()`, up to 100 events wait in memory, then go under the user.
26
+ Allowing anonymous tracking (a dashboard setting) tracks a random device id instead, joined to the user when they
27
+ sign in.
28
+ - **Ids are hashed in the browser** with the environment's analytics key. Ids, emails and names are stored only where
29
+ the app turns identities on (Pro and Business).
30
+ - **Consent:** start `pending` (events wait in memory) or `denied` in the dashboard, then call
31
+ `insights.setConsent(true | false)` from your banner. The answer is remembered in the browser, and withdrawing it
32
+ drops what was not sent yet: events waiting, batches saved in the browser and their retries.
33
+ - **Clicks record the element, never what was typed:** its selector, words, role and link (without its query), and
34
+ the page it was on (the path PlayStep last saw, so heatmaps and reports can place it). Elements marked
35
+ `data-private` or `data-ps-no-capture`, hidden ones, PlayStep's own UI and the app's mask list are skipped, and their
36
+ words never show in a bigger element's. A click inside a PlayStep anchor (`data-guide`) that is no control of its
37
+ own is recorded without words. `analytics(playstep, { mask: ['.billing'] })` adds selectors for one page, and
38
+ `autocapture: { clicks: false }` turns a part off; `{ text: false }`, or the dashboard's "Words on clicked
39
+ elements", keeps clicks without their words.
40
+ - **Needs a secure page** (https or localhost): it hashes with the browser's `crypto.subtle`. Without one it stays off
41
+ and says so once in the console.
42
+ - Batches go to `POST /v1/batch`, kept in local storage and retried when offline, never on the guide queue.
43
+
44
+ API: `setConsent(granted)`, `register(props)` / `unregister(key)` (properties on every event), `group(type, id,
45
+ traits?)` (at most 5 kinds), `flush()`. On sign-out, `playstep.identify('')` stops filing events under the user and keeps
46
+ their guide progress. What follows goes under no one until the next sign-in: it is dropped, or with anonymous tracking
47
+ on it goes under a new device id, so it never lands with whoever signs in next. Consent and properties stay, groups
48
+ go. `playstep.reset()` forgets the user, the device and those properties too. Script tag: `(window.playstepInsights ||= []).push(['setConsent', true])` works before it loads.
49
+
50
+ About 3.5 KB gzipped from npm (the event queue comes from `@axceera/playstep`), 4.2 KB as the script tag.
51
+
52
+ A page view takes the title the page has half a second after the URL changes, since single-page apps set it after
53
+ navigating. Calling `identify()` again for the same user sends `$identify` only when the traits changed.
@@ -0,0 +1,51 @@
1
+ import { PlayStep, PlayStepPlugin, QueueOptions } from "@axceera/playstep/plugin";
2
+ //#region ../spec/src/types.d.ts
3
+ /** Insights' settings, present when the app turned analytics on (see analyticsConfigSchema). */
4
+ interface AnalyticsConfig {
5
+ url: string;
6
+ hash_key: string;
7
+ anonymous: boolean;
8
+ consent: 'granted' | 'pending' | 'denied';
9
+ /** `text` (PLAY-107) is missing from configs published before it: missing means on. */
10
+ autocapture: {
11
+ pages: boolean;
12
+ clicks: boolean;
13
+ forms: boolean;
14
+ rage: boolean;
15
+ dead: boolean;
16
+ text: boolean;
17
+ };
18
+ identities: boolean;
19
+ mask: string[];
20
+ }
21
+ //#endregion
22
+ //#region src/insights.d.ts
23
+ type Autocapture = AnalyticsConfig['autocapture'];
24
+ type Props = Record<string, unknown>;
25
+ interface InsightsOptions {
26
+ /** Over the app's autocapture settings for this page: `{ clicks: false }`, or `{ dead: true }`. */
27
+ autocapture?: Partial<Autocapture>;
28
+ /** More CSS selectors whose elements are never captured, besides the app's mask list. */
29
+ mask?: string[];
30
+ /** Tests and the e2e harness: replaces fetch and sendBeacon. */
31
+ send?: QueueOptions['send'];
32
+ }
33
+ interface Insights {
34
+ /** The add-on itself, for playstep.use() (analytics() attaches it for you). */
35
+ plugin: PlayStepPlugin;
36
+ /** Your consent banner's answer, remembered in this browser: true captures, false stops and forgets the device. */
37
+ setConsent: (granted: boolean) => void;
38
+ /** Properties sent with every event from now on, until unregister() or reset(). */
39
+ register: (props: Props) => void;
40
+ unregister: (key: string) => void;
41
+ /** Puts the user in a company (or another kind of group, at most 5 kinds), with the group's traits. */
42
+ group: (type: string, id: string, traits?: Props) => void;
43
+ /** Sends what is waiting now. */
44
+ flush: () => void;
45
+ }
46
+ //#endregion
47
+ //#region src/index.d.ts
48
+ /** Attaches Insights to PlayStep, once per SDK (a second call returns the first one's API). */
49
+ declare function analytics(playstep: Pick<PlayStep, 'use'>, options?: InsightsOptions): Insights;
50
+ //#endregion
51
+ export { Props as a, InsightsOptions as i, Autocapture as n, Insights as r, analytics as t };
@@ -0,0 +1,2 @@
1
+ import { a as Props, i as InsightsOptions, n as Autocapture, r as Insights, t as analytics } from "./index-BriEsBVD.js";
2
+ export { type Autocapture, type Insights, type InsightsOptions, type Props, analytics };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export{t as analytics}from"./src-DzSCbLW0.js";
@@ -0,0 +1,6 @@
1
+ import { i as InsightsOptions, r as Insights } from "./index-BriEsBVD.js";
2
+ //#region src/react.d.ts
3
+ /** Attaches Insights to PlayStep (the first call's options win) and returns its API. */
4
+ export declare function useInsights(options?: InsightsOptions): Insights;
5
+ //#endregion
6
+ export type { Insights, InsightsOptions };
package/dist/react.js ADDED
@@ -0,0 +1 @@
1
+ import{t as e}from"./src-DzSCbLW0.js";import{playstep as t}from"@axceera/playstep";function n(n){return e(t,n)}export{n as useInsights};
@@ -0,0 +1 @@
1
+ import{createQueue as e,store as t}from"@axceera/playstep/plugin";const n=/^(:|radix-|headlessui-|react-|ember\d|mui-|v-|css-|sc-|jsx-)|\d{3,}|[a-f0-9]{8,}|__/i,r=/active|selected|current|open|hover|focus|disabled|hidden|show|visible|checked|loading/i,i=e=>`"${e.replace(/["\\]/g,`\\$&`)}"`,a=e=>CSS.escape(e);function o(e,t){try{return e.querySelectorAll(t).length===1}catch{return!1}}function s(e){let t=e.getAttribute(`role`),n=e.tagName,r=(e.getAttribute(`type`)??``).toLowerCase();return t===`tab`||t===`menuitem`||t===`link`||t===`button`?t:t===`checkbox`||t===`switch`||n===`INPUT`&&(r===`checkbox`||r===`radio`)?`checkbox`:n===`A`?`link`:n===`BUTTON`||n===`SUMMARY`||n===`INPUT`&&/^(submit|button|reset|image)$/.test(r)?`button`:n===`SELECT`?`select`:n===`INPUT`||n===`TEXTAREA`?`input`:`other`}function c(e){let t=e.ownerDocument,s=e.tagName.toLowerCase(),c=[];e.id&&!n.test(e.id)&&c.push(`#${a(e.id)}`);for(let t of[`data-testid`,`data-test`,`data-cy`,`data-qa`]){let n=e.getAttribute(t);n&&c.push(`[${t}=${i(n)}]`)}for(let t of[`aria-label`,`name`,`title`,`placeholder`]){let n=e.getAttribute(t);n&&n.length<=80&&c.push(`${s}[${t}=${i(n)}]`)}let l=e.getAttribute(`href`);s===`a`&&l&&!/^javascript:|[?#]/i.test(l)&&l.length<=200&&c.push(`a[href=${i(l)}]`);let u=[...e.classList].filter(e=>/^[a-z][\w-]{1,40}$/i.test(e)&&!n.test(e)&&!r.test(e));u.length&&c.push(`${s}.${u.slice(0,3).map(a).join(`.`)}`);for(let e of c)if(o(t,e))return e;let d=[],f=e;for(;f&&d.length<7;){if(f===t.body){d.unshift(`body`);break}if(f!==e&&f.id&&!n.test(f.id)){d.unshift(`#${a(f.id)}`);break}let r=f.tagName.toLowerCase(),i=f.parentElement?[...f.parentElement.children].filter(e=>e.tagName===f.tagName):[];d.unshift(i.length>1?`${r}:nth-of-type(${i.indexOf(f)+1})`:r),f=f.parentElement}let p=d.join(`>`);return o(t,p)?p:void 0}function l(e){let t=e.getAttribute(`aria-label`)||e.getAttribute(`title`);if(t)return t;let n=e.getAttribute(`aria-labelledby`);if(n)return n.split(/\s+/).map(t=>e.ownerDocument.getElementById(t)?.textContent??``).join(` `);let r=e.labels;return r?.length?r[0].innerText:``}function u(e,t=[]){if(e.closest(`[data-private],[aria-hidden=true],[inert],[data-ps-no-capture],playstep-root,playstep-capture`))return!0;for(let n of t)try{if(e.closest(n))return!0}catch{}return!1}const d=()=>crypto.randomUUID(),f=async e=>`u_`+[...new Uint8Array(await crypto.subtle.digest(`SHA-256`,new TextEncoder().encode(e)),0,20)].map(e=>e.toString(16).padStart(2,`0`)).join(``),p=e=>/^(string|number|boolean)$/.test(typeof e),m=e=>Object.keys(e).length,h=e=>(e??``).replace(/\s+/g,` `).trim().slice(0,80),g=e=>{try{localStorage.removeItem(`playstep:${e}`)}catch{}},_=()=>void 0;function v(e,t=!0){let n={};for(let r in e){let i=e[r];m(n)<50&&/^[A-Za-z_$][\w.:-]{0,63}$/.test(r)&&(i===null||p(i)||t&&Array.isArray(i)&&i.every(p))&&(n[r]=Array.isArray(i)?i.slice(0,25):i)}return n}function y(){let e={};for(let[t,n]of new URLSearchParams(location.search))t.startsWith(`utm_`)&&(e[t.slice(4)]=n);let t=document.referrer&&new URL(document.referrer);return{r:t&&t.host!=location.host?t.origin:void 0,u:m(e)?e:void 0}}function b(e,t,n,r){let i=``;if(n&&!e.matches(`input,select,textarea`)){i=e.innerText;for(let t of e.querySelectorAll(`*`)){let e=u(t,r)&&t.innerText;e&&(i=i.includes(e)?i.replace(e,``):``)}}let a=e.href;return{selector:c(e),text:n&&(h(i)||h(l(e)))||void 0,role:s(e),href:typeof a==`string`&&a.split(/[?#]/)[0]||void 0,x:t.pageX|0,y:t.pageY|0,vw:innerWidth,vh:innerHeight}}function x(n={}){let r,i=``,a,o,s=[],p,x,S,C=!1,w,T,E,D={},O,k={},A={},j=[],M=e=>{let n=t.get(`a:d`);return!n&&e&&t.set(`a:d`,n=`d_${d()}`),n},N=()=>r&&(T??(!w&&r.anonymous?M(!0):void 0)),P=()=>{let e=N();if(!e)return null;let n=t.get(`a:s`),o=e=>typeof e==`string`?e:void 0;return{v:1,key:i,sdk:a,actor:e,device:T&&M()||void 0,guide_key:E,traits:m(D)?D:void 0,user:r.identities&&w?{id:w,email:o(D.email),name:o(D.name)}:void 0,groups:m(k)?k:void 0,context:{platform:`web`,locale:navigator.language,referrer:n?.r,utm:n?.u}}};function F(e){let n=Date.now(),r=t.get(`a:s`);(!r||n-r.t>18e5)&&(r={id:d(),t:n,...y()},x.push({id:d(),name:`$session_start`,ts:e.ts,session:r.id})),r.t=n,t.set(`a:s`,r),x.push({...e,session:r.id})}let I=()=>p==`granted`&&N()&&j.splice(0).forEach(F);function L(e,t={}){if(!r||p==`denied`)return;let n={...A,...t.props},i={id:d(),name:e,ts:Date.now(),page:t.el&&O,...t,props:m(n)?n:void 0};p==`granted`&&N()?F(i):j.length<100&&w!==``&&j.push(i)}function R(e,t){let n=JSON.stringify(D)==JSON.stringify(D=v(t,!1));if(e==w&&e)return void(T&&!n&&L(`$identify`));if(x.flush(),w&&!e&&(j=[],g(`a:d`)),w=e||w&&``,T=E=void 0,!e)return void(k={});Promise.all([f(`${r.hash_key}:${e}`),f(`${i}:${e}`)]).then(t=>{w==e&&([T,E]=t,L(`$identify`),I())})}function z(e){let t=O={path:e};o.pages&&L(`$pageview`,{page:t}),setTimeout(()=>O==t&&(t.title=document.title),500)}function B(){R(``,{}),A={},j=[],g(`a:d`),g(`a:s`)}function V(){S={url:r.url,storageKey:`playstep:a:q:${i}`,envelope:P,send:p==`denied`?_:n.send},x=e(S)}function H(){let e=[];addEventListener(`click`,t=>{let n=t.target;if(!(n instanceof Element))return;let r=n.closest(`a[href],button,input:not([type=hidden]),select,textarea,summary,[role=button],[role=link],[role=tab],[role=menuitem],[role=checkbox],[role=switch],[data-guide]`);if(u(r??n,s))return;let i=!!r?.matches(`a[href],button,input:not([type=hidden]),select,textarea,summary,[role=button],[role=link],[role=tab],[role=menuitem],[role=checkbox],[role=switch]`)&&o.text!=0;if(o.rage&&(e=e.filter(e=>t.timeStamp-e.t<700&&Math.abs(e.x-t.clientX)<30&&Math.abs(e.y-t.clientY)<30),e.push({t:t.timeStamp,x:t.clientX,y:t.clientY})==3&&L(`$rage_click`,{el:b(r??n,t,i,s)})),!r)return;let a=b(r,t,i,s);if(o.clicks&&L(`$click`,{el:a}),o.dead){let e=!1,t=location.href,n=new MutationObserver(()=>e=!0);n.observe(document,{subtree:!0,childList:!0,attributes:!0}),setTimeout(()=>{n.disconnect(),!e&&t==location.href&&L(`$dead_click`,{el:a})},1e3)}},!0),o.forms&&addEventListener(`submit`,e=>{let t=e.target;t instanceof HTMLFormElement&&!u(t,s)&&L(`$submit`,{el:{selector:c(t),role:`form`,text:h(l(t)||t.getAttribute(`name`))}})},!0)}function U(e,c){if(!(r||!e||c.preview)){if(!crypto.subtle)return void(C||(C=(console.warn(`PlayStep Insights needs a secure context`),!0)));r=e,i=c.appKey,a={name:c.sdkName||`web`,version:`0.11.3`},o={...e.autocapture,...n.autocapture},s=[...e.mask,...n.mask??[]],p=t.get(`a:c`)??e.consent,V(),H()}}return{plugin:(...e)=>{e[0]==`config`?U(e[1].analytics,e[2]):r&&(e[0]==`identify`?R(e[1],e[2]):e[0]==`track`?L(e[1],{props:v(e[2])}):e[0]==`page`?z(e[1]):B())},setConsent:e=>{p=e?`granted`:`denied`,t.set(`a:c`,p),e?(S?.send==_&&V(),I()):(j=[],g(`a:d`),S&&(S.send=_,S.envelope=()=>null,x.flush()))},register:e=>void(A={...A,...v(e)}),unregister:e=>void delete A[e],group:(e,t,n)=>{(e in k||m(k)<5)&&(k[e]=t),L(`$group`,{props:{...v(n),group_type:e,group_id:t}})},flush:()=>x?.flush()}}const S=new WeakMap;function C(e,t){let n=S.get(e);return n||(S.set(e,n=x(t)),e.use(n.plugin)),n}export{C as t};
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@axceera/playstep-analytics",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.11.3",
4
+ "description": "PlayStep Insights: product analytics for your web app (pages, clicks, forms, your own events) as an add-on to @axceera/playstep. Loaded only when you add it; signed-in users only unless you allow anonymous tracking.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "homepage": "https://playstep.app/docs/sdk/web",
8
+ "keywords": [
9
+ "product analytics",
10
+ "analytics",
11
+ "onboarding",
12
+ "playstep"
13
+ ],
14
+ "sideEffects": false,
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./react": {
23
+ "types": "./dist/react.d.ts",
24
+ "import": "./dist/react.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist/*.js",
30
+ "dist/*.d.ts"
31
+ ],
32
+ "peerDependencies": {
33
+ "@axceera/playstep": ">=0.10.0"
34
+ },
35
+ "devDependencies": {
36
+ "@size-limit/file": "^14.2.0",
37
+ "happy-dom": "^20.14.6",
38
+ "size-limit": "^14.2.0",
39
+ "tsdown": "^0.23.0",
40
+ "typescript": "~6.0.3",
41
+ "vitest": "^4.1.11",
42
+ "@playstep/spec": "0.1.0",
43
+ "@axceera/playstep": "0.11.3"
44
+ },
45
+ "scripts": {
46
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsdown",
47
+ "test": "vitest run",
48
+ "size": "size-limit",
49
+ "typecheck": "tsc --noEmit",
50
+ "lint": "eslint ."
51
+ }
6
52
  }