@liquidcommercedev/rmn-sdk 1.5.0-beta.1 → 1.5.0-beta.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs CHANGED
@@ -18196,7 +18196,8 @@ class EventService {
18196
18196
  })();
18197
18197
  }
18198
18198
  /**
18199
- * Fires an event using the navigator.sendBeacon method and redirects the user if the event is a click event.
18199
+ * Fires an event using the navigator.sendBeacon method or a fallback method if sendBeacon is not available.
18200
+ * If the event is a click event and a redirect URL is found, it redirects the user to that URL.
18200
18201
  *
18201
18202
  * @param {IFireEventParams} params - The parameters for firing the event.
18202
18203
  * @param {RMN_SPOT_EVENT} params.event - The event type.
@@ -18204,26 +18205,65 @@ class EventService {
18204
18205
  * @returns {Promise<void>} - A promise that resolves when the event is fired.
18205
18206
  */
18206
18207
  async fireEvent({ event, eventUrl }) {
18207
- const didFireEvent = navigator.sendBeacon(eventUrl);
18208
- if (didFireEvent && event === exports.RMN_SPOT_EVENT.CLICK) {
18209
- window.location.href = this.getRedirectUrlFromPayload(eventUrl);
18208
+ var _a;
18209
+ try {
18210
+ const didFireEvent = ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.sendBeacon) === null || _a === void 0 ? void 0 : _a.call(navigator, eventUrl)) || (await this.fallbackEventFire(eventUrl));
18211
+ if (!didFireEvent) {
18212
+ return;
18213
+ }
18214
+ if (event === exports.RMN_SPOT_EVENT.CLICK) {
18215
+ const redirectUrl = this.getRedirectUrlFromPayload(eventUrl);
18216
+ if (redirectUrl) {
18217
+ window.location.href = redirectUrl;
18218
+ }
18219
+ }
18220
+ }
18221
+ catch (_b) {
18222
+ // Handle errors silently
18223
+ }
18224
+ }
18225
+ // Fallback method using fetch if sendBeacon isn't available
18226
+ async fallbackEventFire(url) {
18227
+ try {
18228
+ const racePromise = Promise.race([
18229
+ // Promise #1: The fetch request
18230
+ fetch(url, {
18231
+ method: 'POST',
18232
+ keepalive: true,
18233
+ }),
18234
+ // Promise #2: The timeout
18235
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Request timeout')), 2000)),
18236
+ ]);
18237
+ /**
18238
+ * Prevent requests from hanging indefinitely
18239
+ * Improve user experience by failing fast
18240
+ * Handle slow network conditions gracefully
18241
+ * Ensure resources are freed up in a timely manner
18242
+ */
18243
+ const response = await racePromise;
18244
+ return response.ok;
18245
+ }
18246
+ catch (_a) {
18247
+ return false;
18210
18248
  }
18211
18249
  }
18212
18250
  /**
18213
18251
  * Extracts and decodes a URL from a base64-encoded query parameter.
18214
18252
  *
18215
18253
  * @param {string} url - The URL containing the base64-encoded query parameter.
18216
- * @returns {string} - The decoded URL or an empty string if decoding fails.
18254
+ * @returns {string | null} - The decoded URL or null if not found or invalid.
18217
18255
  */
18218
18256
  getRedirectUrlFromPayload(url) {
18219
- var _a, _b;
18220
- const base64String = (_a = new URL(url).searchParams.get('e')) !== null && _a !== void 0 ? _a : '';
18221
18257
  try {
18258
+ const base64String = new URL(url).searchParams.get('e');
18259
+ if (!base64String) {
18260
+ return null;
18261
+ }
18222
18262
  const data = JSON.parse(atob(base64String));
18223
- return (_b = data.ur) !== null && _b !== void 0 ? _b : '';
18263
+ return data.ur || null;
18224
18264
  }
18225
- catch (_c) {
18226
- return '';
18265
+ catch (_a) {
18266
+ return null;
18227
18267
  }
18228
18268
  }
18229
18269
  }
package/dist/index.esm.js CHANGED
@@ -18194,7 +18194,8 @@ class EventService {
18194
18194
  })();
18195
18195
  }
18196
18196
  /**
18197
- * Fires an event using the navigator.sendBeacon method and redirects the user if the event is a click event.
18197
+ * Fires an event using the navigator.sendBeacon method or a fallback method if sendBeacon is not available.
18198
+ * If the event is a click event and a redirect URL is found, it redirects the user to that URL.
18198
18199
  *
18199
18200
  * @param {IFireEventParams} params - The parameters for firing the event.
18200
18201
  * @param {RMN_SPOT_EVENT} params.event - The event type.
@@ -18202,26 +18203,65 @@ class EventService {
18202
18203
  * @returns {Promise<void>} - A promise that resolves when the event is fired.
18203
18204
  */
18204
18205
  async fireEvent({ event, eventUrl }) {
18205
- const didFireEvent = navigator.sendBeacon(eventUrl);
18206
- if (didFireEvent && event === RMN_SPOT_EVENT.CLICK) {
18207
- window.location.href = this.getRedirectUrlFromPayload(eventUrl);
18206
+ var _a;
18207
+ try {
18208
+ const didFireEvent = ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.sendBeacon) === null || _a === void 0 ? void 0 : _a.call(navigator, eventUrl)) || (await this.fallbackEventFire(eventUrl));
18209
+ if (!didFireEvent) {
18210
+ return;
18211
+ }
18212
+ if (event === RMN_SPOT_EVENT.CLICK) {
18213
+ const redirectUrl = this.getRedirectUrlFromPayload(eventUrl);
18214
+ if (redirectUrl) {
18215
+ window.location.href = redirectUrl;
18216
+ }
18217
+ }
18218
+ }
18219
+ catch (_b) {
18220
+ // Handle errors silently
18221
+ }
18222
+ }
18223
+ // Fallback method using fetch if sendBeacon isn't available
18224
+ async fallbackEventFire(url) {
18225
+ try {
18226
+ const racePromise = Promise.race([
18227
+ // Promise #1: The fetch request
18228
+ fetch(url, {
18229
+ method: 'POST',
18230
+ keepalive: true,
18231
+ }),
18232
+ // Promise #2: The timeout
18233
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Request timeout')), 2000)),
18234
+ ]);
18235
+ /**
18236
+ * Prevent requests from hanging indefinitely
18237
+ * Improve user experience by failing fast
18238
+ * Handle slow network conditions gracefully
18239
+ * Ensure resources are freed up in a timely manner
18240
+ */
18241
+ const response = await racePromise;
18242
+ return response.ok;
18243
+ }
18244
+ catch (_a) {
18245
+ return false;
18208
18246
  }
18209
18247
  }
18210
18248
  /**
18211
18249
  * Extracts and decodes a URL from a base64-encoded query parameter.
18212
18250
  *
18213
18251
  * @param {string} url - The URL containing the base64-encoded query parameter.
18214
- * @returns {string} - The decoded URL or an empty string if decoding fails.
18252
+ * @returns {string | null} - The decoded URL or null if not found or invalid.
18215
18253
  */
18216
18254
  getRedirectUrlFromPayload(url) {
18217
- var _a, _b;
18218
- const base64String = (_a = new URL(url).searchParams.get('e')) !== null && _a !== void 0 ? _a : '';
18219
18255
  try {
18256
+ const base64String = new URL(url).searchParams.get('e');
18257
+ if (!base64String) {
18258
+ return null;
18259
+ }
18220
18260
  const data = JSON.parse(atob(base64String));
18221
- return (_b = data.ur) !== null && _b !== void 0 ? _b : '';
18261
+ return data.ur || null;
18222
18262
  }
18223
- catch (_c) {
18224
- return '';
18263
+ catch (_a) {
18264
+ return null;
18225
18265
  }
18226
18266
  }
18227
18267
  }
@@ -27,7 +27,8 @@ export declare class EventService {
27
27
  private handleIntersectionObserver;
28
28
  private fireImpressionEvent;
29
29
  /**
30
- * Fires an event using the navigator.sendBeacon method and redirects the user if the event is a click event.
30
+ * Fires an event using the navigator.sendBeacon method or a fallback method if sendBeacon is not available.
31
+ * If the event is a click event and a redirect URL is found, it redirects the user to that URL.
31
32
  *
32
33
  * @param {IFireEventParams} params - The parameters for firing the event.
33
34
  * @param {RMN_SPOT_EVENT} params.event - The event type.
@@ -35,11 +36,12 @@ export declare class EventService {
35
36
  * @returns {Promise<void>} - A promise that resolves when the event is fired.
36
37
  */
37
38
  private fireEvent;
39
+ private fallbackEventFire;
38
40
  /**
39
41
  * Extracts and decodes a URL from a base64-encoded query parameter.
40
42
  *
41
43
  * @param {string} url - The URL containing the base64-encoded query parameter.
42
- * @returns {string} - The decoded URL or an empty string if decoding fails.
44
+ * @returns {string | null} - The decoded URL or null if not found or invalid.
43
45
  */
44
46
  private getRedirectUrlFromPayload;
45
47
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@liquidcommercedev/rmn-sdk",
3
3
  "description": "LiquidCommerce RMN SDK",
4
4
  "author": "LiquidCommerce Tech",
5
- "version": "1.5.0-beta.1",
5
+ "version": "1.5.0-beta.2",
6
6
  "homepage": "https://docs.liquidcommerce.co/rmn-sdk",
7
7
  "main": "./dist/index.cjs",
8
8
  "module": "./dist/index.esm.js",