@liquidcommercedev/rmn-sdk 1.5.0-beta.26 → 1.5.0-beta.27

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/dist/index.cjs CHANGED
@@ -6133,121 +6133,6 @@ function getEventTypeFromRawEvent(event) {
6133
6133
  }
6134
6134
 
6135
6135
  // Configuration object with target field names
6136
- const extractorConfig = {
6137
- ids: [
6138
- // Ps: The function handles all the variations of keywords that end with "id" or "ids"
6139
- // Universal product identifiers
6140
- 'gtin',
6141
- 'gtin8',
6142
- 'gtin12',
6143
- 'gtin13',
6144
- 'gtin14',
6145
- 'mpn',
6146
- 'sku',
6147
- 'upc',
6148
- 'ean',
6149
- 'isbn',
6150
- 'isbn10',
6151
- 'isbn13',
6152
- 'asin',
6153
- // Product codes and references
6154
- 'coupon',
6155
- 'barcode',
6156
- 'product_code',
6157
- 'part_number',
6158
- 'model_number',
6159
- 'item_variant',
6160
- 'item_number',
6161
- 'article_number',
6162
- 'reference',
6163
- 'salsifyGrouping',
6164
- 'grouping',
6165
- ],
6166
- price: [
6167
- 'price',
6168
- 'unitPrice',
6169
- 'cost',
6170
- 'current_price',
6171
- 'sale_price',
6172
- 'price_value',
6173
- 'sale_price_value',
6174
- 'regular_price',
6175
- 'discount_price',
6176
- 'unit_price',
6177
- 'original_price',
6178
- 'final_price',
6179
- 'retail_price',
6180
- ],
6181
- quantity: ['quantity', 'qty'],
6182
- };
6183
- /**
6184
- * Extracts deep values from an object based on specified target type
6185
- * @param data - The source data object to extract values from
6186
- * @param target - The type of values to extract ('ids' or 'price')
6187
- * @param options - Optional configuration for the extraction process
6188
- * @returns Array of extracted values or a single value if onlyFirst is true
6189
- */
6190
- function extractDeepValues(data, target, options = {}) {
6191
- const {
6192
- // eslint-disable-next-line @typescript-eslint/naming-convention
6193
- onlyFirst = false, shouldIncludeZero = false, } = options;
6194
- const values = [];
6195
- const targetProperties = new Set(extractorConfig[target].map((name) => name.toLowerCase()));
6196
- /**
6197
- * Checks if a property name matches the target criteria
6198
- */
6199
- const isTargetField = (key) => {
6200
- const normalizedKey = key.toLowerCase();
6201
- const hasTarget = targetProperties.has(normalizedKey);
6202
- if (target === 'ids') {
6203
- return normalizedKey.endsWith('id') || normalizedKey.endsWith('ids') || hasTarget;
6204
- }
6205
- return hasTarget;
6206
- };
6207
- /**
6208
- * Validates and normalizes extracted values
6209
- */
6210
- const validateValue = (value) => {
6211
- if (typeof value === 'string') {
6212
- return value.trim().length > 0;
6213
- }
6214
- if (typeof value === 'number') {
6215
- return !isNaN(value) && (shouldIncludeZero || value !== 0);
6216
- }
6217
- return false;
6218
- };
6219
- /**
6220
- * Processes a value and extracts matching fields
6221
- */
6222
- const processValue = (value, currentKey) => {
6223
- // Early exit conditions
6224
- if (value == null || (onlyFirst && values.length > 0))
6225
- return;
6226
- // Process current value if it matches target criteria
6227
- if (currentKey && isTargetField(currentKey)) {
6228
- if (Array.isArray(value)) {
6229
- const validValues = value.filter(validateValue);
6230
- values.push(...validValues);
6231
- }
6232
- else if (validateValue(value)) {
6233
- values.push(value);
6234
- }
6235
- return;
6236
- }
6237
- // Recursive processing for nested structures
6238
- if (Array.isArray(value)) {
6239
- value.forEach((item) => processValue(item));
6240
- }
6241
- else if (typeof value === 'object') {
6242
- Object.entries(value).forEach(([key, val]) => processValue(val, key));
6243
- }
6244
- };
6245
- processValue(data);
6246
- // Return based on options
6247
- if (values.length === 0)
6248
- return undefined;
6249
- return onlyFirst ? values[0] : values;
6250
- }
6251
6136
  /**
6252
6137
  * Cleans and normalizes an array of product IDs by:
6253
6138
  * 1. Converting all IDs to strings
@@ -17200,7 +17085,8 @@ if (typeof window !== 'undefined' && typeof window.customElements !== 'undefined
17200
17085
  CarouselElement = CustomCarouselElement;
17201
17086
  }
17202
17087
 
17203
- function SkeletonTemplate({ fluid, width, height }) {
17088
+ function SkeletonTemplate({ fluid, width, height, spotType, }) {
17089
+ const isSmall = spotType === exports.RMN_SPOT_TYPE.RB_IN_TEXT;
17204
17090
  return `
17205
17091
  <style>
17206
17092
  :host {
@@ -17211,42 +17097,20 @@ function SkeletonTemplate({ fluid, width, height }) {
17211
17097
  width: ${fluid ? '100%' : `${width}px`};
17212
17098
  height: ${fluid ? '100%' : `${height}px`};
17213
17099
  background: #ffffff;
17214
- padding: 20px;
17100
+ padding: ${isSmall ? '0' : '1rem'};
17215
17101
  border-radius: 5px;
17216
17102
  }
17217
17103
 
17218
- .content {
17219
- height: 100%;
17220
- display: flex;
17221
- flex-direction: column;
17222
- gap: 20px;
17223
- }
17224
-
17225
- .image-placeholder {
17104
+ .line {
17226
17105
  width: 100%;
17227
17106
  height: 100%;
17107
+ min-height: 15px;
17228
17108
  background: #f0f0f0;
17229
- border-radius: 4px;
17230
- position: relative;
17231
- overflow: hidden;
17232
- }
17233
-
17234
- .lines-container {
17235
- display: flex;
17236
- flex-direction: column;
17237
- justify-content: flex-end;
17238
- }
17239
-
17240
- .line {
17241
- height: 20px;
17242
- background: #f0f0f0;
17243
- border-radius: 4px;
17244
- margin-bottom: 15px;
17109
+ border-radius: 5px;
17245
17110
  position: relative;
17246
17111
  overflow: hidden;
17247
17112
  }
17248
17113
 
17249
- .image-placeholder::after,
17250
17114
  .line::after {
17251
17115
  content: "";
17252
17116
  position: absolute;
@@ -17263,18 +17127,6 @@ function SkeletonTemplate({ fluid, width, height }) {
17263
17127
  animation: shimmer 1.5s infinite;
17264
17128
  }
17265
17129
 
17266
- .line.header {
17267
- width: 25%;
17268
- }
17269
-
17270
- .line.description {
17271
- width: 65%;
17272
- }
17273
-
17274
- .line.button {
17275
- width: 40%;
17276
- }
17277
-
17278
17130
  @keyframes shimmer {
17279
17131
  0% {
17280
17132
  transform: translateX(-100%);
@@ -17285,14 +17137,7 @@ function SkeletonTemplate({ fluid, width, height }) {
17285
17137
  }
17286
17138
  </style>
17287
17139
 
17288
- <div class="content">
17289
- <div class="image-placeholder"></div>
17290
- <div class="lines-container">
17291
- <div class="line header"></div>
17292
- <div class="line description"></div>
17293
- <div class="line button"></div>
17294
- </div>
17295
- </div>
17140
+ <div class="line"></div>
17296
17141
  `;
17297
17142
  }
17298
17143
 
@@ -17471,6 +17316,7 @@ class ElementService {
17471
17316
  const skeleton = document.createElement(SKELETON_ELEMENT_TAG);
17472
17317
  const dimensions = SPOT_DIMENSIONS[params.spotType];
17473
17318
  skeleton.data = {
17319
+ spotType: params.spotType,
17474
17320
  fluid: params.fluid,
17475
17321
  ...dimensions,
17476
17322
  };
@@ -19386,45 +19232,45 @@ class DataLayerMonitor {
19386
19232
  return result;
19387
19233
  }
19388
19234
  for (const pushedEvent of args) {
19389
- const normalizedData = this.cleanEventData(pushedEvent);
19390
- if (normalizedData) {
19391
- this.listener(normalizedData);
19235
+ const eventName = getEventTypeFromRawEvent(pushedEvent.event);
19236
+ if (!eventName) {
19237
+ continue;
19238
+ }
19239
+ const productData = this.extractProductData(pushedEvent);
19240
+ const eventData = {
19241
+ event: eventName,
19242
+ products: productData,
19243
+ };
19244
+ if (productData) {
19245
+ this.listener(eventData);
19392
19246
  }
19393
19247
  }
19394
19248
  return result;
19395
19249
  };
19396
19250
  }
19397
- cleanEventData(data) {
19398
- const eventName = getEventTypeFromRawEvent(data.event);
19399
- if (!eventName) {
19400
- return null;
19401
- }
19402
- const productIds = extractDeepValues(data, 'ids', {
19403
- onlyFirst: false,
19404
- shouldIncludeZero: true,
19405
- });
19406
- if (Array.isArray(productIds) && productIds.length === 0) {
19407
- return null;
19408
- }
19409
- const normalizedData = {
19410
- event: eventName,
19411
- productIds,
19412
- };
19413
- if (eventName === exports.RMN_SPOT_EVENT.PURCHASE) {
19414
- const productPrice = extractDeepValues(data, 'price', {
19415
- onlyFirst: true,
19416
- shouldIncludeZero: true,
19417
- });
19418
- const productQuantity = extractDeepValues(data, 'quantity', {
19419
- onlyFirst: true,
19420
- shouldIncludeZero: true,
19421
- });
19422
- if (productPrice) {
19423
- normalizedData.productPrice = productPrice;
19424
- normalizedData.productQuantity = productQuantity !== null && productQuantity !== void 0 ? productQuantity : 1;
19251
+ extractProductData(event) {
19252
+ var _a, _b, _c, _d, _e, _f, _g, _h;
19253
+ const items = ((_a = event === null || event === void 0 ? void 0 : event.value) === null || _a === void 0 ? void 0 : _a.items) ||
19254
+ ((_b = event === null || event === void 0 ? void 0 : event.ecommerce) === null || _b === void 0 ? void 0 : _b.items) ||
19255
+ ((_d = (_c = event === null || event === void 0 ? void 0 : event.ecommerce) === null || _c === void 0 ? void 0 : _c.detail) === null || _d === void 0 ? void 0 : _d.products) ||
19256
+ ((_f = (_e = event === null || event === void 0 ? void 0 : event.ecommerce) === null || _e === void 0 ? void 0 : _e.checkout) === null || _f === void 0 ? void 0 : _f.products) ||
19257
+ ((_h = (_g = event === null || event === void 0 ? void 0 : event.ecommerce) === null || _g === void 0 ? void 0 : _g.purchase) === null || _h === void 0 ? void 0 : _h.products) ||
19258
+ [];
19259
+ return items.map((item) => {
19260
+ const data = {
19261
+ id: item.item_id || item.id || '',
19262
+ name: item.item_name || item.name || '',
19263
+ brand: item.item_brand || item.brand || '',
19264
+ variant: item.item_variant || item.variant || '',
19265
+ price: Number(item.price) || 0,
19266
+ quantity: Number(item.quantity) || 1,
19267
+ discount: Number(item.discount) || 0,
19268
+ };
19269
+ if (!data.id) {
19270
+ return null;
19425
19271
  }
19426
- }
19427
- return normalizedData;
19272
+ return data;
19273
+ });
19428
19274
  }
19429
19275
  stop() {
19430
19276
  if (this.originalPush) {
@@ -19474,40 +19320,49 @@ class MonitorService {
19474
19320
  var _a;
19475
19321
  if (!spots)
19476
19322
  return;
19477
- const eventProductIds = new Set(cleanProductIds(eventData.productIds));
19478
19323
  for (const spot of Object.values(spots)) {
19479
19324
  if (!spot.productIds.length)
19480
19325
  continue;
19481
- const hasCommonProductIds = cleanProductIds(spot.productIds).find((productId) => eventProductIds.has(productId));
19482
- if (!hasCommonProductIds || !Object.values(exports.RMN_SPOT_EVENT).includes(eventData.event)) {
19483
- continue;
19326
+ for (const data of eventData.products) {
19327
+ if (!Object.values(exports.RMN_SPOT_EVENT).includes(eventData.event)) {
19328
+ continue;
19329
+ }
19330
+ const spotRelatedProductIdsSet = new Set(cleanProductIds(spot.productIds));
19331
+ const [eventProductId] = cleanProductIds([data.id]);
19332
+ const [eventProductBrand] = cleanProductIds([data.brand]);
19333
+ const [eventVariantBrand] = cleanProductIds([data.variant]);
19334
+ const isProductMatch = [eventProductId, eventProductBrand, eventVariantBrand].some((id) => spotRelatedProductIdsSet.has(id));
19335
+ if (!isProductMatch) {
19336
+ continue;
19337
+ }
19338
+ const eventPosition = spot.events.findIndex((event) => event.event === eventData.event);
19339
+ if (eventPosition === -1)
19340
+ continue;
19341
+ const eventUrl = spot.events[eventPosition].url;
19342
+ let additionalQueryParams = '';
19343
+ if (eventData.event === exports.RMN_SPOT_EVENT.PURCHASE) {
19344
+ const gmv = data.price && data.quantity ? data.price * data.quantity : undefined;
19345
+ // gmv = gross merchandise value, it is calculated by multiplying the product price by the product quantity
19346
+ additionalQueryParams = objectToQueryParams({ gmv });
19347
+ }
19348
+ // Fire the event and publish it to the pubsub service
19349
+ await this.fireAndPublishSpotEvent({
19350
+ spotEvent: eventData.event,
19351
+ eventUrl: `${eventUrl}${additionalQueryParams ? `&${additionalQueryParams}` : ''}`,
19352
+ placementId: spot.placementId,
19353
+ spotId: spot.spotId,
19354
+ });
19355
+ // Remove the event url from the spot to prevent duplicate events
19356
+ spot.events[eventPosition].url = '';
19357
+ // Update the spots in the local storage
19358
+ (_a = this.localStorageService) === null || _a === void 0 ? void 0 : _a.setSpot(spot.spotId, {
19359
+ placementId: spot.placementId,
19360
+ spotId: spot.spotId,
19361
+ spotType: spot.spotType,
19362
+ events: spot.events,
19363
+ productIds: spot.productIds,
19364
+ });
19484
19365
  }
19485
- const eventPosition = spot.events.findIndex((event) => event.event === eventData.event);
19486
- if (eventPosition === -1)
19487
- continue;
19488
- const eventUrl = spot.events[eventPosition].url;
19489
- const gmv = eventData.productPrice && eventData.productQuantity
19490
- ? eventData.productPrice * eventData.productQuantity
19491
- : undefined;
19492
- // gmv = gross merchandise value, it is calculated by multiplying the product price by the product quantity
19493
- const additionalQueryParams = objectToQueryParams({ gmv });
19494
- // Fire the event and publish it to the pubsub service
19495
- await this.fireAndPublishSpotEvent({
19496
- spotEvent: eventData.event,
19497
- eventUrl: `${eventUrl}${additionalQueryParams ? `&${additionalQueryParams}` : ''}`,
19498
- placementId: spot.placementId,
19499
- spotId: spot.spotId,
19500
- });
19501
- // Remove the event url from the spot to prevent duplicate events
19502
- spot.events[eventPosition].url = '';
19503
- // Update the spots in the local storage
19504
- (_a = this.localStorageService) === null || _a === void 0 ? void 0 : _a.setSpot(spot.spotId, {
19505
- placementId: spot.placementId,
19506
- spotId: spot.spotId,
19507
- spotType: spot.spotType,
19508
- events: spot.events,
19509
- productIds: spot.productIds,
19510
- });
19511
19366
  }
19512
19367
  }
19513
19368
  async fireAndPublishSpotEvent({ spotEvent, eventUrl, placementId, spotId, }) {