@releval/tracker 1.0.0-bootstrap.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 +190 -0
- package/README.md +366 -0
- package/dist/ResultImpressionCollector.d.cts +762 -0
- package/dist/ResultImpressionCollector.d.mts +762 -0
- package/dist/react.cjs +180 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +139 -0
- package/dist/react.d.mts +139 -0
- package/dist/react.mjs +174 -0
- package/dist/react.mjs.map +1 -0
- package/dist/releval-tracker.cjs +8 -0
- package/dist/releval-tracker.d.cts +135 -0
- package/dist/releval-tracker.d.mts +135 -0
- package/dist/releval-tracker.global.js +2 -0
- package/dist/releval-tracker.mjs +3 -0
- package/dist/tracker.cjs +1663 -0
- package/dist/tracker.cjs.map +1 -0
- package/dist/tracker.mjs +1634 -0
- package/dist/tracker.mjs.map +1 -0
- package/package.json +131 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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 the 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 the 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 any 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
|
+
Copyright 2025 Releval
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
# @releval/tracker
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript library for tracking [User Behavior Insights](https://www.ubisearch.dev/) on websites.
|
|
4
|
+
It reports searches, result impressions, result clicks, and the conversions that follow, to a [Releval](https://releval.co) deployment, using the standard UBI event shape so everything joins back to the originating query.
|
|
5
|
+
|
|
6
|
+
The full integration guide, covering Site registration, the `query_id` flow, and how the pieces fit together, is [Browser tracker](https://releval.co/docs/user-behavior-insights/browser-tracker) in the Releval documentation.
|
|
7
|
+
|
|
8
|
+
Highlights:
|
|
9
|
+
|
|
10
|
+
- High-level API for the core search interactions: `trackSearch`, `trackResultImpression`, `trackResultClick`, `trackResultEvent`
|
|
11
|
+
- Click-time attribution: a conversion on a later page automatically resolves the query that produced it
|
|
12
|
+
- Declarative collectors that bind server-rendered markup via data attributes
|
|
13
|
+
- React adapter on the `@releval/tracker/react` subpath
|
|
14
|
+
- Batched delivery with retries, plus the Beacon API for events fired during page unload
|
|
15
|
+
- Automatic session and anonymous client IDs
|
|
16
|
+
- Full TypeScript definitions, one runtime dependency ([ulidx](https://github.com/perry-mitchell/ulidx)), ~8.4 kB gzipped
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @releval/tracker
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Without a bundler, serve the IIFE build (`dist/releval-tracker.global.js`) from your own static assets and load it with a `<script>` tag; it exposes a global `Releval`.
|
|
25
|
+
The same file is available as the `./script` subpath (`import '@releval/tracker/script'`) and via jsDelivr/unpkg, though self-hosting is recommended.
|
|
26
|
+
|
|
27
|
+
You also need a **Site** registered in your deployment of Releval before events will be accepted.
|
|
28
|
+
A Site issues the public `site_id` the tracker stamps on every event and declares which origins may submit them.
|
|
29
|
+
Events carrying an unknown `site_id`, or arriving from an origin outside the allow list, are dropped.
|
|
30
|
+
See [Sites](https://releval.co/docs/user-behavior-insights/sites).
|
|
31
|
+
|
|
32
|
+
The tracker works with Releval v1.0.0 and later.
|
|
33
|
+
|
|
34
|
+
## Quick start
|
|
35
|
+
|
|
36
|
+
### ES module (React, Vue, Angular, etc.)
|
|
37
|
+
|
|
38
|
+
In a single-page app you call the tracking methods directly.
|
|
39
|
+
Report the search when your backend returns results, then report impressions and clicks against that `query_id`:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { Tracker } from '@releval/tracker';
|
|
43
|
+
|
|
44
|
+
const tracker = new Tracker({
|
|
45
|
+
application: 'my-app',
|
|
46
|
+
siteId: 'YOUR_SITE_ID', // issued by Releval when you register a Site
|
|
47
|
+
endpointHost: 'https://releval.example.com', // the URL browsers use to reach Releval
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
tracker.start();
|
|
51
|
+
|
|
52
|
+
// When your search backend returns results (it issues the query_id):
|
|
53
|
+
tracker.trackSearch({ query: 'wireless headphones', queryId: response.query_id });
|
|
54
|
+
|
|
55
|
+
// When a result scrolls into view:
|
|
56
|
+
tracker.trackResultImpression({
|
|
57
|
+
queryId: response.query_id,
|
|
58
|
+
query: 'wireless headphones',
|
|
59
|
+
items: results.map((r, i) => ({ objectId: r.sku, ordinal: i + 1 })),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// When the user clicks a result (also records attribution for later conversions):
|
|
63
|
+
tracker.trackResultClick({
|
|
64
|
+
queryId: response.query_id,
|
|
65
|
+
query: 'wireless headphones',
|
|
66
|
+
objectId: result.sku,
|
|
67
|
+
ordinal: rank, // absolute 1-based rank across pagination
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Later - another route, or after a full page navigation - the conversion
|
|
71
|
+
// picks up the query recorded at click time:
|
|
72
|
+
tracker.trackResultEvent({ actionName: 'add_to_cart', objectId: result.sku });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Omit `endpointHost` during development and events are logged to the console instead of being sent anywhere.
|
|
76
|
+
|
|
77
|
+
### React
|
|
78
|
+
|
|
79
|
+
The `@releval/tracker/react` subpath ships a provider that owns the tracker's lifecycle, a hook to read it, a `SearchResults` context declaring which search produced the results beneath it, and a ref hook for impressions.
|
|
80
|
+
React is an optional peer dependency, so this is only pulled in when you import it.
|
|
81
|
+
The entry carries the `'use client'` directive, so it works under the Next.js App Router.
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import {
|
|
85
|
+
SearchResults,
|
|
86
|
+
TrackerProvider,
|
|
87
|
+
useResultImpression,
|
|
88
|
+
useSearchResults,
|
|
89
|
+
useTracker,
|
|
90
|
+
} from '@releval/tracker/react';
|
|
91
|
+
|
|
92
|
+
function Root() {
|
|
93
|
+
return (
|
|
94
|
+
<TrackerProvider options={{ application: 'my-app', siteId: 'YOUR_SITE_ID', endpointHost: 'https://releval.example.com' }}>
|
|
95
|
+
<App />
|
|
96
|
+
</TrackerProvider>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function ResultsPage({ response, query }) {
|
|
101
|
+
return (
|
|
102
|
+
<SearchResults queryId={response.query_id} query={query}>
|
|
103
|
+
{response.results.map((r, i) => (
|
|
104
|
+
<ResultCard key={r.sku} result={r} ordinal={i + 1} />
|
|
105
|
+
))}
|
|
106
|
+
</SearchResults>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function ResultCard({ result, ordinal }) {
|
|
111
|
+
const tracker = useTracker();
|
|
112
|
+
const search = useSearchResults();
|
|
113
|
+
// One impression per query, the first time the card becomes visible.
|
|
114
|
+
const ref = useResultImpression({ objectId: result.sku, ordinal });
|
|
115
|
+
return (
|
|
116
|
+
<article
|
|
117
|
+
ref={ref}
|
|
118
|
+
onClick={() =>
|
|
119
|
+
search &&
|
|
120
|
+
tracker.trackResultClick({
|
|
121
|
+
objectId: result.sku,
|
|
122
|
+
ordinal,
|
|
123
|
+
queryId: search.queryId,
|
|
124
|
+
query: search.query,
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
>
|
|
128
|
+
{result.name}
|
|
129
|
+
</article>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`TrackerProvider` starts the tracker on mount and stops it on unmount, and `options` is read once. If you need to change
|
|
135
|
+
identity after login, use `tracker.setUserId`.
|
|
136
|
+
Events dispatched by child mount effects before the provider starts are buffered and replayed, so nothing is lost on a hard load of a deep route.
|
|
137
|
+
`useTracker()` throws if used outside a provider.
|
|
138
|
+
|
|
139
|
+
### Script tag (no bundler)
|
|
140
|
+
|
|
141
|
+
Load the IIFE build, then configure the tracker in a `defer`red script that runs after it:
|
|
142
|
+
|
|
143
|
+
```html
|
|
144
|
+
<script defer src="path/to/releval-tracker.global.js"></script>
|
|
145
|
+
<script defer src="tracker-setup.js"></script>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Server-rendered pages can bind results declaratively: put
|
|
149
|
+
|
|
150
|
+
1. `data-query-id` (and optionally `data-query`) on the results container
|
|
151
|
+
2. `data-object-id` and `data-ordinal` on each result, and the collectors will do the rest:
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
// tracker-setup.js - runs after the deferred library above has loaded
|
|
155
|
+
var tracker = new Releval.Tracker({
|
|
156
|
+
application: 'my-site',
|
|
157
|
+
siteId: 'YOUR_SITE_ID', // issued by Releval when you register a Site
|
|
158
|
+
endpointHost: 'https://releval.example.com',
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
tracker.trackResultClicks({
|
|
162
|
+
selector: '[data-query-id] [data-object-id]',
|
|
163
|
+
ignore: '[data-add-to-cart]', // nested buttons are not result clicks
|
|
164
|
+
});
|
|
165
|
+
tracker.trackResultImpressions({
|
|
166
|
+
selector: '[data-query-id] [data-object-id]',
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Conversions: buttons carrying data-action-name (e.g. "add_to_cart") are
|
|
170
|
+
// routed through trackResultEvent, which resolves the originating query
|
|
171
|
+
// from the recorded result click - on this page or a later one.
|
|
172
|
+
tracker.trackResultClicks({ selector: '[data-add-to-cart]' });
|
|
173
|
+
|
|
174
|
+
// Report the search this page was rendered for, so queries with no clicks
|
|
175
|
+
// are still recorded.
|
|
176
|
+
var grid = document.querySelector('[data-query-id]');
|
|
177
|
+
if (grid) {
|
|
178
|
+
tracker.trackSearch({ queryId: grid.dataset.queryId, query: grid.dataset.query });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
tracker.start();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
```html
|
|
185
|
+
<div data-query-id="<%= queryId %>" data-query="<%= query %>">
|
|
186
|
+
<a data-object-id="SKU-1" data-ordinal="1" href="/p/SKU-1">...
|
|
187
|
+
<button data-add-to-cart data-object-id="SKU-1" data-action-name="add_to_cart">Add</button>
|
|
188
|
+
</a>
|
|
189
|
+
</div>
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Every full-page navigation builds a fresh `Tracker`, so this setup runs again on each page.
|
|
193
|
+
That is fine: the client ID, session, and click-time attribution persist in browser storage, so a conversion several pages later still resolves its query.
|
|
194
|
+
Attribution is scoped to a session (30-minute inactivity window by default), so a conversion in a later session is
|
|
195
|
+
sent unattributed rather than joined to a stale query.
|
|
196
|
+
When results come back on the same page (e.g. an AJAX search), also report the search itself: `tracker.trackSearch({ query, queryId })`.
|
|
197
|
+
|
|
198
|
+
## What you can track
|
|
199
|
+
|
|
200
|
+
### Searches
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
tracker.trackSearch({ query, queryId });
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Reports the search itself as a `search` event with `message_type: QUERY`.
|
|
207
|
+
Call it when your backend returns results, so that queries with no clicks are still recorded and abandonment can be measured.
|
|
208
|
+
The `queryId` comes from your search backend's track-query call to Releval; the tracker never invents one.
|
|
209
|
+
|
|
210
|
+
### Result impressions
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
tracker.trackResultImpression({ items: [{ objectId, ordinal }], queryId, query? });
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
One impression per result that became visible, deduplicated per `(queryId, objectId)` for the life of the tracker.
|
|
217
|
+
In React, prefer the `useResultImpression` ref hook, which fires when the element first becomes visible.
|
|
218
|
+
|
|
219
|
+
### Result clicks
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
tracker.trackResultClick({ objectId, ordinal, queryId, query?, objectIdField?, actionName? });
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Reports a click on a result.
|
|
226
|
+
`queryId` and `ordinal` are required, so the click can always be joined back to the query and rank that produced it.
|
|
227
|
+
The click also records attribution for the object, which later conversions use.
|
|
228
|
+
|
|
229
|
+
### Conversions
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
tracker.trackResultEvent({ actionName, objectId, ordinal?, queryId?, query?, objectIdField? });
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Reports a follow-on action such as `add_to_cart`, `purchase`, or `view`.
|
|
236
|
+
`queryId` and `ordinal` are optional here: when omitted, they resolve from the attribution recorded by `trackResultClick` for the same `objectId` - on this page or a later one.
|
|
237
|
+
`tracker.getResultAttribution(objectId)` returns the recorded attribution (`{ queryId, ordinal?, query? } | undefined`) if you need it for a custom payload.
|
|
238
|
+
|
|
239
|
+
### Custom events
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
tracker.dispatch({ action_name: 'begin_checkout', event_attributes: { cart_total: total } });
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Sends any event through the pipeline when none of the high-level methods fit.
|
|
246
|
+
The pipeline stamps `timestamp`, IDs, and context onto the object you pass (it is mutated, not copied).
|
|
247
|
+
`EventAttributes`, `EventObject` and `EventPosition` types are exported.
|
|
248
|
+
|
|
249
|
+
### Declarative collectors
|
|
250
|
+
|
|
251
|
+
For server-rendered pages, bind results by data attributes instead of writing per-interaction code.
|
|
252
|
+
Both methods return a function that stops the collection again.
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
tracker.trackResultClicks({ selector, root?, ignore?, resolve? }); // result clicks
|
|
256
|
+
tracker.trackResultImpressions({ selector, root?, resolve? }); // one impression per visible result
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
The default resolver reads `data-object-id`, `data-ordinal`, `data-object-id-field` and optionally `data-action-name` from the matched element, and `data-query-id` / `data-query` from the nearest ancestor carrying `data-query-id` (the element itself counts).
|
|
260
|
+
A custom `resolve(element)` can read anything else; returning `undefined` skips the element.
|
|
261
|
+
The default resolution is exported as `readResultData` for custom resolvers that only need to add to it.
|
|
262
|
+
Clicks that cannot be resolved into a complete event are skipped with a single warning, and an ordinal must be a positive integer.
|
|
263
|
+
Elements added or re-rendered after `start()` are covered automatically (delegation plus a mutation observer).
|
|
264
|
+
Keep the selector scoped to your result elements: one that matches `<body>` itself is skipped with a warning.
|
|
265
|
+
|
|
266
|
+
### About `ordinal`
|
|
267
|
+
|
|
268
|
+
`ordinal` is the absolute, 1-based rank across pagination: `(page - 1) * pageSize + positionOnPage` with `page` 1-based.
|
|
269
|
+
It must equal index + 1 of the object in the `query_response_hit_ids` your backend sent to track-query for that `query_id`.
|
|
270
|
+
So either one `query_id` spans all pages with absolute ordinals, or each page fetch is its own track-query call.
|
|
271
|
+
An `ordinal` that is not a positive integer is dropped with a one-time warning rather than sent.
|
|
272
|
+
|
|
273
|
+
### What is attached automatically
|
|
274
|
+
|
|
275
|
+
Every event is enriched before delivery with:
|
|
276
|
+
|
|
277
|
+
- `timestamp`, a per-event `event_id` (ULID), and the tracker version
|
|
278
|
+
- `application`, `site_id`, and `user_id` from your options
|
|
279
|
+
- `session_id` and `client_id` (see [Identity](#identity))
|
|
280
|
+
- `event_attributes.browser` (user agent, language, screen; `webdriver: true` when automation is detected, so bot traffic can be filtered in analysis)
|
|
281
|
+
- `event_attributes.page` (URL, title, referrer)
|
|
282
|
+
|
|
283
|
+
You can add your own context with a custom enricher, or mirror events to another destination with a custom sink.
|
|
284
|
+
Both return a disposer:
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
tracker.addEnricher({
|
|
288
|
+
enrich(event) {
|
|
289
|
+
event.event_attributes.experiment = { variant: 'b' }; // your A/B split key
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
tracker.addSink({
|
|
294
|
+
emit(event) {
|
|
295
|
+
myAnalytics.send(event);
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Configuration
|
|
301
|
+
|
|
302
|
+
Delivering to Releval requires the pair `endpointHost` + `siteId` together (the TypeScript type enforces it).
|
|
303
|
+
Omit `endpointHost` for development mode: events go to the console - or only to sinks you add, once any are added - and nothing leaves the page.
|
|
304
|
+
With env-driven config, spread the pair together so the type holds:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
const tracker = new Tracker({
|
|
308
|
+
application: 'my-app',
|
|
309
|
+
...(import.meta.env.VITE_RELEVAL_HOST
|
|
310
|
+
? {
|
|
311
|
+
endpointHost: import.meta.env.VITE_RELEVAL_HOST,
|
|
312
|
+
siteId: import.meta.env.VITE_RELEVAL_SITE_ID,
|
|
313
|
+
}
|
|
314
|
+
: {}),
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
| Option | Type | Default | Description |
|
|
319
|
+
|---|---|---|---|
|
|
320
|
+
| `application` | `string` | *required* | Application name attached to every event. Match it to what your backend sends to track-query. |
|
|
321
|
+
| `endpointHost` | `string` | `undefined` | Base URL browsers use to reach the Releval deployment - not your site's own origin. A reverse-proxy path prefix is allowed; events POST to `<endpointHost>/api/v1/ubi/track-event`. |
|
|
322
|
+
| `siteId` | `string` | `undefined` | Public Site identifier issued by Releval. Required with `endpointHost`; events whose `site_id` does not match a registered Site are dropped server-side. |
|
|
323
|
+
| `userId` | `string` | `undefined` | Initial user id. Must be an opaque, pseudonymous identifier - never an email. Change it mid-session with `setUserId`. |
|
|
324
|
+
| `sessionInactivityTimeoutMs` | `number` | `1800000` (30 min) | Inactivity timeout before rotating the session |
|
|
325
|
+
| `maxSessionDurationMs` | `number` | `86400000` (24 hr) | Max session duration before forced rotation |
|
|
326
|
+
| `debug` | `boolean` | `false` | Log every dispatched event and every delivery outcome to the console. Useful when setting up: the server accepts every request with `202` and validates asynchronously, so a logged delivery proves transport, not acceptance - confirm arrival in the Site's activity column in Releval. |
|
|
327
|
+
| `logger` | `Logger` | `ConsoleLogger` | Replace the default console logger to route (or silence) tracker diagnostics. To fan out to several destinations, pass a logger that wraps them. |
|
|
328
|
+
|
|
329
|
+
## Identity
|
|
330
|
+
|
|
331
|
+
| Member | Description |
|
|
332
|
+
|---|---|
|
|
333
|
+
| `sessionId` | Current session ID (auto-rotates on inactivity or max duration; a page load counts as activity) |
|
|
334
|
+
| `clientId` | Stable anonymous client ID (persisted in `localStorage`) |
|
|
335
|
+
| `setUserId(id \| undefined)` | Set or clear the user id mid-session (login/logout). Applies to events dispatched after the call; an explicit `user_id` on an event still wins. |
|
|
336
|
+
|
|
337
|
+
Forward `tracker.clientId` (and `tracker.sessionId`) with your search request, and have your backend pass the client id as `client_id` on its track-query call.
|
|
338
|
+
It is the only browser-side key on the query record, so without it queries cannot be joined to browser events - including queries that got no clicks at all.
|
|
339
|
+
|
|
340
|
+
## Lifecycle
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
tracker.start(); // Attach collectors, replay anything dispatched early, start the session
|
|
344
|
+
await tracker.flush(); // Force-deliver queued events now (e.g. before a critical action)
|
|
345
|
+
tracker.stop(); // Detach collectors, flush pending events
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Dispatches before `start()` are buffered (bounded, stamped with the moment they happened and the user identity current at the time) and replayed at `start()` - framework lifecycles routinely dispatch first.
|
|
349
|
+
Dispatches after `stop()` are dropped, not queued.
|
|
350
|
+
`flush()` resolves once the current batch has been sent.
|
|
351
|
+
On a normal page unload you do not need it - delivery already falls back to the Beacon API.
|
|
352
|
+
|
|
353
|
+
## Examples
|
|
354
|
+
|
|
355
|
+
Two example apps demonstrate the library in different integration styles:
|
|
356
|
+
|
|
357
|
+
| Example | Path | Description |
|
|
358
|
+
|---|---|---|
|
|
359
|
+
| **HTML script tag** | [`examples/html-script/`](examples/html-script/) | Multi-page e-commerce site using the IIFE build, declarative collectors and cross-page attribution |
|
|
360
|
+
| **React SPA** | [`examples/react-spa/`](examples/react-spa/) | Single-page React app (Vite + Chakra UI + React Router) using the ESM build and the React adapter |
|
|
361
|
+
|
|
362
|
+
Both implement a "RelevalTech" storefront with catalog, product detail, cart, and checkout pages, a floating event log panel, and the full search, impression, click, and conversion flow.
|
|
363
|
+
|
|
364
|
+
## Developing
|
|
365
|
+
|
|
366
|
+
See [DEVELOPING.md](DEVELOPING.md) for project structure, development commands, architecture, and build outputs.
|