@sortment/web-sdk 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2021-present Telescope Technology Limited (trading as “Lightdash”)
2
+
3
+ Portions of this software are licensed as follows:
4
+
5
+ * All content that resides under the "packages/backend/src/ee" directory of this repository, if that directory exists, is licensed under the license defined in "packages/backend/src/ee/LICENSE".
6
+ * All third party components incorporated into the Lightdash Software are licensed under the original license provided by the owner of the applicable component.
7
+ * Content outside of the above mentioned directories or restrictions above is available under the "MIT" license as defined below.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,174 @@
1
- # Temporary Holding Version
1
+ # @sortment/web-sdk
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
+ Send events from your website to Sortment: custom events, user identity, and
4
+ page views, clicks, form submits and sessions captured for you. About 6 KB
5
+ gzipped.
6
+
7
+ ## Install
8
+
9
+ You need a **publishable key** (`srt_pk_…`) for your Sortment project; your
10
+ Sortment admin creates it. Publishable keys are safe to ship in a web page;
11
+ you can pin them to your site's domains. Never put a secret key (`srt_…`) in
12
+ a browser: the SDK refuses it.
13
+
14
+ ### With an AI coding agent
15
+
16
+ Paste this into Claude Code, Cursor, Copilot or any coding agent working in
17
+ your site's code:
18
+
19
+ ```text
20
+ Install the Sortment web SDK in this project. Follow the guide at
21
+ https://cdn.jsdelivr.net/npm/@sortment/web-sdk@0/llms-full.txt step by step.
22
+ Publishable key: srt_pk_YOUR_KEY
23
+ Sortment host: https://app.sortment.com
24
+ Then identify signed-in users and track our key business events, and tell me
25
+ which events you added.
26
+ ```
27
+
28
+ The guide tells the agent where to install for your framework, how to handle
29
+ consent, how to name events, and how to check that events arrive. An index
30
+ for agents is at
31
+ [`llms.txt`](https://cdn.jsdelivr.net/npm/@sortment/web-sdk@0/llms.txt).
32
+
33
+ ### Script tag
34
+
35
+ Paste this before `</head>` on every page:
36
+
37
+ ```html
38
+ <script>
39
+ (function (w, d, key, host) {
40
+ var s = (w.sortment = w.sortment || []);
41
+ if (!Array.isArray(s)) return;
42
+ [
43
+ 'init',
44
+ 'identify',
45
+ 'track',
46
+ 'page',
47
+ 'alias',
48
+ 'reset',
49
+ 'setConsent',
50
+ 'flush',
51
+ ].forEach(function (m) {
52
+ s[m] = function () {
53
+ s.push([m].concat([].slice.call(arguments)));
54
+ };
55
+ });
56
+ var t = d.createElement('script');
57
+ t.async = true;
58
+ t.src =
59
+ 'https://cdn.jsdelivr.net/npm/@sortment/web-sdk@0/dist/sortment.umd.js';
60
+ d.head.appendChild(t);
61
+ s.init(key, { apiHost: host });
62
+ })(window, document, 'srt_pk_YOUR_KEY', 'https://app.sortment.com');
63
+ </script>
64
+ ```
65
+
66
+ Calls made before the file loads are recorded and replayed, so
67
+ `window.sortment.track(...)` works straight away.
68
+
69
+ ### npm
70
+
71
+ ```bash
72
+ npm install @sortment/web-sdk
73
+ ```
74
+
75
+ ```ts
76
+ import { sortment } from '@sortment/web-sdk';
77
+
78
+ sortment.init('srt_pk_YOUR_KEY', { apiHost: 'https://app.sortment.com' });
79
+ ```
80
+
81
+ `init` is a no-op during server-side rendering, so it is safe to call from
82
+ shared code.
83
+
84
+ ## API
85
+
86
+ ```ts
87
+ sortment.track('Added to cart', { sku: 'A1', price: 600 });
88
+ sortment.identify('user-42', { email: 'asha@example.com', plan: 'pro' });
89
+ sortment.page('Pricing'); // page views are captured for you; use this for custom names
90
+ sortment.alias('user-42'); // links the current anonymous id to user-42
91
+ sortment.reset(); // on logout: forget the user, new anonymous id
92
+ sortment.setConsent(true); // or false; remembered across visits
93
+ await sortment.flush(); // send now
94
+ ```
95
+
96
+ Every event carries an anonymous id created on the first visit, and the user
97
+ id once `identify` is called, so a visitor's events before and after login
98
+ belong to the same person.
99
+
100
+ ## Auto-capture
101
+
102
+ All on by default. Turn pieces off with `autoCapture`, or pass `false`:
103
+
104
+ ```ts
105
+ sortment.init(key, {
106
+ autoCapture: { clicks: false }, // pageViews, clicks, formSubmits, sessions, attribution
107
+ });
108
+ ```
109
+
110
+ | Event | When |
111
+ | ----------------- | --------------------------------------------------------------------- |
112
+ | `page_viewed` | On load and on single-page-app navigation (not hash changes) |
113
+ | `element_clicked` | Clicks on links, buttons and elements with `data-sortment-event` |
114
+ | `form_submitted` | Form submits: the form's id, name, action and field count. No values. |
115
+ | `session_started` | First event after 30 minutes without activity |
116
+
117
+ UTM parameters and ad click ids (`gclid`, `fbclid`, `msclkid`) are added to
118
+ every event of the visit as `context.campaign`.
119
+
120
+ Name the events marketers care about in your HTML, no code needed:
121
+
122
+ ```html
123
+ <button data-sortment-event="Started trial" data-sortment-prop-plan="pro">
124
+ Try Pro
125
+ </button>
126
+ ```
127
+
128
+ This sends `Started trial` with `{ plan: 'pro' }` instead of `element_clicked`.
129
+
130
+ ## Privacy
131
+
132
+ - Input values are never read. Only the labels of submit buttons are.
133
+ - Keys that look sensitive (password, token, card number, CVV, PIN, OTP,
134
+ SSN, IBAN…) are replaced with `[redacted]` in properties, traits (also
135
+ where they are stored on the device) and URL query and fragment parameters. Add your own with `sensitiveFields: ['dateOfBirth']`.
136
+ - `data-sortment-mask` on an element keeps its clicks but drops its text;
137
+ `data-sortment-ignore` stops capturing it and everything inside.
138
+ - With `defaultConsent: 'pending'`, nothing is stored on the device or sent
139
+ until `setConsent(true)`. Events from before that are held in memory and
140
+ sent on consent. `setConsent(false)` deletes queued events and stored ids,
141
+ and applies to other open tabs of the site too.
142
+
143
+ ## Delivery
144
+
145
+ Events are queued in `localStorage` and sent in batches every 5 seconds, or
146
+ as soon as 20 are waiting. Failed sends are retried with exponential backoff
147
+ (up to 10 tries), and events survive reloads and going offline. When the page
148
+ is hidden or closed the queue is sent with `navigator.sendBeacon`; the events
149
+ stay queued and are sent again on the next page load, in case the beacon never
150
+ arrives. Sortment drops events it has already received, so retries never
151
+ double-count. Each project (write key) keeps its own queue and ids, so several
152
+ projects on one site never mix.
153
+
154
+ ## Options
155
+
156
+ | Option | Default | |
157
+ | ----------------------- | -------------------------- | ---------------------------------------- |
158
+ | `apiHost` | `https://app.sortment.com` | Your Sortment URL |
159
+ | `flushInterval` | `5000` | Milliseconds between sends |
160
+ | `flushAt` | `20` | Send when this many are queued (max 100) |
161
+ | `maxQueueSize` | `1000` | Oldest events are dropped beyond this |
162
+ | `maxRetries` | `10` | Failed sends before an event is dropped |
163
+ | `autoCapture` | all on | See above |
164
+ | `sessionTimeoutMinutes` | `30` | |
165
+ | `defaultConsent` | `'granted'` | `'pending'` to wait for `setConsent` |
166
+ | `maskSensitiveFields` | `true` | |
167
+ | `sensitiveFields` | `[]` | Extra keys to redact |
168
+ | `debug` | `false` | Log every event to the console |
169
+
170
+ ## Releasing
171
+
172
+ Bump `version` in `package.json` and `src/version.ts`, merge, then push a tag
173
+ `web-sdk-v<version>`. The `publish-web-sdk` workflow tests, builds and
174
+ publishes to npm; jsDelivr serves the script-tag file from there.