@ebitex/visitors-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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,53 @@
1
- # Temporary Holding Version
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.
1
+ # @ebitex/visitors-sdk
2
+
3
+ Recognise a returning visitor **with their consent**, honour Global Privacy Control and your own consent
4
+ tool, and let them forget their device, from any site. You do not need forms, or any other ebitex product
5
+ beyond Audience.
6
+
7
+ - Zero runtime dependencies. Works with a plain `<script>` tag, with a bundler, and with React.
8
+ - The visitor's token is kept in a first-party cookie **only while ebitex says that device may be
9
+ recognised**, and removed when recognition is denied, withdrawn, or the device is forgotten.
10
+ - "Forget this device" is one operation with several doors: a footer link, a function, a React button, or a
11
+ plain request from your own server.
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ npm install @ebitex/visitors-sdk
17
+ ```
18
+
19
+ Or, on a page with no build step:
20
+
21
+ ```html
22
+ <script type="module"
23
+ src="https://cdn.jsdelivr.net/npm/@ebitex/visitors-sdk@0/dist/script.js"
24
+ data-ebitex-key="ebx_your_browser_key"></script>
25
+ <footer><a href="#" data-ebitex-forget>Forget this device</a></footer>
26
+ ```
27
+
28
+ ## Quick start
29
+
30
+ ```ts
31
+ import { createVisitors } from '@ebitex/visitors-sdk'
32
+
33
+ const visitors = createVisitors({ key: 'ebx_your_browser_key' })
34
+ const { consent } = await visitors.refresh()
35
+ ```
36
+
37
+ You need an Audience **delivery key** that is restricted to your site's origins (Audience, Settings, Delivery
38
+ keys). The key is visible to anyone who views your page, which is safe only because of that restriction.
39
+ Never put an unrestricted key in a page.
40
+
41
+ Full guide: <https://developers.ebitex.io/getting-started/visitors/>
42
+
43
+ ## Entry points
44
+
45
+ | Import | For |
46
+ |---|---|
47
+ | `@ebitex/visitors-sdk` | `createVisitors`, `cookieStorage`, the types |
48
+ | `@ebitex/visitors-sdk/react` | `VisitorsProvider`, `useVisitors`, `ForgetDeviceButton` (React is an optional peer) |
49
+ | `@ebitex/visitors-sdk/script` | The drop-in script for pages with no build step |
50
+
51
+ ## Licence
52
+
53
+ Apache-2.0
@@ -0,0 +1,25 @@
1
+ import type { VisitorSession } from './types.js';
2
+ /** The default API address. */
3
+ export declare const DEFAULT_ENDPOINT = "https://api.ebitex.io";
4
+ /** An error answer from ebitex, or an answer the SDK could not read. */
5
+ export declare class VisitorsApiError extends Error {
6
+ /** The HTTP status, or `0` when the request never got an answer (a network failure). */
7
+ readonly status: number;
8
+ /** The `error` member of the answer, when there was one: for example `invalid_api_key`, `origin_denied` or `rate_limited`. */
9
+ readonly code: string | undefined;
10
+ /** The `X-Request-Id` of the answer. Quote it when you ask for help. */
11
+ readonly requestId: string | undefined;
12
+ constructor(message: string, status: number, code?: string, requestId?: string);
13
+ }
14
+ export interface RawConsent {
15
+ cmp: 'granted' | 'denied' | null;
16
+ gpc: boolean;
17
+ dnt: boolean;
18
+ cmpString: string | null;
19
+ }
20
+ export interface VisitorsClient {
21
+ session(visitor: string | null, consent: RawConsent): Promise<VisitorSession>;
22
+ forget(visitor: string): Promise<string>;
23
+ }
24
+ export declare function createClient(key: string, endpoint: string, doFetch: typeof globalThis.fetch): VisitorsClient;
25
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,YAAY,CAAA;AAE9D,+BAA+B;AAC/B,eAAO,MAAM,gBAAgB,0BAA0B,CAAA;AAEvD,wEAAwE;AACxE,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,wFAAwF;IACxF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IAEvB,8HAA8H;IAC9H,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;IAEjC,wEAAwE;IACxE,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAA;gBAE1B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAO/E;AAED,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,SAAS,GAAG,QAAQ,GAAG,IAAI,CAAA;IAChC,GAAG,EAAE,OAAO,CAAA;IACZ,GAAG,EAAE,OAAO,CAAA;IACZ,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IAC7E,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CACzC;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,UAAU,CAAC,KAAK,GAAG,cAAc,CAsC5G"}
package/dist/client.js ADDED
@@ -0,0 +1,72 @@
1
+ /** The default API address. */
2
+ export const DEFAULT_ENDPOINT = 'https://api.ebitex.io';
3
+ /** An error answer from ebitex, or an answer the SDK could not read. */
4
+ export class VisitorsApiError extends Error {
5
+ /** The HTTP status, or `0` when the request never got an answer (a network failure). */
6
+ status;
7
+ /** The `error` member of the answer, when there was one: for example `invalid_api_key`, `origin_denied` or `rate_limited`. */
8
+ code;
9
+ /** The `X-Request-Id` of the answer. Quote it when you ask for help. */
10
+ requestId;
11
+ constructor(message, status, code, requestId) {
12
+ super(message);
13
+ this.name = 'VisitorsApiError';
14
+ this.status = status;
15
+ this.code = code;
16
+ this.requestId = requestId;
17
+ }
18
+ }
19
+ export function createClient(key, endpoint, doFetch) {
20
+ const base = endpoint.replace(/\/+$/, '');
21
+ async function post(path, body) {
22
+ let response;
23
+ try {
24
+ response = await doFetch(`${base}/audience/delivery/v1/visitors/${path}`, {
25
+ method: 'POST',
26
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
27
+ body: JSON.stringify(body),
28
+ });
29
+ }
30
+ catch (cause) {
31
+ throw new VisitorsApiError(cause instanceof Error ? cause.message : 'The request could not be sent.', 0);
32
+ }
33
+ const requestId = response.headers.get('X-Request-Id') ?? undefined;
34
+ const json = await response.json().catch(() => null);
35
+ if (!response.ok) {
36
+ const code = typeof json?.error === 'string' ? json.error : undefined;
37
+ throw new VisitorsApiError(code ?? `The request failed with status ${response.status}.`, response.status, code, requestId);
38
+ }
39
+ return json;
40
+ }
41
+ return {
42
+ async session(visitor, consent) {
43
+ const json = await post('session', { visitor, consent });
44
+ if (!isSession(json))
45
+ throw new VisitorsApiError('The answer was not a visitor session.', 200, 'invalid_response');
46
+ return json;
47
+ },
48
+ async forget(visitor) {
49
+ const json = await post('forget', { visitor });
50
+ const message = json?.message;
51
+ if (typeof message !== 'string')
52
+ throw new VisitorsApiError('The answer was not a forget confirmation.', 200, 'invalid_response');
53
+ return message;
54
+ },
55
+ };
56
+ }
57
+ function isSession(value) {
58
+ if (typeof value !== 'object' || value === null)
59
+ return false;
60
+ const { visitor, consent } = value;
61
+ if (typeof visitor !== 'string' || visitor === '')
62
+ return false;
63
+ if (typeof consent !== 'object' || consent === null)
64
+ return false;
65
+ const state = consent;
66
+ return ((state.recognition === 'granted' || state.recognition === 'denied') &&
67
+ (state.tokenKind === 'recognition' || state.tokenKind === 'session') &&
68
+ typeof state.tokenLifetimeSeconds === 'number' &&
69
+ typeof state.messages === 'object' &&
70
+ state.messages !== null);
71
+ }
72
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mapp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;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IAC7D,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,KAAiD,CAAA;IAC9E,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,EAAE;QAAE,OAAO,KAAK,CAAA;IAC/D,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IACjE,MAAM,KAAK,GAAG,OAAgC,CAAA;IAC9C,OAAO,CACL,CAAC,KAAK,CAAC,WAAW,KAAK,SAAS,IAAI,KAAK,CAAC,WAAW,KAAK,QAAQ,CAAC;QACnE,CAAC,KAAK,CAAC,SAAS,KAAK,aAAa,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,CAAC;QACpE,OAAO,KAAK,CAAC,oBAAoB,KAAK,QAAQ;QAC9C,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ;QAClC,KAAK,CAAC,QAAQ,KAAK,IAAI,CACxB,CAAA;AACH,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { Visitors } from './types.js';
2
+ /**
3
+ * Wires a page that has no build step, from the `<script>` tag that loaded it:
4
+ *
5
+ * ```html
6
+ * <script type="module" src=".../script.js" data-ebitex-key="ebx_..."></script>
7
+ * ```
8
+ *
9
+ * It creates one {@link Visitors}, exposes it as `window.ebitexVisitors` (so your consent tool's callback can call
10
+ * `setConsent`), asks for the visitor's token and consent state, and makes every element marked `data-ebitex-forget` a
11
+ * "forget this device" control, including ones added to the page later.
12
+ *
13
+ * Attributes on the script tag: `data-ebitex-key` (required), `data-ebitex-endpoint`, and `data-ebitex-start="manual"`
14
+ * to wait for your own call to `refresh` or `setConsent` instead of asking at once, which is what you want when your
15
+ * consent tool answers before the visitor is first recognised.
16
+ *
17
+ * Attributes on a control: `data-ebitex-forget-confirm="off"` to skip the confirmation question, `data-ebitex-done-text`
18
+ * and `data-ebitex-error-text` for your own wording, and `data-ebitex-forget-status="#id"` to say where the result goes
19
+ * (by default a status line is added after the control). After a successful forget a `ebitex:visitor-forgotten` event
20
+ * is dispatched on the document with the message in `detail`.
21
+ *
22
+ * Pass an `AbortSignal` to stop listening for clicks, for example when a single-page app tears the page down. Returns
23
+ * `null` when the page has no matching script tag.
24
+ */
25
+ export declare function startDropIn(doc?: Document, signal?: AbortSignal): Visitors | null;
26
+ //# sourceMappingURL=dropIn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dropIn.d.ts","sourceRoot":"","sources":["../src/dropIn.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAK1C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,QAAmB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,QAAQ,GAAG,IAAI,CAiB3F"}
package/dist/dropIn.js ADDED
@@ -0,0 +1,82 @@
1
+ import { createVisitors } from './visitors.js';
2
+ /** What the visitor sees if forgetting fails and the page gave no wording of its own. */
3
+ const DEFAULT_ERROR_TEXT = 'Something went wrong. Please try again.';
4
+ /**
5
+ * Wires a page that has no build step, from the `<script>` tag that loaded it:
6
+ *
7
+ * ```html
8
+ * <script type="module" src=".../script.js" data-ebitex-key="ebx_..."></script>
9
+ * ```
10
+ *
11
+ * It creates one {@link Visitors}, exposes it as `window.ebitexVisitors` (so your consent tool's callback can call
12
+ * `setConsent`), asks for the visitor's token and consent state, and makes every element marked `data-ebitex-forget` a
13
+ * "forget this device" control, including ones added to the page later.
14
+ *
15
+ * Attributes on the script tag: `data-ebitex-key` (required), `data-ebitex-endpoint`, and `data-ebitex-start="manual"`
16
+ * to wait for your own call to `refresh` or `setConsent` instead of asking at once, which is what you want when your
17
+ * consent tool answers before the visitor is first recognised.
18
+ *
19
+ * Attributes on a control: `data-ebitex-forget-confirm="off"` to skip the confirmation question, `data-ebitex-done-text`
20
+ * and `data-ebitex-error-text` for your own wording, and `data-ebitex-forget-status="#id"` to say where the result goes
21
+ * (by default a status line is added after the control). After a successful forget a `ebitex:visitor-forgotten` event
22
+ * is dispatched on the document with the message in `detail`.
23
+ *
24
+ * Pass an `AbortSignal` to stop listening for clicks, for example when a single-page app tears the page down. Returns
25
+ * `null` when the page has no matching script tag.
26
+ */
27
+ export function startDropIn(doc = document, signal) {
28
+ const script = doc.querySelector('script[data-ebitex-key]');
29
+ const key = script?.dataset.ebitexKey;
30
+ if (!script || !key)
31
+ return null;
32
+ const visitors = createVisitors({ key, endpoint: script.dataset.ebitexEndpoint || undefined });
33
+ doc.defaultView.ebitexVisitors = visitors;
34
+ if (script.dataset.ebitexStart !== 'manual')
35
+ void visitors.refresh().catch(() => undefined);
36
+ doc.addEventListener('click', (event) => {
37
+ const control = event.target?.closest('[data-ebitex-forget]');
38
+ if (!control)
39
+ return;
40
+ event.preventDefault();
41
+ void forgetFrom(control, visitors, doc);
42
+ }, { signal });
43
+ return visitors;
44
+ }
45
+ async function forgetFrom(control, visitors, doc) {
46
+ if (control.dataset.ebitexForgetBusy === 'true')
47
+ return;
48
+ const question = visitors.consent?.messages.forgetConfirm;
49
+ if (question && control.dataset.ebitexForgetConfirm !== 'off' && !doc.defaultView.confirm(question))
50
+ return;
51
+ control.dataset.ebitexForgetBusy = 'true';
52
+ try {
53
+ const message = (await visitors.forget()) ?? control.dataset.ebitexDoneText ?? '';
54
+ if (message)
55
+ report(control, message, false, doc);
56
+ doc.dispatchEvent(new CustomEvent('ebitex:visitor-forgotten', { detail: { message } }));
57
+ }
58
+ catch {
59
+ report(control, control.dataset.ebitexErrorText ?? DEFAULT_ERROR_TEXT, true, doc);
60
+ }
61
+ finally {
62
+ delete control.dataset.ebitexForgetBusy;
63
+ }
64
+ }
65
+ /** Shows a result as text only: the wording is the organization's, and is never parsed as markup. */
66
+ function report(control, text, isError, doc) {
67
+ const selector = control.dataset.ebitexForgetStatus;
68
+ let region = selector ? doc.querySelector(selector) : null;
69
+ if (!region) {
70
+ const next = control.nextElementSibling;
71
+ region = next instanceof HTMLElement && next.dataset.ebitexForgetStatusLine === 'true' ? next : null;
72
+ }
73
+ if (!region) {
74
+ region = doc.createElement('span');
75
+ region.dataset.ebitexForgetStatusLine = 'true';
76
+ control.insertAdjacentElement('afterend', region);
77
+ }
78
+ region.setAttribute('role', isError ? 'alert' : 'status');
79
+ region.setAttribute('aria-live', isError ? 'assertive' : 'polite');
80
+ region.textContent = text;
81
+ }
82
+ //# sourceMappingURL=dropIn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dropIn.js","sourceRoot":"","sources":["../src/dropIn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAG9C,yFAAyF;AACzF,MAAM,kBAAkB,GAAG,yCAAyC,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,WAAW,CAAC,MAAgB,QAAQ,EAAE,MAAoB;IACxE,MAAM,MAAM,GAAG,GAAG,CAAC,aAAa,CAAc,yBAAyB,CAAC,CAAA;IACxE,MAAM,GAAG,GAAG,MAAM,EAAE,OAAO,CAAC,SAAS,CAAA;IACrC,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG;QAAE,OAAO,IAAI,CAAA;IAEhC,MAAM,QAAQ,GAAG,cAAc,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,cAAc,IAAI,SAAS,EAAE,CAAC,CAC7F;IAAC,GAAG,CAAC,WAAgE,CAAC,cAAc,GAAG,QAAQ,CAAA;IAChG,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,KAAK,QAAQ;QAAE,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IAE3F,GAAG,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,KAA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C;YAAS,CAAC;QACT,OAAO,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAA;IACzC,CAAC;AACH,CAAC;AAED,qGAAqG;AACrG,SAAS,MAAM,CAAC,OAAoB,EAAE,IAAY,EAAE,OAAgB,EAAE,GAAa;IACjF,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAA;IACnD,IAAI,MAAM,GAAuB,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAc,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAC3F,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,GAAG,OAAO,CAAC,kBAAkB,CAAA;QACvC,MAAM,GAAG,IAAI,YAAY,WAAW,IAAI,IAAI,CAAC,OAAO,CAAC,sBAAsB,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAA;IACtG,CAAC;IACD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,GAAG,GAAG,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAClC,MAAM,CAAC,OAAO,CAAC,sBAAsB,GAAG,MAAM,CAAA;QAC9C,OAAO,CAAC,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAA;IACnD,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;IACzD,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;IAClE,MAAM,CAAC,WAAW,GAAG,IAAI,CAAA;AAC3B,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { createVisitors } from './visitors.js';
2
+ export { cookieStorage } from './storage.js';
3
+ export type { CookieStorageOptions } from './storage.js';
4
+ export { VisitorsApiError, DEFAULT_ENDPOINT } from './client.js';
5
+ export type { ConsentMessages, ConsentOptions, ConsentState, Visitors, VisitorSession, VisitorsOptions, VisitorsSnapshot, VisitorStorage, } from './types.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,YAAY,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAChE,YAAY,EACV,eAAe,EACf,cAAc,EACd,YAAY,EACZ,QAAQ,EACR,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,cAAc,GACf,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createVisitors } from './visitors.js';
2
+ export { cookieStorage } from './storage.js';
3
+ export { VisitorsApiError, DEFAULT_ENDPOINT } from './client.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,26 @@
1
+ import { type ReactNode } from 'react';
2
+ import type { Visitors, VisitorsSnapshot } from '../types.js';
3
+ /** Makes a {@link Visitors} object available to {@link useVisitors} and {@link ForgetDeviceButton} below it. Create the object once, outside any component. */
4
+ export declare function VisitorsProvider({ visitors, children }: {
5
+ visitors: Visitors;
6
+ children: ReactNode;
7
+ }): import("react").JSX.Element;
8
+ /** The visitor's current token and consent state, which re-render the component when they change, and the calls that change them. */
9
+ export declare function useVisitors(): VisitorsSnapshot & Pick<Visitors, 'refresh' | 'setConsent' | 'forget' | 'ready'>;
10
+ export interface ForgetDeviceButtonProps {
11
+ /** The button's label. Defaults to "Forget this device"; pass your own for another language. */
12
+ children?: ReactNode;
13
+ /** Ask the organization's own confirmation question first. Defaults to `true`. */
14
+ confirm?: boolean;
15
+ /** Called with the organization's wording once the device has been forgotten. */
16
+ onForgotten?: (message: string | null) => void;
17
+ /** What to show if forgetting fails. Defaults to an English sentence; pass your own for another language. */
18
+ errorText?: string;
19
+ className?: string;
20
+ }
21
+ /**
22
+ * A button that forgets the visitor's device, asks the organization's own confirmation question first, and shows the
23
+ * organization's own wording afterwards in a status line next to it.
24
+ */
25
+ export declare function ForgetDeviceButton({ children, confirm, onForgotten, errorText, className }: ForgetDeviceButtonProps): import("react").JSX.Element;
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAkF,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AACtH,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAI7D,+JAA+J;AAC/J,wBAAgB,gBAAgB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,EAAE,QAAQ,CAAC;IAAC,QAAQ,EAAE,SAAS,CAAA;CAAE,+BAEnG;AAED,qIAAqI;AACrI,wBAAgB,WAAW,IAAI,gBAAgB,GAAG,IAAI,CAAC,QAAQ,EAAE,SAAS,GAAG,YAAY,GAAG,QAAQ,GAAG,OAAO,CAAC,CAe9G;AAED,MAAM,WAAW,uBAAuB;IACtC,gGAAgG;IAChG,QAAQ,CAAC,EAAE,SAAS,CAAA;IAEpB,kFAAkF;IAClF,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB,iFAAiF;IACjF,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAA;IAE9C,6GAA6G;IAC7G,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,QAAQ,EAAE,OAAc,EAAE,WAAW,EAAE,SAAqD,EAAE,SAAS,EAAE,EAAE,uBAAuB,+BA6BtK"}
@@ -0,0 +1,53 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { createContext, useCallback, useContext, useRef, useState, useSyncExternalStore } from 'react';
3
+ const VisitorsContext = createContext(null);
4
+ /** Makes a {@link Visitors} object available to {@link useVisitors} and {@link ForgetDeviceButton} below it. Create the object once, outside any component. */
5
+ export function VisitorsProvider({ visitors, children }) {
6
+ return _jsx(VisitorsContext.Provider, { value: visitors, children: children });
7
+ }
8
+ /** The visitor's current token and consent state, which re-render the component when they change, and the calls that change them. */
9
+ export function useVisitors() {
10
+ const visitors = useContext(VisitorsContext);
11
+ if (!visitors)
12
+ throw new Error('useVisitors must be used inside a <VisitorsProvider>.');
13
+ // useSyncExternalStore needs the same object back until something changes, so keep the last one.
14
+ const last = useRef({ token: visitors.token, consent: visitors.consent });
15
+ const read = useCallback(() => {
16
+ if (last.current.token !== visitors.token || last.current.consent !== visitors.consent) {
17
+ last.current = { token: visitors.token, consent: visitors.consent };
18
+ }
19
+ return last.current;
20
+ }, [visitors]);
21
+ const snapshot = useSyncExternalStore(visitors.subscribe, read, read);
22
+ return { ...snapshot, refresh: visitors.refresh, setConsent: visitors.setConsent, forget: visitors.forget, ready: visitors.ready };
23
+ }
24
+ /**
25
+ * A button that forgets the visitor's device, asks the organization's own confirmation question first, and shows the
26
+ * organization's own wording afterwards in a status line next to it.
27
+ */
28
+ export function ForgetDeviceButton({ children, confirm = true, onForgotten, errorText = 'Something went wrong. Please try again.', className }) {
29
+ const visitors = useContext(VisitorsContext);
30
+ if (!visitors)
31
+ throw new Error('ForgetDeviceButton must be used inside a <VisitorsProvider>.');
32
+ const [status, setStatus] = useState(null);
33
+ const [busy, setBusy] = useState(false);
34
+ async function onClick() {
35
+ const question = visitors.consent?.messages.forgetConfirm;
36
+ if (confirm && question && !window.confirm(question))
37
+ return;
38
+ setBusy(true);
39
+ try {
40
+ const message = await visitors.forget();
41
+ setStatus(message ? { text: message, isError: false } : null);
42
+ onForgotten?.(message);
43
+ }
44
+ catch {
45
+ setStatus({ text: errorText, isError: true });
46
+ }
47
+ finally {
48
+ setBusy(false);
49
+ }
50
+ }
51
+ return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: className, disabled: busy, onClick: () => void onClick(), children: children ?? 'Forget this device' }), status ? _jsx("span", { role: status.isError ? 'alert' : 'status', children: status.text }) : null] }));
52
+ }
53
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAkB,MAAM,OAAO,CAAA;AAGtH,MAAM,eAAe,GAAG,aAAa,CAAkB,IAAI,CAAC,CAAA;AAE5D,+JAA+J;AAC/J,MAAM,UAAU,gBAAgB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAA+C;IAClG,OAAO,KAAC,eAAe,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,YAAG,QAAQ,GAA4B,CAAA;AACzF,CAAC;AAED,qIAAqI;AACrI,MAAM,UAAU,WAAW;IACzB,MAAM,QAAQ,GAAG,UAAU,CAAC,eAAe,CAAC,CAAA;IAC5C,IAAI,CAAC,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;IAEvF,iGAAiG;IACjG,MAAM,IAAI,GAAG,MAAM,CAAmB,EAAE,KAA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@@
1
+ import { startDropIn } from './dropIn.js';
2
+ // The drop-in entry: load this file with `<script type="module" data-ebitex-key="...">` and it starts itself.
3
+ // See startDropIn for the attributes it reads. Importing the package's main entry never runs this.
4
+ if (typeof document !== 'undefined')
5
+ startDropIn();
6
+ //# sourceMappingURL=script.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script.js","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAEzC,8GAA8G;AAC9G,mGAAmG;AACnG,IAAI,OAAO,QAAQ,KAAK,WAAW;IAAE,WAAW,EAAE,CAAA"}
@@ -0,0 +1,21 @@
1
+ import type { VisitorStorage } from './types.js';
2
+ /** Options for {@link cookieStorage}. */
3
+ export interface CookieStorageOptions {
4
+ /** The cookie's name. Defaults to `ebitex_visitor`. */
5
+ name?: string;
6
+ /** Share the cookie with your other subdomains, for example `example.com`. By default it belongs to the current host only. */
7
+ domain?: string;
8
+ /** The cookie's path. Defaults to `/`. */
9
+ path?: string;
10
+ /** Defaults to `true` on an `https` page and `false` otherwise. */
11
+ secure?: boolean;
12
+ }
13
+ /**
14
+ * Keeps the token in a first-party cookie on your own site: `Path=/`, `SameSite=Lax`, `Secure` on `https`, and a
15
+ * lifetime no longer than the token's. It is readable by your own scripts (it has to be, so that the SDK can send it),
16
+ * which is why a token on its own unlocks nothing.
17
+ *
18
+ * Outside a browser (during server rendering, for example) it does nothing and reads nothing.
19
+ */
20
+ export declare function cookieStorage(options?: CookieStorageOptions): VisitorStorage;
21
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAKhD,yCAAyC;AACzC,MAAM,WAAW,oBAAoB;IACnC,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,8HAA8H;IAC9H,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf,0CAA0C;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,mEAAmE;IACnE,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,cAAc,CAoChF"}
@@ -0,0 +1,51 @@
1
+ /** The longest a browser keeps a cookie (400 days), which is also how long ebitex issues a recognition token for. */
2
+ const MAX_COOKIE_SECONDS = 34_560_000;
3
+ /**
4
+ * Keeps the token in a first-party cookie on your own site: `Path=/`, `SameSite=Lax`, `Secure` on `https`, and a
5
+ * lifetime no longer than the token's. It is readable by your own scripts (it has to be, so that the SDK can send it),
6
+ * which is why a token on its own unlocks nothing.
7
+ *
8
+ * Outside a browser (during server rendering, for example) it does nothing and reads nothing.
9
+ */
10
+ export function cookieStorage(options = {}) {
11
+ const name = options.name ?? 'ebitex_visitor';
12
+ const path = options.path ?? '/';
13
+ const attributes = (maxAgeSeconds) => {
14
+ const secure = options.secure ?? (typeof location !== 'undefined' && location.protocol === 'https:');
15
+ return [`Path=${path}`, `Max-Age=${maxAgeSeconds}`, 'SameSite=Lax', options.domain ? `Domain=${options.domain}` : '', secure ? 'Secure' : '']
16
+ .filter(Boolean)
17
+ .join('; ');
18
+ };
19
+ return {
20
+ get() {
21
+ if (typeof document === 'undefined')
22
+ return null;
23
+ for (const part of document.cookie.split(';')) {
24
+ const separator = part.indexOf('=');
25
+ if (separator < 0)
26
+ continue;
27
+ if (part.slice(0, separator).trim() !== name)
28
+ continue;
29
+ try {
30
+ return decodeURIComponent(part.slice(separator + 1).trim()) || null;
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
36
+ return null;
37
+ },
38
+ set(token, { maxAgeSeconds }) {
39
+ if (typeof document === 'undefined')
40
+ return;
41
+ const age = Math.min(Math.max(Math.floor(maxAgeSeconds), 1), MAX_COOKIE_SECONDS);
42
+ document.cookie = `${name}=${encodeURIComponent(token)}; ${attributes(age)}`;
43
+ },
44
+ remove() {
45
+ if (typeof document === 'undefined')
46
+ return;
47
+ document.cookie = `${name}=; ${attributes(0)}`;
48
+ },
49
+ };
50
+ }
51
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAEA,qHAAqH;AACrH,MAAM,kBAAkB,GAAG,UAAU,CAAA;AAiBrC;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,UAAgC,EAAE;IAC9D,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,gBAAgB,CAAA;IAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,GAAG,CAAA;IAEhC,MAAM,UAAU,GAAG,CAAC,aAAqB,EAAU,EAAE;QACnD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,QAAQ,KAAK,WAAW,IAAI,QAAQ,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAA;QACpG,OAAO,CAAC,QAAQ,IAAI,EAAE,EAAE,WAAW,aAAa,EAAE,EAAE,cAAc,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;aAC1I,MAAM,CAAC,OAAO,CAAC;aACf,IAAI,CAAC,IAAI,CAAC,CAAA;IACf,CAAC,CAAA;IAED,OAAO;QACL,GAAG;YACD,IAAI,OAAO,QAAQ,KAAK,WAAW;gBAAE,OAAO,IAAI,CAAA;YAChD,KAAK,MAAM,IAAI,IAAI,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;gBACnC,IAAI,SAAS,GAAG,CAAC;oBAAE,SAAQ;gBAC3B,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,KAAK,IAAI;oBAAE,SAAQ;gBACtD,IAAI,CAAC;oBACH,OAAO,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,IAAI,CAAA;gBACrE,CAAC;gBAAC,MAAM,CAAC;oBACP,OAAO,IAAI,CAAA;gBACb,CAAC;YACH,CAAC;YACD,OAAO,IAAI,CAAA;QACb,CAAC;QACD,GAAG,CAAC,KAAK,EAAE,EAAE,aAAa,EAAE;YAC1B,IAAI,OAAO,QAAQ,KAAK,WAAW;gBAAE,OAAM;YAC3C,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAA;YAChF,QAAQ,CAAC,MAAM,GAAG,GAAG,IAAI,IAAI,kBAAkB,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,GAAG,CAAC,EAAE,CAAA;QAC9E,CAAC;QACD,MAAM;YACJ,IAAI,OAAO,QAAQ,KAAK,WAAW;gBAAE,OAAM;YAC3C,QAAQ,CAAC,MAAM,GAAG,GAAG,IAAI,MAAM,UAAU,CAAC,CAAC,CAAC,EAAE,CAAA;QAChD,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,113 @@
1
+ /**
2
+ * What you know about the visitor's consent, taken from your own consent tool and from the browser. Send what you
3
+ * saw; ebitex decides what it means. A signal that outranks the organization's default is honoured in this order:
4
+ * your consent tool's answer, then Global Privacy Control, then Do Not Track, then the organization's default.
5
+ */
6
+ export interface ConsentOptions {
7
+ /** The answer from your own consent tool: `'granted'` or `'denied'`. Leave it out until the visitor has answered. */
8
+ cmp?: 'granted' | 'denied' | null;
9
+ /**
10
+ * The browser's Global Privacy Control setting. When you leave it out, the SDK reads `navigator.globalPrivacyControl`
11
+ * for you. Pass `false` to ignore the browser's setting (rarely right: it is the visitor's own preference).
12
+ */
13
+ gpc?: boolean;
14
+ /** The browser's Do Not Track setting. When you leave it out, the SDK reads `navigator.doNotTrack` for you. */
15
+ dnt?: boolean;
16
+ /** Your consent tool's own consent string, kept as evidence of what the visitor was shown. */
17
+ cmpString?: string | null;
18
+ }
19
+ /** The organization's own wording, in the visitor's language, for the things you may need to say. */
20
+ export interface ConsentMessages {
21
+ /** A notice for a visitor whose device is not being recognised, for example "we are not remembering you between visits". */
22
+ recognitionDeclined: string;
23
+ /** The question to ask before forgetting this device. */
24
+ forgetConfirm: string;
25
+ /** What to show once the device has been forgotten. */
26
+ forgetDone: string;
27
+ }
28
+ /** What ebitex decided about recognising this visitor, and why. */
29
+ export interface ConsentState {
30
+ /** `'granted'` when this device may be recognised on a later visit. Only then is the token kept. */
31
+ recognition: 'granted' | 'denied';
32
+ /** What decided it: your consent tool (`'cmp'`), the browser (`'globalPrivacyControl'`, `'doNotTrack'`) or the organization's default (`'organizationDefault'`). */
33
+ source: string;
34
+ /** The version of the organization's consent settings this answer was made under. */
35
+ policyVersion: number;
36
+ /** `'recognition'` tokens may outlive a visit. `'session'` tokens expire a few hours after they are issued, whatever the visitor does, and are never kept across visits. */
37
+ tokenKind: 'recognition' | 'session';
38
+ /** How long the token is good for, in seconds. The default cookie storage uses it as the cookie's lifetime. */
39
+ tokenLifetimeSeconds: number;
40
+ messages: ConsentMessages;
41
+ }
42
+ /** The token to use from now on and the consent state it was issued under. */
43
+ export interface VisitorSession {
44
+ visitor: string;
45
+ consent: ConsentState;
46
+ }
47
+ /**
48
+ * Where the token is kept between visits. The SDK calls `set` only while ebitex reports that this device may be
49
+ * recognised, and calls `remove` when recognition is denied or withdrawn, and when the device is forgotten. Writing an
50
+ * identifier to a visitor's device needs their consent first, which is why persistence goes through this adapter and is
51
+ * never done behind your back. Methods may be asynchronous.
52
+ */
53
+ export interface VisitorStorage {
54
+ get(): string | null | undefined | Promise<string | null | undefined>;
55
+ set(token: string, options: {
56
+ maxAgeSeconds: number;
57
+ }): void | Promise<void>;
58
+ remove(): void | Promise<void>;
59
+ }
60
+ /** Options for {@link createVisitors}. */
61
+ export interface VisitorsOptions {
62
+ /**
63
+ * An Audience delivery key, restricted to the origins of your site. The key is visible to anyone who views your
64
+ * page, which is safe only because it can be used from those origins alone. Never put an unrestricted key in a page.
65
+ */
66
+ key: string;
67
+ /** Where the ebitex API is. Defaults to `https://api.ebitex.io`. */
68
+ endpoint?: string;
69
+ /** What you know about the visitor's consent. Change it later with {@link Visitors.setConsent}. */
70
+ consent?: ConsentOptions;
71
+ /**
72
+ * Where to keep the token between visits. By default a first-party cookie called `ebitex_visitor` (see
73
+ * {@link cookieStorage}). Pass `false` to keep it in memory only, so every page load is a new visitor.
74
+ */
75
+ storage?: VisitorStorage | false;
76
+ /** The `fetch` to use. Defaults to the global one. */
77
+ fetch?: typeof globalThis.fetch;
78
+ }
79
+ /** What a {@link Visitors} object reports to a listener: the current token and consent state. */
80
+ export interface VisitorsSnapshot {
81
+ /** The current token, or `null` before the first answer and after the device has been forgotten. */
82
+ readonly token: string | null;
83
+ /** The consent state from the last answer, or `null` before one and after the device has been forgotten. */
84
+ readonly consent: ConsentState | null;
85
+ }
86
+ /** A visitor's identity on your site, under their consent. Create one with {@link createVisitors}. */
87
+ export interface Visitors extends VisitorsSnapshot {
88
+ /** Resolves once a token kept from an earlier visit has been read into memory. {@link refresh} waits for it, so you rarely need it. */
89
+ readonly ready: Promise<void>;
90
+ /**
91
+ * Asks ebitex for the token to use now and the consent state, sending the stored token if there is one. Call it
92
+ * when the page loads, and again whenever your consent tool's answer changes (or use {@link setConsent}). It stores
93
+ * nothing about the visitor; the token is kept on their device only while recognition is granted.
94
+ */
95
+ refresh(): Promise<VisitorSession>;
96
+ /**
97
+ * Replaces the consent signals, for your consent tool's callback, and asks again. A `cmp` of `'denied'` is a
98
+ * withdrawal: the token is dropped from memory and from storage at once, before the network call returns.
99
+ */
100
+ setConsent(options: ConsentOptions): Promise<VisitorSession>;
101
+ /**
102
+ * Forgets this device. The token stops working for good, what that device unlocked is ended (a link to a person, a
103
+ * connected contact, an emailed-link verification), and the token is removed from memory and storage. Resolves with
104
+ * the organization's own wording to show the visitor, or `null` if there was nothing to forget. Forgetting a device
105
+ * does not erase the person; that is a separate action by the organization.
106
+ *
107
+ * If the request fails the token is kept and the promise rejects, so the visitor can try again.
108
+ */
109
+ forget(): Promise<string | null>;
110
+ /** Calls `listener` whenever the token or consent state changes. Returns a function that stops listening. */
111
+ subscribe(listener: (snapshot: VisitorsSnapshot) => void): () => void;
112
+ }
113
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,qHAAqH;IACrH,GAAG,CAAC,EAAE,SAAS,GAAG,QAAQ,GAAG,IAAI,CAAA;IAEjC;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IAEb,+GAA+G;IAC/G,GAAG,CAAC,EAAE,OAAO,CAAA;IAEb,8FAA8F;IAC9F,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B;AAED,qGAAqG;AACrG,MAAM,WAAW,eAAe;IAC9B,4HAA4H;IAC5H,mBAAmB,EAAE,MAAM,CAAA;IAE3B,yDAAyD;IACzD,aAAa,EAAE,MAAM,CAAA;IAErB,uDAAuD;IACvD,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,mEAAmE;AACnE,MAAM,WAAW,YAAY;IAC3B,oGAAoG;IACpG,WAAW,EAAE,SAAS,GAAG,QAAQ,CAAA;IAEjC,oKAAoK;IACpK,MAAM,EAAE,MAAM,CAAA;IAEd,qFAAqF;IACrF,aAA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EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IAE7B;;;;OAIG;IACH,OAAO,IAAI,OAAO,CAAC,cAAc,CAAC,CAAA;IAElC;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IAE5D;;;;;;;OAOG;IACH,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAEhC,6GAA6G;IAC7G,SAAS,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,gBAAgB,KAAK,IAAI,GAAG,MAAM,IAAI,CAAA;CACtE"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import type { Visitors, VisitorsOptions } from './types.js';
2
+ /**
3
+ * Creates the object that holds a visitor's identity on your site, under their consent.
4
+ *
5
+ * **The token is kept on the visitor's device only while ebitex says that device may be recognised.** On a denial, a
6
+ * withdrawal, or a forget, it is removed. Out of the box the organization's default decides, and until an owner changes
7
+ * it that default is not to recognise, so every page load is a new visitor. Pass your consent tool's answer in
8
+ * `consent`, and keep it up to date with {@link Visitors.setConsent}.
9
+ *
10
+ * ```ts
11
+ * const visitors = createVisitors({ key: 'ebx_...', consent: { cmp: 'granted' } })
12
+ * await visitors.refresh()
13
+ * ```
14
+ */
15
+ export declare function createVisitors(options: VisitorsOptions): Visitors;
16
+ //# sourceMappingURL=visitors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitors.d.ts","sourceRoot":"","sources":["../src/visitors.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgD,QAAQ,EAAE,eAAe,EAAoC,MAAM,YAAY,CAAA;AAE3I;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ,CAkHjE"}
@@ -0,0 +1,132 @@
1
+ import { createClient, DEFAULT_ENDPOINT } from './client.js';
2
+ import { cookieStorage } from './storage.js';
3
+ /**
4
+ * Creates the object that holds a visitor's identity on your site, under their consent.
5
+ *
6
+ * **The token is kept on the visitor's device only while ebitex says that device may be recognised.** On a denial, a
7
+ * withdrawal, or a forget, it is removed. Out of the box the organization's default decides, and until an owner changes
8
+ * it that default is not to recognise, so every page load is a new visitor. Pass your consent tool's answer in
9
+ * `consent`, and keep it up to date with {@link Visitors.setConsent}.
10
+ *
11
+ * ```ts
12
+ * const visitors = createVisitors({ key: 'ebx_...', consent: { cmp: 'granted' } })
13
+ * await visitors.refresh()
14
+ * ```
15
+ */
16
+ export function createVisitors(options) {
17
+ const client = createClient(options.key, options.endpoint ?? DEFAULT_ENDPOINT, options.fetch ?? ((...args) => globalThis.fetch(...args)));
18
+ const storage = options.storage === false ? undefined : (options.storage ?? cookieStorage());
19
+ let consentOptions = { ...options.consent };
20
+ let token = null;
21
+ let consent = null;
22
+ const listeners = new Set();
23
+ // Calls run one after another: a refresh that overtakes a forget would otherwise hand back, and store, a token the forget just revoked.
24
+ let tail = Promise.resolve();
25
+ function run(task) {
26
+ const next = tail.then(task, task);
27
+ tail = next.catch(() => undefined);
28
+ return next;
29
+ }
30
+ // Storage is best effort in both directions: a visitor whose browser refuses a cookie is still a visitor.
31
+ async function persist(write) {
32
+ try {
33
+ await write();
34
+ }
35
+ catch {
36
+ // Intentionally ignored.
37
+ }
38
+ }
39
+ const snapshot = () => ({ token, consent });
40
+ const notify = () => {
41
+ const current = snapshot();
42
+ for (const listener of listeners)
43
+ listener(current);
44
+ };
45
+ const ready = (async () => {
46
+ if (!storage)
47
+ return;
48
+ try {
49
+ const stored = await storage.get();
50
+ if (stored && token === null)
51
+ token = stored;
52
+ }
53
+ catch {
54
+ // A storage that cannot be read is an empty one.
55
+ }
56
+ })();
57
+ function rawConsent() {
58
+ return {
59
+ cmp: consentOptions.cmp ?? null,
60
+ gpc: consentOptions.gpc ?? readBrowserFlag('globalPrivacyControl', (v) => v === true),
61
+ dnt: consentOptions.dnt ?? readBrowserFlag('doNotTrack', (v) => v === '1' || v === 'yes'),
62
+ cmpString: consentOptions.cmpString ?? null,
63
+ };
64
+ }
65
+ async function apply(session) {
66
+ token = session.visitor;
67
+ consent = session.consent;
68
+ if (storage) {
69
+ if (session.consent.recognition === 'granted' && session.consent.tokenKind === 'recognition') {
70
+ await persist(() => storage.set(session.visitor, { maxAgeSeconds: session.consent.tokenLifetimeSeconds }));
71
+ }
72
+ else {
73
+ await persist(() => storage.remove());
74
+ }
75
+ }
76
+ notify();
77
+ return session;
78
+ }
79
+ const visitors = {
80
+ get token() {
81
+ return token;
82
+ },
83
+ get consent() {
84
+ return consent;
85
+ },
86
+ ready,
87
+ refresh() {
88
+ return run(async () => {
89
+ await ready;
90
+ return apply(await client.session(token, rawConsent()));
91
+ });
92
+ },
93
+ async setConsent(next) {
94
+ consentOptions = { ...next };
95
+ if (next.cmp === 'denied') {
96
+ // A withdrawal takes effect now, not when the network answers: nothing may stay on the device meanwhile.
97
+ token = null;
98
+ if (storage)
99
+ await persist(() => storage.remove());
100
+ notify();
101
+ }
102
+ return visitors.refresh();
103
+ },
104
+ forget() {
105
+ return run(async () => {
106
+ await ready;
107
+ if (token === null)
108
+ return consent?.messages.forgetDone ?? null;
109
+ const message = await client.forget(token);
110
+ token = null;
111
+ consent = null;
112
+ if (storage)
113
+ await persist(() => storage.remove());
114
+ notify();
115
+ return message;
116
+ });
117
+ },
118
+ subscribe(listener) {
119
+ listeners.add(listener);
120
+ return () => {
121
+ listeners.delete(listener);
122
+ };
123
+ },
124
+ };
125
+ return visitors;
126
+ }
127
+ function readBrowserFlag(name, truthy) {
128
+ if (typeof navigator === 'undefined')
129
+ return false;
130
+ return truthy(navigator[name]);
131
+ }
132
+ //# sourceMappingURL=visitors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitors.js","sourceRoot":"","sources":["../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,OAAO,OAAO,EAAE,QAAQ,CAAC,UAAU,IAAI,IAAI,CAAA;gBAC/D,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;gBAC1C,KAAK,GAAG,IAAI,CAAA;gBACZ,OAAO,GAAG,IAAI,CAAA;gBACd,IAAI,OAAO;oBAAE,MAAM,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;gBAClD,MAAM,EAAE,CAAA;gBACR,OAAO,OAAO,CAAA;YAChB,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,SAAS,CAAC,QAAQ;YAChB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;YACvB,OAAO,GAAG,EAAE;gBACV,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YAC5B,CAAC,CAAA;QACH,CAAC;KACF,CAAA;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED,SAAS,eAAe,CAAC,IAAY,EAAE,MAAmC;IACxE,IAAI,OAAO,SAAS,KAAK,WAAW;QAAE,OAAO,KAAK,CAAA;IAClD,OAAO,MAAM,CAAE,SAAgD,CAAC,IAAI,CAAC,CAAC,CAAA;AACxE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
- {
2
- "name": "@ebitex/visitors-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@ebitex/visitors-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Recognise a returning visitor with their consent, honour Global Privacy Control and your own consent tool, and let them forget their device, from any site. No forms needed.",
5
+ "keywords": [
6
+ "ebitex",
7
+ "audience",
8
+ "consent",
9
+ "gdpr",
10
+ "privacy",
11
+ "global-privacy-control",
12
+ "visitor",
13
+ "forget-me"
14
+ ],
15
+ "license": "Apache-2.0",
16
+ "homepage": "https://developers.ebitex.io/getting-started/visitors/",
17
+ "type": "module",
18
+ "sideEffects": [
19
+ "./dist/script.js"
20
+ ],
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js"
28
+ },
29
+ "./react": {
30
+ "types": "./dist/react/index.d.ts",
31
+ "import": "./dist/react/index.js"
32
+ },
33
+ "./script": {
34
+ "types": "./dist/script.d.ts",
35
+ "import": "./dist/script.js"
36
+ }
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "scripts": {
42
+ "build": "tsc -p tsconfig.build.json",
43
+ "lint": "oxlint",
44
+ "test": "vitest run"
45
+ },
46
+ "peerDependencies": {
47
+ "react": ">=19"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "react": {
51
+ "optional": true
52
+ }
53
+ },
54
+ "devDependencies": {
55
+ "@testing-library/jest-dom": "^7.0.0",
56
+ "@testing-library/react": "^16.3.2",
57
+ "@types/react": "^19.2.17",
58
+ "@vitejs/plugin-react": "^6.0.3",
59
+ "jsdom": "^29.1.1",
60
+ "oxlint": "^1.71.0",
61
+ "react": "^19.2.7",
62
+ "react-dom": "^19.2.7",
63
+ "typescript": "~6.0.2",
64
+ "vitest": "^4.1.10"
65
+ }
66
+ }