@blotoutio/providers-shop-gpt-sdk 1.10.0 → 1.10.2

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 (4) hide show
  1. package/index.cjs.js +22 -62
  2. package/index.js +22 -62
  3. package/index.mjs +22 -62
  4. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -431,42 +431,6 @@ const previewKeyName = 'previewShopGPT';
431
431
 
432
432
  const keyPrefix = `_worker`;
433
433
 
434
- const getCookieValue = (key) => {
435
- var _a;
436
- try {
437
- if (!document || !document.cookie) {
438
- return '';
439
- }
440
- const cookies = parseCookies(document.cookie);
441
- return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
442
- }
443
- catch {
444
- return '';
445
- }
446
- };
447
- const parseCookies = (cookie) => {
448
- return Object.fromEntries(cookie
449
- .split(/;\s+/)
450
- .map((r) => r.split('=').map((str) => str.trim()))
451
- .map(([cookieKey, ...cookieValues]) => {
452
- const cookieValue = cookieValues.join('=');
453
- if (!cookieKey) {
454
- return [];
455
- }
456
- let decodedValue = '';
457
- if (cookieValue) {
458
- try {
459
- decodedValue = decodeURIComponent(cookieValue);
460
- }
461
- catch (e) {
462
- console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
463
- decodedValue = cookieValue;
464
- }
465
- }
466
- return [cookieKey, decodedValue];
467
- }));
468
- };
469
-
470
434
  const canLog = () => {
471
435
  try {
472
436
  return localStorage.getItem('edgeTagDebug') === '1';
@@ -596,9 +560,6 @@ const getLocalStorageData = (destination) => {
596
560
  const saveLocalStorageData = (destination, data) => {
597
561
  saveData(destination, 'local', data, SHOP_GPT_LOCAL_STORAGE_KEY);
598
562
  };
599
- const getSessionId = () => {
600
- return getCookieValue('tag_session');
601
- };
602
563
 
603
564
  // eslint-disable-next-line @nx/enforce-module-boundaries
604
565
  const hasPreviewKey = () => {
@@ -622,20 +583,18 @@ const setUserInteracted = (destination) => {
622
583
  chatbot: { ...session === null || session === void 0 ? void 0 : session.chatbot, hasUserInteracted: true },
623
584
  });
624
585
  };
625
- const getProductActions = (destination) => {
586
+ const getProductActions = (destination, sessionId) => {
626
587
  var _a;
627
588
  const local = getLocalStorageData(destination);
628
- const sessionId = getSessionId();
629
589
  if (!local || !sessionId) {
630
590
  logger.error('No local storage data or session id');
631
591
  return null;
632
592
  }
633
593
  return (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.products;
634
594
  };
635
- const setProductAction = (destination, productId, action, value) => {
595
+ const setProductAction = (destination, sessionId, productId, action, value) => {
636
596
  var _a, _b, _c;
637
597
  const local = getLocalStorageData(destination);
638
- const sessionId = getSessionId();
639
598
  if (!local || !sessionId) {
640
599
  logger.error('No local storage data or session id');
641
600
  return;
@@ -655,19 +614,17 @@ const setProductAction = (destination, productId, action, value) => {
655
614
  const updatedLocal = { [sessionId]: local[sessionId] };
656
615
  saveLocalStorageData(destination, updatedLocal);
657
616
  };
658
- const getShopGPTLoaded = (destination) => {
617
+ const getShopGPTLoaded = (destination, sessionId) => {
659
618
  var _a, _b;
660
619
  const local = getLocalStorageData(destination);
661
- const sessionId = getSessionId();
662
620
  if (!local || !sessionId) {
663
621
  logger.error('No local storage data or session id');
664
622
  return false;
665
623
  }
666
624
  return (_b = (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.isShopGPTLoaded) !== null && _b !== void 0 ? _b : false;
667
625
  };
668
- const setShopGPTLoaded = (destination, value) => {
626
+ const setShopGPTLoaded = (destination, sessionId, value) => {
669
627
  const local = getLocalStorageData(destination);
670
- const sessionId = getSessionId();
671
628
  if (!local || !sessionId) {
672
629
  logger.error('No local storage data or session id');
673
630
  return;
@@ -681,7 +638,7 @@ const setShopGPTLoaded = (destination, value) => {
681
638
  saveLocalStorageData(destination, updatedLocal);
682
639
  };
683
640
 
684
- const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, }) => {
641
+ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, sessionId, }) => {
685
642
  if (!baseURL) {
686
643
  throw new Error(`baseURL missing`);
687
644
  }
@@ -812,7 +769,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
812
769
  };
813
770
  const sendEvent = async (action, currency, actionData) => {
814
771
  var _a;
815
- const storageData = (_a = getProductActions(baseURL)) !== null && _a !== void 0 ? _a : {};
772
+ const storageData = (_a = getProductActions(baseURL, sessionId)) !== null && _a !== void 0 ? _a : {};
816
773
  const response = await fetchImpl(getURL('/user/event'), {
817
774
  method: 'POST',
818
775
  headers: getHeaders(true),
@@ -848,7 +805,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
848
805
  // eslint-disable-next-line @nx/enforce-module-boundaries
849
806
  const error = (message) => console.error(message);
850
807
  const init = (params) => {
851
- var _a, _b, _c;
808
+ var _a, _b, _c, _d, _e, _f;
852
809
  if (typeof window == 'undefined' || typeof document == 'undefined') {
853
810
  // if loaded in non-browser SDKs, return early
854
811
  return;
@@ -867,7 +824,7 @@ const init = (params) => {
867
824
  return;
868
825
  }
869
826
  const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, botIconUrl, css, nudge, loadUiManually, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
870
- setShopGPTLoaded(params.baseUrl, !loadUiManually);
827
+ setShopGPTLoaded(params.baseUrl, (_d = params.session) === null || _d === void 0 ? void 0 : _d.sessionId, !loadUiManually);
871
828
  const experiment = createExperiment({
872
829
  name: getExperimentName(mode),
873
830
  userId: params.userId,
@@ -887,6 +844,7 @@ const init = (params) => {
887
844
  baseURL: params.baseUrl,
888
845
  storeAPI,
889
846
  userId: params.userId,
847
+ sessionId: (_e = params.session) === null || _e === void 0 ? void 0 : _e.sessionId,
890
848
  });
891
849
  uiImplementation.init({
892
850
  destination: params.baseUrl,
@@ -905,6 +863,7 @@ const init = (params) => {
905
863
  botIconUrl,
906
864
  css,
907
865
  nudge,
866
+ sessionId: (_f = params.session) === null || _f === void 0 ? void 0 : _f.sessionId,
908
867
  });
909
868
  if (!loadUiManually) {
910
869
  uiImplementation.loadUI();
@@ -912,31 +871,31 @@ const init = (params) => {
912
871
  }
913
872
  };
914
873
 
915
- const getClickedProductsInContents = (destination, data) => {
916
- const storedData = getProductActions(destination);
874
+ const getClickedProductsInContents = (destination, sessionId, data) => {
875
+ const storedData = getProductActions(destination, sessionId);
917
876
  const contents = data['contents'];
918
877
  if (!contents || !Array.isArray(contents) || !storedData) {
919
878
  return;
920
879
  }
921
880
  return contents.flatMap((content) => { var _a; return ((_a = storedData[content.id]) === null || _a === void 0 ? void 0 : _a.clicked) ? [content.id] : []; });
922
881
  };
923
- const tag = ({ eventName, destination, data, }) => {
882
+ const tag = ({ eventName, destination, data, sessionId, }) => {
924
883
  var _a;
925
- const clickedProducts = getClickedProductsInContents(destination, data);
884
+ const clickedProducts = getClickedProductsInContents(destination, sessionId, data);
926
885
  if (eventName === 'AddToCart') {
927
886
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
928
- setProductAction(destination, id, 'addToCart', true);
887
+ setProductAction(destination, sessionId, id, 'addToCart', true);
929
888
  });
930
889
  }
931
890
  else if (eventName == 'RemoveFromCart') {
932
891
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
933
- setProductAction(destination, id, 'addToCart', false);
892
+ setProductAction(destination, sessionId, id, 'addToCart', false);
934
893
  });
935
894
  }
936
895
  return {
937
- session: getProductActions(destination),
896
+ session: getProductActions(destination, sessionId),
938
897
  preview: hasPreviewKey(),
939
- isShopGPTLoaded: (_a = getShopGPTLoaded(destination)) !== null && _a !== void 0 ? _a : false,
898
+ isShopGPTLoaded: (_a = getShopGPTLoaded(destination, sessionId)) !== null && _a !== void 0 ? _a : false,
940
899
  };
941
900
  };
942
901
 
@@ -5092,7 +5051,7 @@ class ShopGPT extends r$2 {
5092
5051
  }
5093
5052
  productClicked(e) {
5094
5053
  e.stopPropagation();
5095
- setProductAction(this.destination, e.detail.productId, 'clicked', true);
5054
+ setProductAction(this.destination, this.sessionId, e.detail.productId, 'clicked', true);
5096
5055
  this.shopGPTAPI.sendEvent('productRecommendationClicked', this.getSiteCurrency().currency, {
5097
5056
  productId: e.detail.productId,
5098
5057
  value: e.detail.value,
@@ -5361,6 +5320,7 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
5361
5320
  shopGPT.botIconUrl = params.botIconUrl;
5362
5321
  shopGPT.css = params.css;
5363
5322
  shopGPT.nudge = params.nudge;
5323
+ shopGPT.sessionId = params.sessionId;
5364
5324
  },
5365
5325
  loadUI() {
5366
5326
  if (!shopGPT) {
@@ -5372,13 +5332,13 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
5372
5332
  return;
5373
5333
  }
5374
5334
  document.body.append(shopGPT);
5375
- setShopGPTLoaded(shopGPT.destination, true);
5335
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, true);
5376
5336
  },
5377
5337
  destroy() {
5378
5338
  if (!shopGPT) {
5379
5339
  return;
5380
5340
  }
5381
- setShopGPTLoaded(shopGPT.destination, false);
5341
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, false);
5382
5342
  shopGPT.remove();
5383
5343
  shopGPT = undefined;
5384
5344
  delete window[registryKey];
package/index.js CHANGED
@@ -432,42 +432,6 @@ var ProvidersShopGptSdk = (function () {
432
432
 
433
433
  const keyPrefix = `_worker`;
434
434
 
435
- const getCookieValue = (key) => {
436
- var _a;
437
- try {
438
- if (!document || !document.cookie) {
439
- return '';
440
- }
441
- const cookies = parseCookies(document.cookie);
442
- return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
443
- }
444
- catch {
445
- return '';
446
- }
447
- };
448
- const parseCookies = (cookie) => {
449
- return Object.fromEntries(cookie
450
- .split(/;\s+/)
451
- .map((r) => r.split('=').map((str) => str.trim()))
452
- .map(([cookieKey, ...cookieValues]) => {
453
- const cookieValue = cookieValues.join('=');
454
- if (!cookieKey) {
455
- return [];
456
- }
457
- let decodedValue = '';
458
- if (cookieValue) {
459
- try {
460
- decodedValue = decodeURIComponent(cookieValue);
461
- }
462
- catch (e) {
463
- console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
464
- decodedValue = cookieValue;
465
- }
466
- }
467
- return [cookieKey, decodedValue];
468
- }));
469
- };
470
-
471
435
  const canLog = () => {
472
436
  try {
473
437
  return localStorage.getItem('edgeTagDebug') === '1';
@@ -597,9 +561,6 @@ var ProvidersShopGptSdk = (function () {
597
561
  const saveLocalStorageData = (destination, data) => {
598
562
  saveData(destination, 'local', data, SHOP_GPT_LOCAL_STORAGE_KEY);
599
563
  };
600
- const getSessionId = () => {
601
- return getCookieValue('tag_session');
602
- };
603
564
 
604
565
  // eslint-disable-next-line @nx/enforce-module-boundaries
605
566
  const hasPreviewKey = () => {
@@ -623,20 +584,18 @@ var ProvidersShopGptSdk = (function () {
623
584
  chatbot: { ...session === null || session === void 0 ? void 0 : session.chatbot, hasUserInteracted: true },
624
585
  });
625
586
  };
626
- const getProductActions = (destination) => {
587
+ const getProductActions = (destination, sessionId) => {
627
588
  var _a;
628
589
  const local = getLocalStorageData(destination);
629
- const sessionId = getSessionId();
630
590
  if (!local || !sessionId) {
631
591
  logger.error('No local storage data or session id');
632
592
  return null;
633
593
  }
634
594
  return (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.products;
635
595
  };
636
- const setProductAction = (destination, productId, action, value) => {
596
+ const setProductAction = (destination, sessionId, productId, action, value) => {
637
597
  var _a, _b, _c;
638
598
  const local = getLocalStorageData(destination);
639
- const sessionId = getSessionId();
640
599
  if (!local || !sessionId) {
641
600
  logger.error('No local storage data or session id');
642
601
  return;
@@ -656,19 +615,17 @@ var ProvidersShopGptSdk = (function () {
656
615
  const updatedLocal = { [sessionId]: local[sessionId] };
657
616
  saveLocalStorageData(destination, updatedLocal);
658
617
  };
659
- const getShopGPTLoaded = (destination) => {
618
+ const getShopGPTLoaded = (destination, sessionId) => {
660
619
  var _a, _b;
661
620
  const local = getLocalStorageData(destination);
662
- const sessionId = getSessionId();
663
621
  if (!local || !sessionId) {
664
622
  logger.error('No local storage data or session id');
665
623
  return false;
666
624
  }
667
625
  return (_b = (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.isShopGPTLoaded) !== null && _b !== void 0 ? _b : false;
668
626
  };
669
- const setShopGPTLoaded = (destination, value) => {
627
+ const setShopGPTLoaded = (destination, sessionId, value) => {
670
628
  const local = getLocalStorageData(destination);
671
- const sessionId = getSessionId();
672
629
  if (!local || !sessionId) {
673
630
  logger.error('No local storage data or session id');
674
631
  return;
@@ -682,7 +639,7 @@ var ProvidersShopGptSdk = (function () {
682
639
  saveLocalStorageData(destination, updatedLocal);
683
640
  };
684
641
 
685
- const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, }) => {
642
+ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, sessionId, }) => {
686
643
  if (!baseURL) {
687
644
  throw new Error(`baseURL missing`);
688
645
  }
@@ -813,7 +770,7 @@ var ProvidersShopGptSdk = (function () {
813
770
  };
814
771
  const sendEvent = async (action, currency, actionData) => {
815
772
  var _a;
816
- const storageData = (_a = getProductActions(baseURL)) !== null && _a !== void 0 ? _a : {};
773
+ const storageData = (_a = getProductActions(baseURL, sessionId)) !== null && _a !== void 0 ? _a : {};
817
774
  const response = await fetchImpl(getURL('/user/event'), {
818
775
  method: 'POST',
819
776
  headers: getHeaders(true),
@@ -849,7 +806,7 @@ var ProvidersShopGptSdk = (function () {
849
806
  // eslint-disable-next-line @nx/enforce-module-boundaries
850
807
  const error = (message) => console.error(message);
851
808
  const init = (params) => {
852
- var _a, _b, _c;
809
+ var _a, _b, _c, _d, _e, _f;
853
810
  if (typeof window == 'undefined' || typeof document == 'undefined') {
854
811
  // if loaded in non-browser SDKs, return early
855
812
  return;
@@ -868,7 +825,7 @@ var ProvidersShopGptSdk = (function () {
868
825
  return;
869
826
  }
870
827
  const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, botIconUrl, css, nudge, loadUiManually, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
871
- setShopGPTLoaded(params.baseUrl, !loadUiManually);
828
+ setShopGPTLoaded(params.baseUrl, (_d = params.session) === null || _d === void 0 ? void 0 : _d.sessionId, !loadUiManually);
872
829
  const experiment = createExperiment({
873
830
  name: getExperimentName(mode),
874
831
  userId: params.userId,
@@ -888,6 +845,7 @@ var ProvidersShopGptSdk = (function () {
888
845
  baseURL: params.baseUrl,
889
846
  storeAPI,
890
847
  userId: params.userId,
848
+ sessionId: (_e = params.session) === null || _e === void 0 ? void 0 : _e.sessionId,
891
849
  });
892
850
  uiImplementation.init({
893
851
  destination: params.baseUrl,
@@ -906,6 +864,7 @@ var ProvidersShopGptSdk = (function () {
906
864
  botIconUrl,
907
865
  css,
908
866
  nudge,
867
+ sessionId: (_f = params.session) === null || _f === void 0 ? void 0 : _f.sessionId,
909
868
  });
910
869
  if (!loadUiManually) {
911
870
  uiImplementation.loadUI();
@@ -913,31 +872,31 @@ var ProvidersShopGptSdk = (function () {
913
872
  }
914
873
  };
915
874
 
916
- const getClickedProductsInContents = (destination, data) => {
917
- const storedData = getProductActions(destination);
875
+ const getClickedProductsInContents = (destination, sessionId, data) => {
876
+ const storedData = getProductActions(destination, sessionId);
918
877
  const contents = data['contents'];
919
878
  if (!contents || !Array.isArray(contents) || !storedData) {
920
879
  return;
921
880
  }
922
881
  return contents.flatMap((content) => { var _a; return ((_a = storedData[content.id]) === null || _a === void 0 ? void 0 : _a.clicked) ? [content.id] : []; });
923
882
  };
924
- const tag = ({ eventName, destination, data, }) => {
883
+ const tag = ({ eventName, destination, data, sessionId, }) => {
925
884
  var _a;
926
- const clickedProducts = getClickedProductsInContents(destination, data);
885
+ const clickedProducts = getClickedProductsInContents(destination, sessionId, data);
927
886
  if (eventName === 'AddToCart') {
928
887
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
929
- setProductAction(destination, id, 'addToCart', true);
888
+ setProductAction(destination, sessionId, id, 'addToCart', true);
930
889
  });
931
890
  }
932
891
  else if (eventName == 'RemoveFromCart') {
933
892
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
934
- setProductAction(destination, id, 'addToCart', false);
893
+ setProductAction(destination, sessionId, id, 'addToCart', false);
935
894
  });
936
895
  }
937
896
  return {
938
- session: getProductActions(destination),
897
+ session: getProductActions(destination, sessionId),
939
898
  preview: hasPreviewKey(),
940
- isShopGPTLoaded: (_a = getShopGPTLoaded(destination)) !== null && _a !== void 0 ? _a : false,
899
+ isShopGPTLoaded: (_a = getShopGPTLoaded(destination, sessionId)) !== null && _a !== void 0 ? _a : false,
941
900
  };
942
901
  };
943
902
 
@@ -5093,7 +5052,7 @@ ${this.comment ? this.comment : E}</textarea
5093
5052
  }
5094
5053
  productClicked(e) {
5095
5054
  e.stopPropagation();
5096
- setProductAction(this.destination, e.detail.productId, 'clicked', true);
5055
+ setProductAction(this.destination, this.sessionId, e.detail.productId, 'clicked', true);
5097
5056
  this.shopGPTAPI.sendEvent('productRecommendationClicked', this.getSiteCurrency().currency, {
5098
5057
  productId: e.detail.productId,
5099
5058
  value: e.detail.value,
@@ -5362,6 +5321,7 @@ ${this.comment ? this.comment : E}</textarea
5362
5321
  shopGPT.botIconUrl = params.botIconUrl;
5363
5322
  shopGPT.css = params.css;
5364
5323
  shopGPT.nudge = params.nudge;
5324
+ shopGPT.sessionId = params.sessionId;
5365
5325
  },
5366
5326
  loadUI() {
5367
5327
  if (!shopGPT) {
@@ -5373,13 +5333,13 @@ ${this.comment ? this.comment : E}</textarea
5373
5333
  return;
5374
5334
  }
5375
5335
  document.body.append(shopGPT);
5376
- setShopGPTLoaded(shopGPT.destination, true);
5336
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, true);
5377
5337
  },
5378
5338
  destroy() {
5379
5339
  if (!shopGPT) {
5380
5340
  return;
5381
5341
  }
5382
- setShopGPTLoaded(shopGPT.destination, false);
5342
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, false);
5383
5343
  shopGPT.remove();
5384
5344
  shopGPT = undefined;
5385
5345
  delete window[registryKey];
package/index.mjs CHANGED
@@ -429,42 +429,6 @@ const previewKeyName = 'previewShopGPT';
429
429
 
430
430
  const keyPrefix = `_worker`;
431
431
 
432
- const getCookieValue = (key) => {
433
- var _a;
434
- try {
435
- if (!document || !document.cookie) {
436
- return '';
437
- }
438
- const cookies = parseCookies(document.cookie);
439
- return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
440
- }
441
- catch {
442
- return '';
443
- }
444
- };
445
- const parseCookies = (cookie) => {
446
- return Object.fromEntries(cookie
447
- .split(/;\s+/)
448
- .map((r) => r.split('=').map((str) => str.trim()))
449
- .map(([cookieKey, ...cookieValues]) => {
450
- const cookieValue = cookieValues.join('=');
451
- if (!cookieKey) {
452
- return [];
453
- }
454
- let decodedValue = '';
455
- if (cookieValue) {
456
- try {
457
- decodedValue = decodeURIComponent(cookieValue);
458
- }
459
- catch (e) {
460
- console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
461
- decodedValue = cookieValue;
462
- }
463
- }
464
- return [cookieKey, decodedValue];
465
- }));
466
- };
467
-
468
432
  const canLog = () => {
469
433
  try {
470
434
  return localStorage.getItem('edgeTagDebug') === '1';
@@ -594,9 +558,6 @@ const getLocalStorageData = (destination) => {
594
558
  const saveLocalStorageData = (destination, data) => {
595
559
  saveData(destination, 'local', data, SHOP_GPT_LOCAL_STORAGE_KEY);
596
560
  };
597
- const getSessionId = () => {
598
- return getCookieValue('tag_session');
599
- };
600
561
 
601
562
  // eslint-disable-next-line @nx/enforce-module-boundaries
602
563
  const hasPreviewKey = () => {
@@ -620,20 +581,18 @@ const setUserInteracted = (destination) => {
620
581
  chatbot: { ...session === null || session === void 0 ? void 0 : session.chatbot, hasUserInteracted: true },
621
582
  });
622
583
  };
623
- const getProductActions = (destination) => {
584
+ const getProductActions = (destination, sessionId) => {
624
585
  var _a;
625
586
  const local = getLocalStorageData(destination);
626
- const sessionId = getSessionId();
627
587
  if (!local || !sessionId) {
628
588
  logger.error('No local storage data or session id');
629
589
  return null;
630
590
  }
631
591
  return (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.products;
632
592
  };
633
- const setProductAction = (destination, productId, action, value) => {
593
+ const setProductAction = (destination, sessionId, productId, action, value) => {
634
594
  var _a, _b, _c;
635
595
  const local = getLocalStorageData(destination);
636
- const sessionId = getSessionId();
637
596
  if (!local || !sessionId) {
638
597
  logger.error('No local storage data or session id');
639
598
  return;
@@ -653,19 +612,17 @@ const setProductAction = (destination, productId, action, value) => {
653
612
  const updatedLocal = { [sessionId]: local[sessionId] };
654
613
  saveLocalStorageData(destination, updatedLocal);
655
614
  };
656
- const getShopGPTLoaded = (destination) => {
615
+ const getShopGPTLoaded = (destination, sessionId) => {
657
616
  var _a, _b;
658
617
  const local = getLocalStorageData(destination);
659
- const sessionId = getSessionId();
660
618
  if (!local || !sessionId) {
661
619
  logger.error('No local storage data or session id');
662
620
  return false;
663
621
  }
664
622
  return (_b = (_a = local[sessionId]) === null || _a === void 0 ? void 0 : _a.isShopGPTLoaded) !== null && _b !== void 0 ? _b : false;
665
623
  };
666
- const setShopGPTLoaded = (destination, value) => {
624
+ const setShopGPTLoaded = (destination, sessionId, value) => {
667
625
  const local = getLocalStorageData(destination);
668
- const sessionId = getSessionId();
669
626
  if (!local || !sessionId) {
670
627
  logger.error('No local storage data or session id');
671
628
  return;
@@ -679,7 +636,7 @@ const setShopGPTLoaded = (destination, value) => {
679
636
  saveLocalStorageData(destination, updatedLocal);
680
637
  };
681
638
 
682
- const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, }) => {
639
+ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, storeAPI, sessionId, }) => {
683
640
  if (!baseURL) {
684
641
  throw new Error(`baseURL missing`);
685
642
  }
@@ -810,7 +767,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
810
767
  };
811
768
  const sendEvent = async (action, currency, actionData) => {
812
769
  var _a;
813
- const storageData = (_a = getProductActions(baseURL)) !== null && _a !== void 0 ? _a : {};
770
+ const storageData = (_a = getProductActions(baseURL, sessionId)) !== null && _a !== void 0 ? _a : {};
814
771
  const response = await fetchImpl(getURL('/user/event'), {
815
772
  method: 'POST',
816
773
  headers: getHeaders(true),
@@ -846,7 +803,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
846
803
  // eslint-disable-next-line @nx/enforce-module-boundaries
847
804
  const error = (message) => console.error(message);
848
805
  const init = (params) => {
849
- var _a, _b, _c;
806
+ var _a, _b, _c, _d, _e, _f;
850
807
  if (typeof window == 'undefined' || typeof document == 'undefined') {
851
808
  // if loaded in non-browser SDKs, return early
852
809
  return;
@@ -865,7 +822,7 @@ const init = (params) => {
865
822
  return;
866
823
  }
867
824
  const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, botIconUrl, css, nudge, loadUiManually, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
868
- setShopGPTLoaded(params.baseUrl, !loadUiManually);
825
+ setShopGPTLoaded(params.baseUrl, (_d = params.session) === null || _d === void 0 ? void 0 : _d.sessionId, !loadUiManually);
869
826
  const experiment = createExperiment({
870
827
  name: getExperimentName(mode),
871
828
  userId: params.userId,
@@ -885,6 +842,7 @@ const init = (params) => {
885
842
  baseURL: params.baseUrl,
886
843
  storeAPI,
887
844
  userId: params.userId,
845
+ sessionId: (_e = params.session) === null || _e === void 0 ? void 0 : _e.sessionId,
888
846
  });
889
847
  uiImplementation.init({
890
848
  destination: params.baseUrl,
@@ -903,6 +861,7 @@ const init = (params) => {
903
861
  botIconUrl,
904
862
  css,
905
863
  nudge,
864
+ sessionId: (_f = params.session) === null || _f === void 0 ? void 0 : _f.sessionId,
906
865
  });
907
866
  if (!loadUiManually) {
908
867
  uiImplementation.loadUI();
@@ -910,31 +869,31 @@ const init = (params) => {
910
869
  }
911
870
  };
912
871
 
913
- const getClickedProductsInContents = (destination, data) => {
914
- const storedData = getProductActions(destination);
872
+ const getClickedProductsInContents = (destination, sessionId, data) => {
873
+ const storedData = getProductActions(destination, sessionId);
915
874
  const contents = data['contents'];
916
875
  if (!contents || !Array.isArray(contents) || !storedData) {
917
876
  return;
918
877
  }
919
878
  return contents.flatMap((content) => { var _a; return ((_a = storedData[content.id]) === null || _a === void 0 ? void 0 : _a.clicked) ? [content.id] : []; });
920
879
  };
921
- const tag = ({ eventName, destination, data, }) => {
880
+ const tag = ({ eventName, destination, data, sessionId, }) => {
922
881
  var _a;
923
- const clickedProducts = getClickedProductsInContents(destination, data);
882
+ const clickedProducts = getClickedProductsInContents(destination, sessionId, data);
924
883
  if (eventName === 'AddToCart') {
925
884
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
926
- setProductAction(destination, id, 'addToCart', true);
885
+ setProductAction(destination, sessionId, id, 'addToCart', true);
927
886
  });
928
887
  }
929
888
  else if (eventName == 'RemoveFromCart') {
930
889
  clickedProducts === null || clickedProducts === void 0 ? void 0 : clickedProducts.forEach((id) => {
931
- setProductAction(destination, id, 'addToCart', false);
890
+ setProductAction(destination, sessionId, id, 'addToCart', false);
932
891
  });
933
892
  }
934
893
  return {
935
- session: getProductActions(destination),
894
+ session: getProductActions(destination, sessionId),
936
895
  preview: hasPreviewKey(),
937
- isShopGPTLoaded: (_a = getShopGPTLoaded(destination)) !== null && _a !== void 0 ? _a : false,
896
+ isShopGPTLoaded: (_a = getShopGPTLoaded(destination, sessionId)) !== null && _a !== void 0 ? _a : false,
938
897
  };
939
898
  };
940
899
 
@@ -5090,7 +5049,7 @@ class ShopGPT extends r$2 {
5090
5049
  }
5091
5050
  productClicked(e) {
5092
5051
  e.stopPropagation();
5093
- setProductAction(this.destination, e.detail.productId, 'clicked', true);
5052
+ setProductAction(this.destination, this.sessionId, e.detail.productId, 'clicked', true);
5094
5053
  this.shopGPTAPI.sendEvent('productRecommendationClicked', this.getSiteCurrency().currency, {
5095
5054
  productId: e.detail.productId,
5096
5055
  value: e.detail.value,
@@ -5359,6 +5318,7 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
5359
5318
  shopGPT.botIconUrl = params.botIconUrl;
5360
5319
  shopGPT.css = params.css;
5361
5320
  shopGPT.nudge = params.nudge;
5321
+ shopGPT.sessionId = params.sessionId;
5362
5322
  },
5363
5323
  loadUI() {
5364
5324
  if (!shopGPT) {
@@ -5370,13 +5330,13 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
5370
5330
  return;
5371
5331
  }
5372
5332
  document.body.append(shopGPT);
5373
- setShopGPTLoaded(shopGPT.destination, true);
5333
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, true);
5374
5334
  },
5375
5335
  destroy() {
5376
5336
  if (!shopGPT) {
5377
5337
  return;
5378
5338
  }
5379
- setShopGPTLoaded(shopGPT.destination, false);
5339
+ setShopGPTLoaded(shopGPT.destination, shopGPT.sessionId, false);
5380
5340
  shopGPT.remove();
5381
5341
  shopGPT = undefined;
5382
5342
  delete window[registryKey];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/providers-shop-gpt-sdk",
3
- "version": "1.10.0",
3
+ "version": "1.10.2",
4
4
  "description": "Shop GPT SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",