@dotcms/experiments 0.0.1-alpha
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 +106 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +6536 -0
- package/package.json +33 -0
- package/src/index.d.ts +3 -0
- package/src/lib/components/DotExperimentsProvider.d.ts +47 -0
- package/src/lib/contexts/DotExperimentsContext.d.ts +12 -0
- package/src/lib/dot-experiments.d.ts +289 -0
- package/src/lib/hooks/useExperiments.d.ts +14 -0
- package/src/lib/shared/constants.d.ts +95 -0
- package/src/lib/shared/mocks/mock.d.ts +41 -0
- package/src/lib/shared/models.d.ts +201 -0
- package/src/lib/shared/parser/parser.d.ts +54 -0
- package/src/lib/shared/persistence/index-db-database-handler.d.ts +87 -0
- package/src/lib/shared/utils/DotLogger.d.ts +15 -0
- package/src/lib/shared/utils/utils.d.ts +73 -0
- package/src/lib/standalone.d.ts +7 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the configuration for the SDK experiment.
|
|
3
|
+
*
|
|
4
|
+
* @interface DotExperimentConfig
|
|
5
|
+
*/
|
|
6
|
+
export interface DotExperimentConfig {
|
|
7
|
+
/**
|
|
8
|
+
* This is `Analytics Key` provided by Analytics Integration App at DotCMS.
|
|
9
|
+
*/
|
|
10
|
+
apiKey: string;
|
|
11
|
+
/**
|
|
12
|
+
* The URL of the Public DotCMS Host server for the SDK to connect to.
|
|
13
|
+
*/
|
|
14
|
+
server: string;
|
|
15
|
+
/**
|
|
16
|
+
* A flag that determines whether the experiment is in debug mode or not. When in debug mode, the SDK could provide more logging information.
|
|
17
|
+
*/
|
|
18
|
+
debug: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* An optional property. True by default to automatically track page views. It's useful for understanding user behavior in the experiment.
|
|
21
|
+
*/
|
|
22
|
+
trackPageView?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* An optional property. It is a function that handles URL redirections. When supplied, the SDK will call this function instead of using the default browser redirect when it's necessary to redirect the page.
|
|
25
|
+
* @param url
|
|
26
|
+
*/
|
|
27
|
+
redirectFn?: (url: string) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Represents the configuration for the LookBackWindow.
|
|
31
|
+
*
|
|
32
|
+
* @interface LookBackWindow
|
|
33
|
+
*/
|
|
34
|
+
export interface LookBackWindow {
|
|
35
|
+
/**
|
|
36
|
+
* Defines the time period in milliseconds when the LookBackWindow should expire.
|
|
37
|
+
*/
|
|
38
|
+
expireMillis: number;
|
|
39
|
+
/**
|
|
40
|
+
* A value indicating the expiration timestamp of the experiment.
|
|
41
|
+
*/
|
|
42
|
+
expireTime?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Represents the associated value with the LookBackWindow.
|
|
45
|
+
*/
|
|
46
|
+
value: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Represents the configurations for checking whether the current page is an Experiment page, or a target.
|
|
50
|
+
*
|
|
51
|
+
* @interface Regexs
|
|
52
|
+
*/
|
|
53
|
+
interface Regexs {
|
|
54
|
+
/**
|
|
55
|
+
* A regular expression for validating if the page is an Experiment page.
|
|
56
|
+
*/
|
|
57
|
+
isExperimentPage: string;
|
|
58
|
+
/**
|
|
59
|
+
* A regular expression for validating if the page is the target page. This can be null.
|
|
60
|
+
*/
|
|
61
|
+
isTargetPage: string | null;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Represents a variant that is applied when a request is made.
|
|
65
|
+
*
|
|
66
|
+
* @interface Variant
|
|
67
|
+
*/
|
|
68
|
+
export interface Variant {
|
|
69
|
+
/**
|
|
70
|
+
* The name of the variant.
|
|
71
|
+
*/
|
|
72
|
+
name: string;
|
|
73
|
+
/**
|
|
74
|
+
* The fully qualified URL where the variant is being applied, with query parameters already set.
|
|
75
|
+
*/
|
|
76
|
+
url: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Represents an experiment with all its configurations.
|
|
80
|
+
*
|
|
81
|
+
* @interface Experiment
|
|
82
|
+
*/
|
|
83
|
+
export interface Experiment {
|
|
84
|
+
/**
|
|
85
|
+
* The unique identifier for the experiment.
|
|
86
|
+
*/
|
|
87
|
+
id: string;
|
|
88
|
+
/**
|
|
89
|
+
* The lookback window object for the experiment.
|
|
90
|
+
*/
|
|
91
|
+
lookBackWindow: LookBackWindow;
|
|
92
|
+
/**
|
|
93
|
+
* The name of the experiment.
|
|
94
|
+
*/
|
|
95
|
+
name: string;
|
|
96
|
+
/**
|
|
97
|
+
* The URL of the page where the experiment is applied.
|
|
98
|
+
*/
|
|
99
|
+
pageUrl: string;
|
|
100
|
+
/**
|
|
101
|
+
* The object containing regular expressions for validating pages.
|
|
102
|
+
*/
|
|
103
|
+
regexs: Regexs;
|
|
104
|
+
/**
|
|
105
|
+
* The unique running identifier for the experiment.
|
|
106
|
+
*/
|
|
107
|
+
runningId: string;
|
|
108
|
+
/**
|
|
109
|
+
* The variant applied to the user making the request.
|
|
110
|
+
*/
|
|
111
|
+
variant: Variant;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Represents the experiments assigned and their details.
|
|
115
|
+
*
|
|
116
|
+
* @interface AssignedExperiments
|
|
117
|
+
*/
|
|
118
|
+
export interface AssignedExperiments {
|
|
119
|
+
/**
|
|
120
|
+
* The ids of the experiments that are excluded in the assignment.
|
|
121
|
+
*/
|
|
122
|
+
excludedExperimentIds: string[];
|
|
123
|
+
/**
|
|
124
|
+
* An array representing the assigned experiments.
|
|
125
|
+
*/
|
|
126
|
+
experiments: Experiment[];
|
|
127
|
+
/**
|
|
128
|
+
* The ids of the experiments included in the assignment.
|
|
129
|
+
*/
|
|
130
|
+
includedExperimentIds: string[];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Represents the response from backend holding information about running experiments and variant assignment.
|
|
134
|
+
*
|
|
135
|
+
* @interface IsUserIncludedApiResponse
|
|
136
|
+
*/
|
|
137
|
+
export interface IsUserIncludedApiResponse {
|
|
138
|
+
/**
|
|
139
|
+
* The object holding all experiment-related information.
|
|
140
|
+
*/
|
|
141
|
+
entity: AssignedExperiments;
|
|
142
|
+
/**
|
|
143
|
+
* An array holding possible error messages.
|
|
144
|
+
*/
|
|
145
|
+
errors: string[];
|
|
146
|
+
/**
|
|
147
|
+
* A map that holds internationalization (i18n) messages.
|
|
148
|
+
*/
|
|
149
|
+
i18nMessagesMap: Record<string, string>;
|
|
150
|
+
/**
|
|
151
|
+
* An array of generic messages.
|
|
152
|
+
*/
|
|
153
|
+
messages: string[];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Represents a single experiment event.
|
|
157
|
+
*
|
|
158
|
+
* @interface ExperimentEvent
|
|
159
|
+
*/
|
|
160
|
+
export interface ExperimentEvent {
|
|
161
|
+
/**
|
|
162
|
+
* The name or identifier of the experiment.
|
|
163
|
+
*/
|
|
164
|
+
experiment: string;
|
|
165
|
+
/**
|
|
166
|
+
* The unique running identifier of the experiment.
|
|
167
|
+
*/
|
|
168
|
+
runningId: string;
|
|
169
|
+
/**
|
|
170
|
+
* A flag that determines if the current page is the one where the experiment is conducted.
|
|
171
|
+
*/
|
|
172
|
+
isExperimentPage: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* A flag that determines if the current page is the target page of the experiment.
|
|
175
|
+
*/
|
|
176
|
+
isTargetPage: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Represents the time period for which the experiment is valid or should be considered.
|
|
179
|
+
*/
|
|
180
|
+
lookBackWindow: string;
|
|
181
|
+
/**
|
|
182
|
+
* Represents the variant of the experiment to specify different versions of an experiment.
|
|
183
|
+
*/
|
|
184
|
+
variant: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Represents parsed data of an experiment that is ready to be sent to Analytics.
|
|
188
|
+
*
|
|
189
|
+
* @interface ExperimentParsed
|
|
190
|
+
*/
|
|
191
|
+
export interface ExperimentParsed {
|
|
192
|
+
/**
|
|
193
|
+
* The URL of the experiment. It helps track the location or origin of the experiment.
|
|
194
|
+
*/
|
|
195
|
+
href: string;
|
|
196
|
+
/**
|
|
197
|
+
* An array holding details of individual experiments being conducted.
|
|
198
|
+
*/
|
|
199
|
+
experiments: ExperimentEvent[];
|
|
200
|
+
}
|
|
201
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Experiment, ExperimentParsed } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* This arrow function parses a given set of assigned experiments for analytics.
|
|
4
|
+
*
|
|
5
|
+
* This process involves iterating over the experiments, which are currently in the "Running" state as received from the DotCMS endpoint,
|
|
6
|
+
* analyzing each experiment's relevant data such as running ID, variant name, and look back window value.
|
|
7
|
+
* It also performs regular expression verification for both 'isExperimentPage' and 'isTargetPage' against the current URL.
|
|
8
|
+
*
|
|
9
|
+
* The parsed data is useful for tracking and understanding the user's interaction with the experiment-targeted components during their visit.
|
|
10
|
+
*
|
|
11
|
+
* Contains an object with experiments information.
|
|
12
|
+
*
|
|
13
|
+
* @param experiments
|
|
14
|
+
* @param {Location} location - This parameter is the object representing the current location (URL) of the user.
|
|
15
|
+
* Mostly employed for matching the regular expressions to detect whether the current page is an 'ExperimentPage' or a 'TargetPage'.
|
|
16
|
+
*
|
|
17
|
+
* @returns {ExperimentParsed} - The function returns an object with the original URL and an array of each experiment's comprehensive detail.
|
|
18
|
+
* The return object is suitable for further analytical operations. Each experiment's detail includes the experiment ID, running ID, variant name,
|
|
19
|
+
* look back window value, and booleans that represent whether current URL is 'isExperimentPage' or 'isTargetPage' for the respective experiment.
|
|
20
|
+
*/
|
|
21
|
+
export declare const parseDataForAnalytics: (experiments: Experiment[], location: Location) => ExperimentParsed;
|
|
22
|
+
/**
|
|
23
|
+
* This utility function performs regular expression (regex) matching on a supplied URL.
|
|
24
|
+
*
|
|
25
|
+
* @param {string | null} regexToCheck - The regular expression to match against the URL.
|
|
26
|
+
* @param {string} href - This is the target URL, which is aimed to be matched against the provided regular expression.
|
|
27
|
+
* @returns {boolean} -The function returns a Boolean value.
|
|
28
|
+
*/
|
|
29
|
+
export declare const verifyRegex: (regexToCheck: string | null, href: string) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* This function merges newly fetched data with the data stored from IndexedDB, preparing it for re-storage in IndexedDB.
|
|
32
|
+
*
|
|
33
|
+
* @param { AssignedExperiments | null } fetchExperiments - The experiment data fetched from the API.
|
|
34
|
+
* @param { AssignedExperiments | null } storedExperiments - The experiment data currently stored in IndexedDB.
|
|
35
|
+
*
|
|
36
|
+
* @returns { AssignedExperiments } - The parsed experiment data ready for storing.
|
|
37
|
+
*
|
|
38
|
+
* Following cases are handled -
|
|
39
|
+
* 1) When new Data is received without Old data. This is assumed to be the first time data is received.
|
|
40
|
+
* 2) When only old data is received, implying that the timestamp hasn't expired and the data is available in IndexedDB.
|
|
41
|
+
* The data is verified and any expired experiment is removed before being assigned to dataToStorage.
|
|
42
|
+
* 3) When both old data and new data is present. This implies that the record existed in IndexedDB but was fetched because the flag had expired.
|
|
43
|
+
* Additional operations are performed to add expiry time to experiments, merging all experiments, and storing them.
|
|
44
|
+
*
|
|
45
|
+
* There could be scenarios where none of these conditions are met, in that case, dataToStorage will be the default empty object.
|
|
46
|
+
*/
|
|
47
|
+
export declare const parseData: (fetchExperiments: Experiment[] | undefined, storedExperiments: Experiment[] | undefined) => Experiment[];
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the array of experiment IDs from the given AssignedExperiments..
|
|
50
|
+
*
|
|
51
|
+
* @returns {string[]} Returns an array of experiment IDs if available, otherwise an empty array.
|
|
52
|
+
* @param experiments
|
|
53
|
+
*/
|
|
54
|
+
export declare const getExperimentsIds: (experiments: Experiment[]) => string[];
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the configuration for a database connection.
|
|
3
|
+
* @interface
|
|
4
|
+
*/
|
|
5
|
+
interface DbConfig {
|
|
6
|
+
db_name: string;
|
|
7
|
+
db_store: string;
|
|
8
|
+
db_key_path: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The `DatabaseHandler` class offers specific methods to store and get data
|
|
12
|
+
* from IndexedDB.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // To fetch data from the IndexedDB
|
|
16
|
+
* const data = await DatabaseHandler.getData();
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // To store an object of type AssignedExperiments to IndexedDB
|
|
20
|
+
* await DatabaseHandler.persistData(anAssignedExperiment);
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // To get an object of type AssignedExperiments to IndexedDB
|
|
24
|
+
* await DatabaseHandler.persistData(anAssignedExperiment);
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
export declare class IndexDBDatabaseHandler {
|
|
28
|
+
private config;
|
|
29
|
+
constructor(config: DbConfig);
|
|
30
|
+
/**
|
|
31
|
+
* Saves the provided data to indexDB.
|
|
32
|
+
*
|
|
33
|
+
* @async
|
|
34
|
+
* @param {AssignedExperiments} data - The data to be saved.
|
|
35
|
+
* @returns {Promise<any>} - The result of the save operation.
|
|
36
|
+
*/
|
|
37
|
+
persistData<T>(data: T): Promise<IDBValidKey>;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves data from the database using a specific key.
|
|
40
|
+
*
|
|
41
|
+
* @async
|
|
42
|
+
* @returns {Promise<any>} A promise that resolves with the data retrieved from the database.
|
|
43
|
+
*/
|
|
44
|
+
getData<T>(): Promise<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Deletes all the data from the IndexedDB store.
|
|
47
|
+
*
|
|
48
|
+
* @async
|
|
49
|
+
* @returns {Promise<void>} - The result of the delete operation.
|
|
50
|
+
*/
|
|
51
|
+
clearData(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the flag indicating that the experiment has already been checked.
|
|
54
|
+
*
|
|
55
|
+
* @function setFlagExperimentAlreadyChecked
|
|
56
|
+
* @returns {void}
|
|
57
|
+
*/
|
|
58
|
+
setFlagExperimentAlreadyChecked(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Sets the fetch expired time in the local storage.
|
|
61
|
+
*
|
|
62
|
+
* @return {void}
|
|
63
|
+
*/
|
|
64
|
+
setFetchExpiredTime(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Builds an error message based on the provided error object.
|
|
67
|
+
* @param {DOMException | null} error - The error object to build the message from.
|
|
68
|
+
* @returns {string} The constructed error message.
|
|
69
|
+
*/
|
|
70
|
+
private getOnErrorMessage;
|
|
71
|
+
/**
|
|
72
|
+
* Creates or opens a IndexedDB database with the specified version.
|
|
73
|
+
*
|
|
74
|
+
*
|
|
75
|
+
* @returns {Promise<IDBDatabase>} A promise that resolves to the opened database.
|
|
76
|
+
* The promise will be rejected with an error message if there was a database error.
|
|
77
|
+
*/
|
|
78
|
+
private openDB;
|
|
79
|
+
/**
|
|
80
|
+
* Retrieves the result of a database request from an Event object.
|
|
81
|
+
*
|
|
82
|
+
* @param {Event} event - The Event object containing the database request.
|
|
83
|
+
* @returns {IDBDatabase} - The result of the database request, casted as an IDBDatabase object.
|
|
84
|
+
*/
|
|
85
|
+
private getRequestResult;
|
|
86
|
+
}
|
|
87
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger for the dotCMS SDK
|
|
3
|
+
*/
|
|
4
|
+
export declare class DotLogger {
|
|
5
|
+
private readonly isDebug;
|
|
6
|
+
private readonly packageName;
|
|
7
|
+
constructor(isDebug: boolean, packageName: string);
|
|
8
|
+
group(label: string): void;
|
|
9
|
+
groupEnd(): void;
|
|
10
|
+
time(label: string): void;
|
|
11
|
+
timeEnd(label: string): void;
|
|
12
|
+
log(message: string): void;
|
|
13
|
+
warn(message: string): void;
|
|
14
|
+
error(message: string): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { DotExperimentConfig, Experiment, Variant } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first script element that includes the experiment script identifier.
|
|
4
|
+
*
|
|
5
|
+
* @return {HTMLScriptElement|undefined} - The found script element or undefined if none is found.
|
|
6
|
+
*/
|
|
7
|
+
export declare const getExperimentScriptTag: () => HTMLScriptElement;
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves experiment attributes from a given script element.
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
* @return {DotExperimentConfig | null} - The experiment attributes or null if there are no valid attributes present.
|
|
13
|
+
*/
|
|
14
|
+
export declare const getDataExperimentAttributes: (location: Location) => DotExperimentConfig | null;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves the data attributes from the experiment script tag.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* Given the custom script tag in your HTML:
|
|
20
|
+
* <script src="/dot-experiments.iife.js"
|
|
21
|
+
* defer=""
|
|
22
|
+
* data-experiment-api-key="api-token"
|
|
23
|
+
* data-experiment-server="http://localhost:8080/"
|
|
24
|
+
* data-experiment-debug>
|
|
25
|
+
* </script>
|
|
26
|
+
*
|
|
27
|
+
* @returns {DotExperimentConfig | null} The data attributes of the experiment script tag, or null if no experiment script is found.
|
|
28
|
+
*/
|
|
29
|
+
export declare const getScriptDataAttributes: (location: Location) => DotExperimentConfig | null;
|
|
30
|
+
/**
|
|
31
|
+
* Checks the flag indicating whether the experiment has already been checked.
|
|
32
|
+
*
|
|
33
|
+
* @function checkFlagExperimentAlreadyChecked
|
|
34
|
+
* @returns {boolean} - returns true if experiment has already been checked, otherwise false.
|
|
35
|
+
*/
|
|
36
|
+
export declare const checkFlagExperimentAlreadyChecked: () => boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if the data needs to be invalidated based on the creation date.
|
|
39
|
+
*
|
|
40
|
+
* @returns {boolean} - True if the data needs to be invalidated, false otherwise.
|
|
41
|
+
*/
|
|
42
|
+
export declare const isDataCreateValid: () => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Ad to an absolute path the baseUrl depending on the location.
|
|
45
|
+
*
|
|
46
|
+
* @param {string | null} absolutePath - The absolute path of the URL.
|
|
47
|
+
* @param {Location} location - The location object representing the current URL.
|
|
48
|
+
* @returns {string | null} - The full URL or null if absolutePath is null.
|
|
49
|
+
*/
|
|
50
|
+
export declare const getFullUrl: (location: Location, absolutePath: string | null) => string | null;
|
|
51
|
+
/**
|
|
52
|
+
* Updates the URL with the queryParam with the experiment variant name.
|
|
53
|
+
*
|
|
54
|
+
* @param {Location|string} location - The current location object or the URL string.
|
|
55
|
+
* @param {Variant} variant - The experiment variant to update the URL with.
|
|
56
|
+
* @returns {string} The updated URL string.
|
|
57
|
+
*/
|
|
58
|
+
export declare const updateUrlWithExperimentVariant: (location: Location | string, variant: Variant | null) => string;
|
|
59
|
+
/**
|
|
60
|
+
* Check if two arrays of Experiment objects are equal.
|
|
61
|
+
*
|
|
62
|
+
* @param {Experiment[]} obj1 - The first array of Experiment objects.
|
|
63
|
+
* @param {Experiment[]} obj2 - The second array of Experiment objects.
|
|
64
|
+
* @return {boolean} - True if the arrays are equal, false otherwise.
|
|
65
|
+
*/
|
|
66
|
+
export declare const objectsAreEqual: (obj1: Experiment[], obj2: Experiment[]) => boolean;
|
|
67
|
+
/**
|
|
68
|
+
* A function to redirect the user to a new URL.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} href - The URL to redirect to.
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
export declare const defaultRedirectFn: (href: string) => string;
|