@sortment/web-sdk 0.0.0-stage → 0.1.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/LICENSE +25 -0
- package/README.md +154 -2
- package/dist/sortment.cjs +1066 -0
- package/dist/sortment.cjs.map +1 -0
- package/dist/sortment.mjs +1066 -0
- package/dist/sortment.mjs.map +1 -0
- package/dist/sortment.umd.js +2 -0
- package/dist/sortment.umd.js.map +1 -0
- package/dist/types/autocapture.d.ts +17 -0
- package/dist/types/browser.d.ts +8 -0
- package/dist/types/client.d.ts +61 -0
- package/dist/types/context.d.ts +15 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/logger.d.ts +7 -0
- package/dist/types/mask.d.ts +8 -0
- package/dist/types/queue.d.ts +70 -0
- package/dist/types/session.d.ts +16 -0
- package/dist/types/storage.d.ts +30 -0
- package/dist/types/types.d.ts +107 -0
- package/dist/types/version.d.ts +2 -0
- package/package.json +43 -3
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,155 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sortment/web-sdk
|
|
2
2
|
|
|
3
|
-
|
|
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_…`) from Sortment → Settings → Event
|
|
10
|
+
sources. Publishable keys are safe to ship in a web page; you can pin them to
|
|
11
|
+
your site's domains. Never put a secret key (`srt_…`) in a browser: the SDK
|
|
12
|
+
refuses it.
|
|
13
|
+
|
|
14
|
+
### Script tag
|
|
15
|
+
|
|
16
|
+
Paste this before `</head>` on every page:
|
|
17
|
+
|
|
18
|
+
```html
|
|
19
|
+
<script>
|
|
20
|
+
(function (w, d, key, host) {
|
|
21
|
+
var s = (w.sortment = w.sortment || []);
|
|
22
|
+
if (!Array.isArray(s)) return;
|
|
23
|
+
[
|
|
24
|
+
'init',
|
|
25
|
+
'identify',
|
|
26
|
+
'track',
|
|
27
|
+
'page',
|
|
28
|
+
'alias',
|
|
29
|
+
'reset',
|
|
30
|
+
'setConsent',
|
|
31
|
+
'flush',
|
|
32
|
+
].forEach(function (m) {
|
|
33
|
+
s[m] = function () {
|
|
34
|
+
s.push([m].concat([].slice.call(arguments)));
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
var t = d.createElement('script');
|
|
38
|
+
t.async = true;
|
|
39
|
+
t.src =
|
|
40
|
+
'https://cdn.jsdelivr.net/npm/@sortment/web-sdk@0/dist/sortment.umd.js';
|
|
41
|
+
d.head.appendChild(t);
|
|
42
|
+
s.init(key, { apiHost: host });
|
|
43
|
+
})(window, document, 'srt_pk_YOUR_KEY', 'https://app.sortment.com');
|
|
44
|
+
</script>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Calls made before the file loads are recorded and replayed, so
|
|
48
|
+
`window.sortment.track(...)` works straight away.
|
|
49
|
+
|
|
50
|
+
### npm
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @sortment/web-sdk
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { sortment } from '@sortment/web-sdk';
|
|
58
|
+
|
|
59
|
+
sortment.init('srt_pk_YOUR_KEY', { apiHost: 'https://app.sortment.com' });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`init` is a no-op during server-side rendering, so it is safe to call from
|
|
63
|
+
shared code.
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
sortment.track('Added to cart', { sku: 'A1', price: 600 });
|
|
69
|
+
sortment.identify('user-42', { email: 'asha@example.com', plan: 'pro' });
|
|
70
|
+
sortment.page('Pricing'); // page views are captured for you; use this for custom names
|
|
71
|
+
sortment.alias('user-42'); // links the current anonymous id to user-42
|
|
72
|
+
sortment.reset(); // on logout: forget the user, new anonymous id
|
|
73
|
+
sortment.setConsent(true); // or false; remembered across visits
|
|
74
|
+
await sortment.flush(); // send now
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Every event carries an anonymous id created on the first visit, and the user
|
|
78
|
+
id once `identify` is called, so a visitor's events before and after login
|
|
79
|
+
belong to the same person.
|
|
80
|
+
|
|
81
|
+
## Auto-capture
|
|
82
|
+
|
|
83
|
+
All on by default. Turn pieces off with `autoCapture`, or pass `false`:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
sortment.init(key, {
|
|
87
|
+
autoCapture: { clicks: false }, // pageViews, clicks, formSubmits, sessions, attribution
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
| Event | When |
|
|
92
|
+
| ----------------- | --------------------------------------------------------------------- |
|
|
93
|
+
| `page_viewed` | On load and on single-page-app navigation (not hash changes) |
|
|
94
|
+
| `element_clicked` | Clicks on links, buttons and elements with `data-sortment-event` |
|
|
95
|
+
| `form_submitted` | Form submits: the form's id, name, action and field count. No values. |
|
|
96
|
+
| `session_started` | First event after 30 minutes without activity |
|
|
97
|
+
|
|
98
|
+
UTM parameters and ad click ids (`gclid`, `fbclid`, `msclkid`) are added to
|
|
99
|
+
every event of the visit as `context.campaign`.
|
|
100
|
+
|
|
101
|
+
Name the events marketers care about in your HTML, no code needed:
|
|
102
|
+
|
|
103
|
+
```html
|
|
104
|
+
<button data-sortment-event="Started trial" data-sortment-prop-plan="pro">
|
|
105
|
+
Try Pro
|
|
106
|
+
</button>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This sends `Started trial` with `{ plan: 'pro' }` instead of `element_clicked`.
|
|
110
|
+
|
|
111
|
+
## Privacy
|
|
112
|
+
|
|
113
|
+
- Input values are never read. Only the labels of submit buttons are.
|
|
114
|
+
- Keys that look sensitive (password, token, card number, CVV, PIN, OTP,
|
|
115
|
+
SSN, IBAN…) are replaced with `[redacted]` in properties, traits (also
|
|
116
|
+
where they are stored on the device) and URL query and fragment parameters. Add your own with `sensitiveFields: ['dateOfBirth']`.
|
|
117
|
+
- `data-sortment-mask` on an element keeps its clicks but drops its text;
|
|
118
|
+
`data-sortment-ignore` stops capturing it and everything inside.
|
|
119
|
+
- With `defaultConsent: 'pending'`, nothing is stored on the device or sent
|
|
120
|
+
until `setConsent(true)`. Events from before that are held in memory and
|
|
121
|
+
sent on consent. `setConsent(false)` deletes queued events and stored ids,
|
|
122
|
+
and applies to other open tabs of the site too.
|
|
123
|
+
|
|
124
|
+
## Delivery
|
|
125
|
+
|
|
126
|
+
Events are queued in `localStorage` and sent in batches every 5 seconds, or
|
|
127
|
+
as soon as 20 are waiting. Failed sends are retried with exponential backoff
|
|
128
|
+
(up to 10 tries), and events survive reloads and going offline. When the page
|
|
129
|
+
is hidden or closed the queue is sent with `navigator.sendBeacon`; the events
|
|
130
|
+
stay queued and are sent again on the next page load, in case the beacon never
|
|
131
|
+
arrives. Sortment drops events it has already received, so retries never
|
|
132
|
+
double-count. Each project (write key) keeps its own queue and ids, so several
|
|
133
|
+
projects on one site never mix.
|
|
134
|
+
|
|
135
|
+
## Options
|
|
136
|
+
|
|
137
|
+
| Option | Default | |
|
|
138
|
+
| ----------------------- | -------------------------- | ---------------------------------------- |
|
|
139
|
+
| `apiHost` | `https://app.sortment.com` | Your Sortment URL |
|
|
140
|
+
| `flushInterval` | `5000` | Milliseconds between sends |
|
|
141
|
+
| `flushAt` | `20` | Send when this many are queued (max 100) |
|
|
142
|
+
| `maxQueueSize` | `1000` | Oldest events are dropped beyond this |
|
|
143
|
+
| `maxRetries` | `10` | Failed sends before an event is dropped |
|
|
144
|
+
| `autoCapture` | all on | See above |
|
|
145
|
+
| `sessionTimeoutMinutes` | `30` | |
|
|
146
|
+
| `defaultConsent` | `'granted'` | `'pending'` to wait for `setConsent` |
|
|
147
|
+
| `maskSensitiveFields` | `true` | |
|
|
148
|
+
| `sensitiveFields` | `[]` | Extra keys to redact |
|
|
149
|
+
| `debug` | `false` | Log every event to the console |
|
|
150
|
+
|
|
151
|
+
## Releasing
|
|
152
|
+
|
|
153
|
+
Bump `version` in `package.json` and `src/version.ts`, merge, then push a tag
|
|
154
|
+
`web-sdk-v<version>`. The `publish-web-sdk` workflow tests, builds and
|
|
155
|
+
publishes to npm; jsDelivr serves the script-tag file from there.
|