@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.
Files changed (3) hide show
  1. package/README.md +85 -47
  2. package/index.esm.js +7 -0
  3. 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
- - 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
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
- - **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
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
- - [Overview](#overview)
23
- - [Installation](#installation)
24
- - [Getting Started](#getting-started)
25
- - [Components](#components)
26
- - [How A/B Testing Works](#how-ab-testing-works-with-dotcmsexperiments)
27
- - [Usage](#usage)
28
- - [Support](#support)
29
- - [Contributing](#contributing)
30
- - [Licensing](#licensing)
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
- ### Components
51
+ ### `withExperiments` Higher-Order Component
49
52
 
50
- ### `DotExperimentsProvider`
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
- #### Props
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
- - **config**: Configuration object for DotCMS Analytics integration
56
- - **apiKey**: Your API key from the DotCMS Analytics app
57
- - **server**: The URL of your dotCMS instance
58
- - **redirectFn**: The redirect function to use when assigning users to experiment variants
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
- import { DotExperimentsProvider } from "@dotcms/experiments";
64
- import { useRouter } from 'next/router';
78
+ "use client";
65
79
 
66
- const { replace } = useRouter();
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
- apiKey: 'your-api-key-from-dotcms-analytics-app',
70
- server: 'https://your-dotcms-instance.com',
71
- redirectFn: replace // Use replace from useRouter in Next.js
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
- return (
75
- <DotExperimentsProvider config={experimentConfig}>
76
-
77
- <Header>
78
- <Navigation />
79
- </Header>
80
- <DotcmsLayout />
81
- <Footer />
82
-
83
- </DotExperimentsProvider>
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
- ### How A/B Testing Works with @dotcms/experiments
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
- The A/B testing process with `@dotcms/experiments` is designed to be straightforward and automatic:
123
+ ### How It Works
90
124
 
91
- 1. **Experiment Assignment**: When a user visits a page that includes an experiment, the library first checks if the user has been assigned to an experiment variant. If not, it queries DotCMS Analytics to determine if there are active experiments and assigns the user to the appropriate variant
125
+ Once you wrap your component with `withExperiments`, the SDK automatically handles:
92
126
 
93
- 2. **Page Redirection**: If the user's assigned variant differs from the current page, the library automatically redirects the user to the correct variant page. This ensures that the user experiences the variant they have been assigned to
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
- 3. **Tracking Pageviews**: After redirection or upon visiting the page, the library sends a pageview event to DotCMS Analytics. This data is used to determine the effectiveness of each variant, ultimately helping to identify which variant performs better in the A/B test
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 more detailed information on A/B testing features and capabilities, visit the [DotCMS A/B Testing Experiments](https://www.dotcms.com/product/ab-testing-experiments) page.
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.0",
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.0",
29
- "@dotcms/react": "^1.2.0",
30
- "@dotcms/uve": "^1.2.0",
31
- "@dotcms/types": "^1.2.0"
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",