@dotcms/experiments 1.2.0 → 1.2.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 +85 -47
- package/index.esm.js +7 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -6,28 +6,31 @@ The `@dotcms/experiments` SDK is the official dotCMS JavaScript library that hel
|
|
|
6
6
|
|
|
7
7
|
### When to Use It
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
9
|
+
- Adding A/B testing capabilities to your web application
|
|
10
|
+
- Running experiments to optimize user experience
|
|
11
|
+
- Testing different page variants with real users
|
|
12
|
+
- Tracking experiment performance with DotCMS Analytics
|
|
13
13
|
|
|
14
14
|
### Key Features
|
|
15
15
|
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
16
|
+
- **User Assignment to Experiments**: Automatically assigns users to different experimental variants, ensuring diverse user experiences and reliable test data
|
|
17
|
+
- **Link Verification for Redirection**: Checks links to ensure users are redirected to their assigned experiment variant, maintaining the integrity of the testing process
|
|
18
|
+
- **Automatic PageView Event Sending**: Automatically sends PageView events to DotCMS Analytics, enabling real-time tracking of user engagement and experiment effectiveness
|
|
19
19
|
|
|
20
20
|
## Table of Contents
|
|
21
21
|
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
22
|
+
- [Overview](#overview)
|
|
23
|
+
- [Installation](#installation)
|
|
24
|
+
- [Getting Started](#getting-started)
|
|
25
|
+
- [withExperiments HOC](#withexperiments-higher-order-component)
|
|
26
|
+
- [Configuration Object](#configuration-object)
|
|
27
|
+
- [Usage](#usage)
|
|
28
|
+
- [With @dotcms/react](#with-dotcmsreact-recommended)
|
|
29
|
+
- [Configuration Best Practices](#configuration-best-practices)
|
|
30
|
+
- [How It Works](#how-it-works)
|
|
31
|
+
- [Support](#support)
|
|
32
|
+
- [Contributing](#contributing)
|
|
33
|
+
- [Licensing](#licensing)
|
|
31
34
|
|
|
32
35
|
## Installation
|
|
33
36
|
|
|
@@ -45,56 +48,91 @@ yarn add @dotcms/experiments
|
|
|
45
48
|
|
|
46
49
|
## Getting Started
|
|
47
50
|
|
|
48
|
-
###
|
|
51
|
+
### `withExperiments` Higher-Order Component
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
This component utilizes React's Context API to provide DotExperiments instances to its descendants, facilitating access to A/B testing features throughout your webapps.
|
|
53
|
+
The SDK exports a single HOC (Higher-Order Component) called `withExperiments` that wraps your page component with experiment functionality. This component handles:
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
- User assignment to experiment variants
|
|
56
|
+
- Automatic redirection to the correct variant
|
|
57
|
+
- Prevention of content flickering during variant loading
|
|
58
|
+
- Automatic page view tracking to dotCMS Analytics
|
|
59
|
+
- Click event handling to maintain variant consistency across navigation
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
### Configuration Object
|
|
62
|
+
|
|
63
|
+
The `config` object passed to `withExperiments` accepts the following properties:
|
|
64
|
+
|
|
65
|
+
- **apiKey** (required): Your API key from the DotCMS Analytics app
|
|
66
|
+
- **server** (required): The URL of your dotCMS instance (e.g., `https://your-dotcms-instance.com`)
|
|
67
|
+
- **redirectFn** (optional): Custom redirect function for SPA navigation (default: `window.location.replace`)
|
|
68
|
+
- **trackPageView** (optional): Enable/disable automatic page view tracking (default: `true`)
|
|
69
|
+
- **debug** (optional): Enable debug logging in the browser console (default: `false`)
|
|
59
70
|
|
|
60
71
|
## Usage
|
|
61
72
|
|
|
73
|
+
### With @dotcms/react (Recommended)
|
|
74
|
+
|
|
75
|
+
The recommended approach is to wrap the `DotCMSLayoutBody` component with `withExperiments`. The pattern supports **conditional wrapping** - experiments are only enabled when an API key is configured:
|
|
76
|
+
|
|
62
77
|
```javascript
|
|
63
|
-
|
|
64
|
-
import { useRouter } from 'next/router';
|
|
78
|
+
"use client";
|
|
65
79
|
|
|
66
|
-
|
|
80
|
+
import { withExperiments } from "@dotcms/experiments";
|
|
81
|
+
import { DotCMSLayoutBody, useEditableDotCMSPage } from "@dotcms/react";
|
|
82
|
+
import { useRouter } from 'next/navigation';
|
|
67
83
|
|
|
84
|
+
// Experiment configuration - only applied if apiKey is present
|
|
68
85
|
const experimentConfig = {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
86
|
+
apiKey: process.env.NEXT_PUBLIC_EXPERIMENTS_API_KEY,
|
|
87
|
+
server: process.env.NEXT_PUBLIC_DOTCMS_HOST,
|
|
88
|
+
debug: true
|
|
72
89
|
};
|
|
73
90
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
export function Page({ pageContent }) {
|
|
92
|
+
const { pageAsset } = useEditableDotCMSPage(pageContent);
|
|
93
|
+
const { replace } = useRouter();
|
|
94
|
+
|
|
95
|
+
// Conditionally wrap with experiments if apiKey is configured
|
|
96
|
+
const DotCMSLayoutBodyComponent = experimentConfig.apiKey
|
|
97
|
+
? withExperiments(DotCMSLayoutBody, {
|
|
98
|
+
...experimentConfig,
|
|
99
|
+
redirectFn: replace
|
|
100
|
+
})
|
|
101
|
+
: DotCMSLayoutBody;
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<main>
|
|
105
|
+
<DotCMSLayoutBodyComponent
|
|
106
|
+
page={pageAsset}
|
|
107
|
+
components={pageComponents}
|
|
108
|
+
/>
|
|
109
|
+
</main>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
85
112
|
```
|
|
86
113
|
|
|
87
|
-
|
|
114
|
+
> 📚 **Learn more about `DotCMSLayoutBody`**: See the [@dotcms/react SDK documentation](https://github.com/dotCMS/core/blob/main/core-web/libs/sdk/react/README.md#dotcmslayoutbody) for complete details on configuring the layout renderer, component mapping, and available props.
|
|
115
|
+
|
|
116
|
+
### Configuration Best Practices
|
|
117
|
+
|
|
118
|
+
- **Use Environment Variables**: Store your API key and server URL in environment variables
|
|
119
|
+
- **Conditional Wrapping**: Only enable experiments when an API key is configured using a ternary operator
|
|
120
|
+
- **Framework Router**: Pass your framework's router function (e.g., `router.replace` for Next.js) to maintain SPA navigation
|
|
121
|
+
- **Debug Mode**: Enable debug logging during development to troubleshoot experiment assignment issues
|
|
88
122
|
|
|
89
|
-
|
|
123
|
+
### How It Works
|
|
90
124
|
|
|
91
|
-
|
|
125
|
+
Once you wrap your component with `withExperiments`, the SDK automatically handles:
|
|
92
126
|
|
|
93
|
-
|
|
127
|
+
1. **User Assignment**: Assigns users to experiment variants when they visit a page with an active experiment
|
|
128
|
+
2. **Automatic Redirection**: Redirects users to their assigned variant URL (prevents seeing the wrong variant)
|
|
129
|
+
3. **Flicker Prevention**: Hides content during redirection to avoid showing the wrong variant momentarily
|
|
130
|
+
4. **Navigation Handling**: Maintains variant consistency when users click links
|
|
131
|
+
5. **Analytics Tracking**: Sends pageview events to DotCMS Analytics automatically
|
|
94
132
|
|
|
95
|
-
|
|
133
|
+
All of this happens behind the scenes - you just need to wrap your component and provide the configuration.
|
|
96
134
|
|
|
97
|
-
**Learn More**: For
|
|
135
|
+
**Learn More**: For detailed information on creating and managing experiments in dotCMS, visit the [DotCMS A/B Testing Experiments](https://www.dotcms.com/product/ab-testing-experiments) page.
|
|
98
136
|
|
|
99
137
|
## Support
|
|
100
138
|
|
package/index.esm.js
CHANGED
|
@@ -1673,6 +1673,13 @@ const useExperimentVariant = data => {
|
|
|
1673
1673
|
setShouldWaitForVariant(false);
|
|
1674
1674
|
return;
|
|
1675
1675
|
}
|
|
1676
|
+
// If variantId is not provided, show content and warn
|
|
1677
|
+
if (!variantId) {
|
|
1678
|
+
// eslint-disable-next-line no-console
|
|
1679
|
+
console.warn('[DotExperiments] variantId is required but missing. ' + 'Please ensure the page data includes variantId in viewAs. ' + 'Showing content to prevent blank screen.');
|
|
1680
|
+
setShouldWaitForVariant(false);
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1676
1683
|
const location = typeof window !== 'undefined' ? window.location : undefined;
|
|
1677
1684
|
if (location && dotExperimentInstance) {
|
|
1678
1685
|
const variantAssigned = dotExperimentInstance.getVariantFromHref(location.pathname);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcms/experiments",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Official JavaScript library to use Experiments with DotCMS.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"react": ">=18",
|
|
27
27
|
"react-dom": ">=18",
|
|
28
|
-
"@dotcms/client": "^1.2.
|
|
29
|
-
"@dotcms/react": "^1.2.
|
|
30
|
-
"@dotcms/uve": "^1.2.
|
|
31
|
-
"@dotcms/types": "^1.2.
|
|
28
|
+
"@dotcms/client": "^1.2.1",
|
|
29
|
+
"@dotcms/react": "^1.2.1",
|
|
30
|
+
"@dotcms/uve": "^1.2.1",
|
|
31
|
+
"@dotcms/types": "^1.2.1"
|
|
32
32
|
},
|
|
33
33
|
"module": "./index.esm.js",
|
|
34
34
|
"type": "module",
|