@ordersune/crm-web-sdk 1.0.8 → 1.0.10

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.
@@ -81,4 +81,13 @@ export interface ShowInAppMessageOptions {
81
81
  headerColor: string;
82
82
  messageColor: string;
83
83
  closeColor: string;
84
+ campaignId: string;
85
+ }
86
+ export interface CampaignPayload {
87
+ campaignId: string;
88
+ opened: boolean;
89
+ clicked: boolean;
90
+ deviceId: string;
91
+ timestamp: number;
92
+ dismissed?: boolean;
84
93
  }
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
package/dist/web-sdk.d.ts CHANGED
@@ -42,8 +42,11 @@ export declare class WebSDK {
42
42
  private queueEvent;
43
43
  private processBatch;
44
44
  private sendBatchToServer;
45
- displayInAppMessage(inAppMessageData: string, context?: any): void;
45
+ displayInAppMessage(inAppMessageData: string, campaignId: string, context?: any): void;
46
46
  private hexToColor;
47
47
  private openWebUrl;
48
48
  private showInAppMessage;
49
+ private logInAppMessageClick;
50
+ private processCampaignClick;
51
+ private buildCampaignPayload;
49
52
  }
package/dist/web-sdk.js CHANGED
@@ -398,7 +398,7 @@ class WebSDK {
398
398
  throw error;
399
399
  }
400
400
  }
401
- displayInAppMessage(inAppMessageData, context) {
401
+ displayInAppMessage(inAppMessageData, campaignId, context) {
402
402
  const transformedData = JSON.parse(inAppMessageData);
403
403
  const title = transformedData.header;
404
404
  const body = transformedData.message;
@@ -420,7 +420,8 @@ class WebSDK {
420
420
  dismissMessageInSeconds: transformedData.modal.dismissMessageInSeconds,
421
421
  headerColor: transformedData.modal.headerColor,
422
422
  messageColor: transformedData.modal.messageColor,
423
- closeColor: transformedData.modal.closeColor
423
+ closeColor: transformedData.modal.closeColor,
424
+ campaignId: campaignId,
424
425
  });
425
426
  }
426
427
  hexToColor(hexColor) {
@@ -436,7 +437,7 @@ class WebSDK {
436
437
  }
437
438
  }
438
439
  showInAppMessage(options) {
439
- const { title, body, primaryButtonText, secondaryButtonText, imageUrl, onClose, buttonPropertiesPrimary, buttonPropertiesSecondary, dismissMessageAutomatically, dismissMessageInSeconds, headerColor, messageColor, closeColor } = options;
440
+ const { title, body, primaryButtonText, secondaryButtonText, imageUrl, onClose, buttonPropertiesPrimary, buttonPropertiesSecondary, dismissMessageAutomatically, dismissMessageInSeconds, headerColor, messageColor, closeColor, campaignId, } = options;
440
441
  // Create modal container
441
442
  const modalOverlay = document.createElement('div');
442
443
  modalOverlay.style.position = 'fixed';
@@ -473,6 +474,7 @@ class WebSDK {
473
474
  if (onClose) {
474
475
  onClose();
475
476
  }
477
+ this.logInAppMessageClick(campaignId, false, true, true);
476
478
  document.body.removeChild(modalOverlay);
477
479
  };
478
480
  // Create content
@@ -563,28 +565,32 @@ class WebSDK {
563
565
  secondaryButton.style.cursor = 'pointer';
564
566
  secondaryButton.onclick = () => {
565
567
  document.body.removeChild(modalOverlay);
568
+ this.logInAppMessageClick(campaignId, true, true, false);
566
569
  onSecondaryButtonPress();
567
570
  };
568
571
  buttonsContainer.appendChild(secondaryButton);
569
572
  }
570
573
  else {
571
574
  // Single button
572
- const primaryButton = document.createElement('button');
573
- primaryButton.textContent = primaryButtonText;
574
- primaryButton.style.width = '100%';
575
- primaryButton.style.padding = '12px';
576
- primaryButton.style.backgroundColor = this.hexToColor(buttonPropertiesPrimary.backgroundColor);
577
- primaryButton.style.color = this.hexToColor(buttonPropertiesPrimary.textColor);
578
- primaryButton.style.border = `2px solid ${this.hexToColor(buttonPropertiesPrimary.borderColor)}`;
579
- primaryButton.style.borderRadius = '8px';
580
- primaryButton.style.fontSize = '16px';
581
- primaryButton.style.fontWeight = '600';
582
- primaryButton.style.cursor = 'pointer';
583
- primaryButton.onclick = () => {
584
- document.body.removeChild(modalOverlay);
585
- onPrimaryButtonPress();
586
- };
587
- buttonsContainer.appendChild(primaryButton);
575
+ if (primaryButtonText && primaryButtonText.trim() !== '') {
576
+ const primaryButton = document.createElement('button');
577
+ primaryButton.textContent = primaryButtonText;
578
+ primaryButton.style.width = '100%';
579
+ primaryButton.style.padding = '12px';
580
+ primaryButton.style.backgroundColor = this.hexToColor(buttonPropertiesPrimary.backgroundColor);
581
+ primaryButton.style.color = this.hexToColor(buttonPropertiesPrimary.textColor);
582
+ primaryButton.style.border = `2px solid ${this.hexToColor(buttonPropertiesPrimary.borderColor)}`;
583
+ primaryButton.style.borderRadius = '8px';
584
+ primaryButton.style.fontSize = '16px';
585
+ primaryButton.style.fontWeight = '600';
586
+ primaryButton.style.cursor = 'pointer';
587
+ primaryButton.onclick = () => {
588
+ document.body.removeChild(modalOverlay);
589
+ this.logInAppMessageClick(campaignId, true, true, false);
590
+ onPrimaryButtonPress();
591
+ };
592
+ buttonsContainer.appendChild(primaryButton);
593
+ }
588
594
  }
589
595
  // Add all elements to the modal
590
596
  modalDialog.appendChild(closeButton);
@@ -608,5 +614,46 @@ class WebSDK {
608
614
  }
609
615
  });
610
616
  }
617
+ async logInAppMessageClick(campaignId, clicked, opened, dismissed) {
618
+ const campaignClickPayload = await this.buildCampaignPayload(campaignId, opened, clicked, dismissed);
619
+ await this.processCampaignClick(campaignClickPayload);
620
+ }
621
+ async processCampaignClick(payload) {
622
+ try {
623
+ const response = await fetch(`${this.endpoint}/analytics/clicks`, {
624
+ method: "POST",
625
+ headers: {
626
+ "Content-Type": "application/json",
627
+ "x-api-key": this.apiKey,
628
+ },
629
+ body: JSON.stringify(payload),
630
+ });
631
+ if (!response.ok) {
632
+ throw new Error(`HTTP error! status: ${response.status}`);
633
+ }
634
+ if (this.debug) {
635
+ (0, utils_1.log)(this.debug, `process campaign click dispatched successfully: ${payload.campaignId}`);
636
+ }
637
+ }
638
+ catch (error) {
639
+ (0, utils_1.log)(this.debug, "Error processing campaign click:", error);
640
+ throw error;
641
+ }
642
+ }
643
+ async buildCampaignPayload(campaignId, opened, clicked, dismissed) {
644
+ const timestamp = Date.now();
645
+ const deviceId = this.deviceId;
646
+ const payload = {
647
+ campaignId,
648
+ opened,
649
+ clicked,
650
+ deviceId,
651
+ timestamp,
652
+ };
653
+ if (dismissed !== undefined) {
654
+ payload.dismissed = dismissed;
655
+ }
656
+ return payload;
657
+ }
611
658
  }
612
659
  exports.WebSDK = WebSDK;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordersune/crm-web-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {