@contentful/optimization-web 0.1.0-alpha → 0.1.0-alpha5

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 CHANGED
@@ -28,6 +28,7 @@ This SDK implements functionality specific to the Web environment, based on the
28
28
  <!-- mtoc-start -->
29
29
 
30
30
  - [Getting Started](#getting-started)
31
+ - [Usage in Vanilla JS Web Pages](#usage-in-vanilla-js-web-pages)
31
32
  - [Reference Implementations](#reference-implementations)
32
33
  - [Configuration](#configuration)
33
34
  - [Top-level Configuration Options](#top-level-configuration-options)
@@ -79,12 +80,25 @@ Import the Optimization class; both CJS and ESM module systems are supported, ES
79
80
  import Optimization from '@contentful/optimization-web'
80
81
  ```
81
82
 
82
- Configure and initialize the Optimization Node SDK:
83
+ Configure and initialize the Optimization Web SDK:
83
84
 
84
85
  ```ts
85
86
  const optimization = new Optimization({ clientId: 'abc123' })
86
87
  ```
87
88
 
89
+ ### Usage in Vanilla JS Web Pages
90
+
91
+ Alternatively, the Web SDK can be used directly within an HTML file:
92
+
93
+ ```html
94
+ <script src="https://cdn.jsdelivr.net/npm/@contentful/optimization-web@latest/dist/contentful-optimization-web.umd.js"></script>
95
+ <script>
96
+ new Optimization({ clientId: 'abc123' })
97
+ // is equal to:
98
+ // window.optimization = new Optimization({ clientId: 'abc123' })
99
+ </script>
100
+ ```
101
+
88
102
  ## Reference Implementations
89
103
 
90
104
  - [Web Vanilla](/implementations/web-vanilla/README.md): Example static Web page that renders and
@@ -134,7 +148,7 @@ descendent SDKs.
134
148
  | ------------------- | --------- | ---------------------------------------------------- | ---------------------------------------------------------------------------------- |
135
149
  | `app` | No | `undefined` | The application definition used to attribute events to a specific consumer app |
136
150
  | `channel` | No | `'web'` | The channel that identifies where events originate from (e.g. `'web'`, `'mobile'`) |
137
- | `library` | No | `{ name: 'Optimization Web API', version: '0.0.0' }` | The client library metadata that is attached to all events |
151
+ | `library` | No | `{ name: 'Optimization Web SDK', version: '0.0.0' }` | The client library metadata that is attached to all events |
138
152
  | `getLocale` | No | Built-in locale resolution | Function used to resolve the locale for outgoing events |
139
153
  | `getPageProperties` | No | Built-in page properties resolution | Function that returns the current page properties |
140
154
  | `getUserAgent` | No | Built-in user agent resolution | Function used to obtain the current user agent string when applicable |
@@ -8,6 +8,8 @@ export type CtflDataset = DOMStringMap & {
8
8
  ctflEntryId: string;
9
9
  /** Optional duplication scope key for de-duplication across views. */
10
10
  ctflDuplicationScope?: string;
11
+ /** Optional baseline ID associated with the personalized entry. */
12
+ ctflBaselineId?: string;
11
13
  /** Optional personalization/experience ID associated with the entry. */
12
14
  ctflPersonalizationId?: string;
13
15
  /** Whether this component view should be treated as sticky. */
@@ -1 +1 @@
1
- {"version":3,"file":"AutoEntryViewTracking.d.ts","sourceRoot":"","sources":["../src/AutoEntryViewTracking.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAsB,MAAM,+BAA+B,CAAA;AACrF,OAAO,KAAK,EACV,+BAA+B,EAC/B,uBAAuB,EACvB,mBAAmB,EACpB,MAAM,aAAa,CAAA;AAIpB;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG;IACvC,4CAA4C;IAC5C,WAAW,EAAE,MAAM,CAAA;IACnB,sEAAsE;IACtE,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAC7B,wEAAwE;IACxE,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,+DAA+D;IAC/D,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IAC7B,+EAA+E;IAC/E,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B,CAAA;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,WAAW,GAAG,UAAU,CAAC,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAA;AAEhF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,IAAI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7B,+EAA+E;IAC/E,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B,CAAA;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,WAAW,GAAG,UAAU,CAAC,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAA;AAEhF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,IAAI,YAAY,CAgBzE;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,0DAA0D;IAC1D,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,8CAA8C;IAC9C,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kCAAkC;IAClC,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,IAAI,SAAS,CAI7D;AAwBD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mCAAmC,GAC7C,MAAM,YAAY,MACZ,SAAS,OAAO,EAAE,MAAM,uBAAuB,KAAG,OAAO,CAAC,IAAI,CA8CpE,CAAA;AAgBH;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,wCAAwC,GACnD,mBAAmB,mBAAmB,EACtC,kCAAgC,KAC/B,+BAuBD,CAAA"}
@@ -1,3 +1,13 @@
1
+ /**
2
+ * Web SDK entrypoint for Contentful Optimization.
3
+ *
4
+ * @packageDocumentation
5
+ * @remarks
6
+ * Exposes a browser-wired {@link Optimization} class built on top of {@link CoreStateful}.
7
+ * When executed in a browser environment, the constructor attaches a singleton instance
8
+ * to `window.optimization` and the class constructor to `window.Optimization` for
9
+ * script-tag / global usage.
10
+ */
1
11
  import { type App, CoreStateful, type CoreStatefulConfig } from '@contentful/optimization-core';
2
12
  import { type ElementViewElementOptions, type ElementViewObserverOptions } from './observers';
3
13
  declare global {
@@ -8,7 +18,20 @@ declare global {
8
18
  optimization?: Optimization;
9
19
  }
10
20
  }
21
+ /**
22
+ * Supported cookie attributes for the Web SDK.
23
+ *
24
+ * @public
25
+ * @remarks
26
+ * These options are passed to {@link Cookies.set} when persisting the anonymous ID.
27
+ */
11
28
  interface CookieAttributes {
29
+ /**
30
+ * Cookie domain attribute.
31
+ *
32
+ * @remarks
33
+ * If omitted, the browser will scope the cookie to the current host.
34
+ */
12
35
  domain?: string;
13
36
  /**
14
37
  * Determines the expiration date of the cookie as the number of days until the cookie expires.
@@ -35,6 +58,12 @@ export interface OptimizationWebConfig extends CoreStatefulConfig {
35
58
  * @defaultValue `false`
36
59
  */
37
60
  autoTrackEntryViews?: boolean;
61
+ /**
62
+ * Cookie configuration used for persisting the anonymous identifier.
63
+ *
64
+ * @remarks
65
+ * Use this to control the cookie domain and expiration.
66
+ */
38
67
  cookie?: CookieAttributes;
39
68
  }
40
69
  /**
@@ -53,9 +82,29 @@ export interface OptimizationWebConfig extends CoreStatefulConfig {
53
82
  * in a browser environment.
54
83
  */
55
84
  declare class Optimization extends CoreStateful {
85
+ /**
86
+ * Observer responsible for element view/dwell-time tracking.
87
+ *
88
+ * @internal
89
+ */
56
90
  private elementViewObserver?;
91
+ /**
92
+ * Observer responsible for detecting entry elements added/removed in the DOM.
93
+ *
94
+ * @internal
95
+ */
57
96
  private elementExistenceObserver?;
97
+ /**
98
+ * Whether automatic entry view tracking is enabled.
99
+ *
100
+ * @internal
101
+ */
58
102
  private autoTrackEntryViews;
103
+ /**
104
+ * Cookie attributes used when persisting the anonymous identifier.
105
+ *
106
+ * @internal
107
+ */
59
108
  private readonly cookieAttributes?;
60
109
  /**
61
110
  * Create a new Optimization Web SDK instance.
@@ -77,6 +126,23 @@ declare class Optimization extends CoreStateful {
77
126
  * ```
78
127
  */
79
128
  constructor(config: OptimizationWebConfig);
129
+ /**
130
+ * Initialize anonymous ID state from cookies.
131
+ *
132
+ * @internal
133
+ * @remarks
134
+ * Reads the legacy anonymous ID cookie (if present), migrates to the current cookie,
135
+ * and ensures SDK state is reset when the persisted anonymous ID differs from the
136
+ * in-memory value.
137
+ */
138
+ private initializeFromCookieValues;
139
+ /**
140
+ * Persist (or clear) the anonymous ID in both cookies and {@link LocalStore}.
141
+ *
142
+ * @param value - Anonymous identifier to persist. If omitted, clears persisted state.
143
+ *
144
+ * @internal
145
+ */
80
146
  private setAnonymousId;
81
147
  /**
82
148
  * Enable automatic entry view tracking for elements with `data-ctfl-*`
@@ -107,6 +173,10 @@ declare class Optimization extends CoreStateful {
107
173
  * @param element - Element to observe.
108
174
  * @param options - Per-element observer options and callback data.
109
175
  *
176
+ * @remarks
177
+ * This method relies on an initialized {@link ElementViewObserver}. If automatic
178
+ * tracking has not been started, the call is a no-op.
179
+ *
110
180
  * @example
111
181
  * ```ts
112
182
  * const element = document.querySelector('#hero')!
@@ -122,6 +192,10 @@ declare class Optimization extends CoreStateful {
122
192
  *
123
193
  * @param element - Element to stop observing.
124
194
  *
195
+ * @remarks
196
+ * This method relies on an initialized {@link ElementViewObserver}. If automatic
197
+ * tracking has not been started, the call is a no-op.
198
+ *
125
199
  * @example
126
200
  * ```ts
127
201
  * optimization.untrackEntryViewForElement(element)
@@ -1 +1 @@
1
- {"version":3,"file":"Optimization.d.ts","sourceRoot":"","sources":["../src/Optimization.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,GAAG,EACR,YAAY,EACZ,KAAK,kBAAkB,EAIxB,MAAM,+BAA+B,CAAA;AAetC,OAAO,EAEL,KAAK,yBAAyB,EAE9B,KAAK,0BAA0B,EAChC,MAAM,aAAa,CAAA;AAKpB,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,qEAAqE;QACrE,YAAY,CAAC,EAAE,OAAO,YAAY,CAAA;QAClC,6DAA6D;QAC7D,YAAY,CAAC,EAAE,YAAY,CAAA;KAC5B;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