@dotcms/events 26.10.13-1 → 26.10.19-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -28
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ The `@dotcms/events` SDK is the official dotCMS library for everything your page
|
|
|
32
32
|
- [How It Works](#how-it-works)
|
|
33
33
|
- [Configuration](#configuration)
|
|
34
34
|
- [Usage](#usage)
|
|
35
|
-
- [
|
|
35
|
+
- [SDK Reference](#sdk-reference)
|
|
36
36
|
- [Migrating from @dotcms/analytics and @dotcms/experiments](#migrating-from-dotcmsanalytics-and-dotcmsexperiments)
|
|
37
37
|
- [Under the Hood](#under-the-hood)
|
|
38
38
|
- [Troubleshooting](#troubleshooting)
|
|
@@ -198,7 +198,7 @@ The [Next.js experiments example](https://github.com/dotCMS/core/tree/main/examp
|
|
|
198
198
|
|
|
199
199
|
### Pageviews
|
|
200
200
|
|
|
201
|
-
A pageview is sent when the page first loads, on every client-side navigation, and when the visitor returns to a page with the Back or Forward button. Each one carries the page's URL, title and
|
|
201
|
+
A pageview is sent when the page first loads, on every client-side navigation, and when the visitor returns to a page with the Back or Forward button. Each one carries the page's URL, title and locale, the campaign (UTM) parameters, and the visitor's device and screen.
|
|
202
202
|
|
|
203
203
|
To send them yourself instead, set `autoPageView: false` and call [`dotEvents.pageView()`](#doteventspageviewdata).
|
|
204
204
|
|
|
@@ -247,16 +247,16 @@ Inside the dotCMS Universal Visual Editor, the SDK sends nothing, and experiment
|
|
|
247
247
|
|
|
248
248
|
| Option | Type | Required | Default | Description |
|
|
249
249
|
| -------------- | ---------------------------------------- | -------- | ------- | --------------------------------------------------------------------------- |
|
|
250
|
-
| `dotcmsUrl` | `string` |
|
|
251
|
-
| `siteAuth` | `string` |
|
|
252
|
-
| `autoPageView` | `boolean` |
|
|
253
|
-
| `experiments` | `boolean \| DotCMSEventsExperimentsConfig` |
|
|
254
|
-
| `impressions` | `boolean \| DotCMSEventsImpressionsConfig` |
|
|
255
|
-
| `clicks` | `boolean` |
|
|
256
|
-
| `queue` | `boolean \| DotCMSEventsQueueConfig` |
|
|
257
|
-
| `debug` | `boolean` |
|
|
258
|
-
| `logLevel` | `'debug' \| 'info' \| 'warn' \| 'error'` |
|
|
259
|
-
| `onError` | `(error: DotCMSEventsError) => void` |
|
|
250
|
+
| `dotcmsUrl` | `string` | ✅ | — | The URL of your dotCMS instance, the same value `createDotCMSClient` takes |
|
|
251
|
+
| `siteAuth` | `string` | ✅ | — | The Site Auth from the Content Analytics app |
|
|
252
|
+
| `autoPageView` | `boolean` | ❌ | `true` | Send pageviews automatically |
|
|
253
|
+
| `experiments` | `boolean \| DotCMSEventsExperimentsConfig` | ❌ | `true` | Run experiments; `false` turns them off |
|
|
254
|
+
| `impressions` | `boolean \| DotCMSEventsImpressionsConfig` | ❌ | `false` | Record content impressions |
|
|
255
|
+
| `clicks` | `boolean` | ❌ | `false` | Record content clicks |
|
|
256
|
+
| `queue` | `boolean \| DotCMSEventsQueueConfig` | ❌ | `true` | Send events in batches; `false` sends each one at once |
|
|
257
|
+
| `debug` | `boolean` | ❌ | `false` | Log what the SDK does to the browser console |
|
|
258
|
+
| `logLevel` | `'debug' \| 'info' \| 'warn' \| 'error'` | ❌ | — | The least important level the console shows |
|
|
259
|
+
| `onError` | `(error: DotCMSEventsError) => void` | ❌ | — | Called when something fails that the page can't see |
|
|
260
260
|
|
|
261
261
|
### Impressions
|
|
262
262
|
|
|
@@ -299,6 +299,13 @@ dotEvents.init({
|
|
|
299
299
|
| `NETWORK` | An events request got no answer |
|
|
300
300
|
| `EXPERIMENTS` | Asking dotCMS for the visitor's variant failed. A 403 means experiments are off for the site; the SDK stops asking for a day |
|
|
301
301
|
|
|
302
|
+
Alongside `code` it carries:
|
|
303
|
+
|
|
304
|
+
- `message` (string): What went wrong.
|
|
305
|
+
- `status` (number): The HTTP status, when dotCMS answered.
|
|
306
|
+
- `detail` (unknown): What dotCMS said about the failure, for `REJECTED`.
|
|
307
|
+
- `events` (number): How many events the failed request carried, for `REJECTED` and `NETWORK`.
|
|
308
|
+
|
|
302
309
|
```ts
|
|
303
310
|
dotEvents.init({
|
|
304
311
|
dotcmsUrl: process.env.NEXT_PUBLIC_DOTCMS_HOST!,
|
|
@@ -365,47 +372,166 @@ window.addEventListener('pageshow', (event) => {
|
|
|
365
372
|
});
|
|
366
373
|
```
|
|
367
374
|
|
|
368
|
-
##
|
|
375
|
+
## SDK Reference
|
|
376
|
+
|
|
377
|
+
`dotEvents` and the types it uses are imported from `@dotcms/events`. The two pieces that print what a page needs to run its experiment have their own entries: `DotCMSExperiment` from `@dotcms/events/react`, and `experimentMarkup` from `@dotcms/events/markup`.
|
|
378
|
+
|
|
379
|
+
### `dotEvents`
|
|
380
|
+
|
|
381
|
+
`dotEvents` is the object every entry point shares. There is one per page: every import gets the same instance, configured by the one `init` call, and it carries the three methods below.
|
|
382
|
+
|
|
383
|
+
Every method does nothing on the server and inside the Universal Visual Editor, and none of them throws: failures reach your page through [`onError`](#errors).
|
|
369
384
|
|
|
370
385
|
### `dotEvents.init(config)`
|
|
371
386
|
|
|
372
|
-
|
|
387
|
+
`dotEvents.init` is the function that starts the SDK. Call it once, as early as possible: in Next.js that is `src/instrumentation-client.ts`, and in any other app wherever it starts, before it renders.
|
|
388
|
+
|
|
389
|
+
| Input | Type | Required | Description |
|
|
390
|
+
| -------- | -------------------- | -------- | ------------------------------------------- |
|
|
391
|
+
| `config` | `DotCMSEventsConfig` | ✅ | [The SDK's configuration](#config-options) |
|
|
392
|
+
|
|
393
|
+
#### Usage
|
|
394
|
+
|
|
395
|
+
```ts
|
|
396
|
+
import { dotEvents } from '@dotcms/events';
|
|
397
|
+
|
|
398
|
+
dotEvents.init({
|
|
399
|
+
dotcmsUrl: process.env.NEXT_PUBLIC_DOTCMS_HOST!,
|
|
400
|
+
siteAuth: process.env.NEXT_PUBLIC_DOTCMS_SITE_AUTH!,
|
|
401
|
+
impressions: true,
|
|
402
|
+
clicks: true
|
|
403
|
+
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
#### What a Second Call Does
|
|
407
|
+
|
|
408
|
+
- **The same configuration**: the second call does nothing.
|
|
409
|
+
- **Another configuration**: ignored, with a console warning.
|
|
410
|
+
- **`dotcmsUrl` or `siteAuth` missing**: nothing starts, and a console error says which one.
|
|
411
|
+
- **`conversion` or `pageView` called before `init` finishes**: the call waits and runs once the SDK is ready, up to 50 waiting calls.
|
|
412
|
+
- **Inside the Universal Visual Editor**: nothing is tracked, and experiment content is shown as it is.
|
|
373
413
|
|
|
374
414
|
### `dotEvents.conversion(name)`
|
|
375
415
|
|
|
376
|
-
|
|
416
|
+
`dotEvents.conversion` is the function that records a conversion. dotCMS records the name and the page; its schema accepts no other data, so a conversion carries no custom properties.
|
|
417
|
+
|
|
418
|
+
| Input | Type | Required | Description |
|
|
419
|
+
| ------ | -------- | -------- | ------------------------------------------------------- |
|
|
420
|
+
| `name` | `string` | ✅ | The conversion's name. An empty name is ignored with a console warning |
|
|
421
|
+
|
|
422
|
+
Call it only once the goal is reached — a completed purchase, a finished download, a created account — never on the click that started it.
|
|
423
|
+
|
|
424
|
+
#### Usage
|
|
425
|
+
|
|
426
|
+
```tsx
|
|
427
|
+
'use client';
|
|
428
|
+
|
|
429
|
+
import { dotEvents } from '@dotcms/events';
|
|
430
|
+
|
|
431
|
+
export function SignupForm() {
|
|
432
|
+
const onSubmit = async (event: React.FormEvent) => {
|
|
433
|
+
event.preventDefault();
|
|
434
|
+
await createAccount(); // your own code
|
|
435
|
+
|
|
436
|
+
dotEvents.conversion('signup');
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
return <form onSubmit={onSubmit}>{/* fields */}</form>;
|
|
440
|
+
}
|
|
441
|
+
```
|
|
377
442
|
|
|
378
443
|
### `dotEvents.pageView(data?)`
|
|
379
444
|
|
|
380
|
-
|
|
445
|
+
`dotEvents.pageView` is the function that sends a pageview by hand. Only needed with `autoPageView: false`.
|
|
446
|
+
|
|
447
|
+
| Input | Type | Required | Description |
|
|
448
|
+
| ------ | ------------------------ | -------- | ------------------------------------------------------------------ |
|
|
449
|
+
| `data` | `DotCMSEventsJsonObject` | ❌ | Your own data, which arrives in the event's `data.custom` |
|
|
450
|
+
|
|
451
|
+
On a page that runs an experiment, the pageview waits for the visitor's variant, and is dropped if the SDK sends the visitor to another variant: that page sends its own.
|
|
452
|
+
|
|
453
|
+
#### Usage
|
|
454
|
+
|
|
455
|
+
```ts
|
|
456
|
+
dotEvents.pageView({ campaign: 'spring' });
|
|
457
|
+
|
|
458
|
+
// A page the browser brings back with the Back button is seen again, without loading again
|
|
459
|
+
window.addEventListener('pageshow', (event) => {
|
|
460
|
+
if (event.persisted) {
|
|
461
|
+
dotEvents.pageView();
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
```
|
|
381
465
|
|
|
382
466
|
### `DotCMSExperiment`
|
|
383
467
|
|
|
468
|
+
`DotCMSExperiment` is a component that runs the page's experiment on the content it wraps. On a page that runs an experiment it wraps `children` in an element marked with the experiment and the variant the server rendered, keeps it hidden, and prints the script that decides the visitor's variant while the HTML is parsed. On a page that runs none it renders `children` alone, so every page can be wrapped the same way.
|
|
469
|
+
|
|
470
|
+
| Input | Type | Required | Default | Description |
|
|
471
|
+
| ----------- | ---------------------------- | -------- | ------- | ------------------------------------------------------------- |
|
|
472
|
+
| `page` | `DotCMSEventsExperimentPage` | ✅ | - | The page asset, requested with the URL's `variantName` |
|
|
473
|
+
| `children` | `ReactNode` | ✅ | - | What the experiment changes: the page's layout, or part of it |
|
|
474
|
+
| `nonce` | `string` | ❌ | - | The nonce of a nonce-based Content-Security-Policy |
|
|
475
|
+
| `className` | `string` | ❌ | - | A class for the element that wraps the content |
|
|
476
|
+
|
|
477
|
+
It uses no hooks, so it also renders in a server component — where it adds no JavaScript to the browser. In a client component it adds about 1.5 KB.
|
|
478
|
+
|
|
479
|
+
#### Usage
|
|
480
|
+
|
|
384
481
|
```tsx
|
|
482
|
+
// src/app/[[...slug]]/page.tsx
|
|
385
483
|
import { DotCMSExperiment } from '@dotcms/events/react';
|
|
386
|
-
```
|
|
387
484
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
485
|
+
export default async function Page({ params, searchParams }: PageProps) {
|
|
486
|
+
const { slug } = await params;
|
|
487
|
+
const { variantName } = await searchParams;
|
|
488
|
+
const path = slug?.length ? `/${slug.join('/')}` : '/';
|
|
489
|
+
|
|
490
|
+
// variantName makes dotCMS render the visitor's variant
|
|
491
|
+
const pageContent = await dotCMSClient.page.get(path, { variantName });
|
|
492
|
+
|
|
493
|
+
return (
|
|
494
|
+
<DotCMSExperiment page={pageContent.pageAsset}>
|
|
495
|
+
<DotCMSLayoutBody page={pageContent.pageAsset} components={pageComponents} />
|
|
496
|
+
</DotCMSExperiment>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
```
|
|
394
500
|
|
|
395
501
|
### `experimentMarkup(page)`
|
|
396
502
|
|
|
503
|
+
`experimentMarkup` is the function that returns what a server-rendered page prints to run its experiment, for frameworks other than React. It returns `null` when the page runs no experiment.
|
|
504
|
+
|
|
505
|
+
| Input | Type | Required | Description |
|
|
506
|
+
| ------ | ---------------------------- | -------- | ------------------------------------------------------------- |
|
|
507
|
+
| `page` | `DotCMSEventsExperimentPage` | ✅ | The page asset, requested with the URL's `variantName` |
|
|
508
|
+
|
|
509
|
+
#### Usage
|
|
510
|
+
|
|
397
511
|
```ts
|
|
398
512
|
import { experimentMarkup } from '@dotcms/events/markup';
|
|
513
|
+
|
|
514
|
+
const markup = experimentMarkup(pageAsset); // null when the page runs no experiment
|
|
515
|
+
|
|
516
|
+
// <style>{markup.style}</style>
|
|
517
|
+
// <script>{markup.script}</script>
|
|
518
|
+
// <div {...markup.attributes}>the content the experiment changes</div>
|
|
399
519
|
```
|
|
400
520
|
|
|
401
|
-
|
|
521
|
+
`style` is the rule that keeps the marked element hidden until the variant is decided, `script` is the boot script's source, and `attributes` are `data-dot-experiment` and `data-dot-variant` for the element that wraps what the experiment varies.
|
|
402
522
|
|
|
403
|
-
|
|
523
|
+
The style and the script must come **before** that element in the HTML the server sends: the script decides the visitor's variant while that HTML is parsed.
|
|
404
524
|
|
|
405
|
-
|
|
525
|
+
### `window.dotEvents`
|
|
406
526
|
|
|
407
|
-
|
|
408
|
-
|
|
527
|
+
After `init`, the same object is on `window`, so a plain script on the page reaches it without a bundler.
|
|
528
|
+
|
|
529
|
+
#### Usage
|
|
530
|
+
|
|
531
|
+
```html
|
|
532
|
+
<script>
|
|
533
|
+
window.dotEvents?.conversion('newsletter');
|
|
534
|
+
</script>
|
|
409
535
|
```
|
|
410
536
|
|
|
411
537
|
## Migrating from @dotcms/analytics and @dotcms/experiments
|
|
@@ -439,6 +565,17 @@ No cookies. Everything carries the `dot_events_` prefix:
|
|
|
439
565
|
| `dot_events_experiments_checked` | sessionStorage | This tab already asked for the visitor's assignments |
|
|
440
566
|
| `dot_events_session_experiments` | sessionStorage | The experiments the visitor joined during the session |
|
|
441
567
|
|
|
568
|
+
### What It Sends
|
|
569
|
+
|
|
570
|
+
Every event carries, besides its own data:
|
|
571
|
+
|
|
572
|
+
- The site's Site Auth, so dotCMS knows which site it belongs to
|
|
573
|
+
- The visitor's anonymous id and the session's id
|
|
574
|
+
- The visitor's screen resolution, viewport size and language
|
|
575
|
+
- The experiments the visitor joined in the session, on pages running an experiment they are in
|
|
576
|
+
|
|
577
|
+
It sends nothing else about the visitor, and no cookies. The four event types — `pageview`, `conversion`, `content_impression` and `content_click` — are the only ones dotCMS accepts; there is no custom-event API.
|
|
578
|
+
|
|
442
579
|
### Endpoints
|
|
443
580
|
|
|
444
581
|
| Request | What for |
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcms/events",
|
|
3
|
-
"version": "26.10.
|
|
3
|
+
"version": "26.10.19-1",
|
|
4
4
|
"description": "Official JavaScript library for dotCMS events: pageviews, conversions, content clicks and impressions, and experiments.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/dotCMS/core.git#main"
|
|
8
8
|
},
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"@dotcms/uve": "26.10.
|
|
10
|
+
"@dotcms/uve": "26.10.19-1",
|
|
11
11
|
"react": ">=18"
|
|
12
12
|
},
|
|
13
13
|
"peerDependenciesMeta": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"analytics": "^0.8.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@dotcms/types": "26.10.
|
|
24
|
+
"@dotcms/types": "26.10.19-1"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"dotCMS",
|