@ordersune/crm-web-sdk 1.0.9 → 1.0.11

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,12 @@ 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 logPushNotificationClick;
51
+ private processCampaignClick;
52
+ private buildCampaignPayload;
49
53
  }
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,6 +565,7 @@ 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);
@@ -583,6 +586,7 @@ class WebSDK {
583
586
  primaryButton.style.cursor = 'pointer';
584
587
  primaryButton.onclick = () => {
585
588
  document.body.removeChild(modalOverlay);
589
+ this.logInAppMessageClick(campaignId, true, true, false);
586
590
  onPrimaryButtonPress();
587
591
  };
588
592
  buttonsContainer.appendChild(primaryButton);
@@ -610,5 +614,50 @@ class WebSDK {
610
614
  }
611
615
  });
612
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 logPushNotificationClick(campaignId) {
622
+ const pushCampaignClickPayload = await this.buildCampaignPayload(campaignId, true, true);
623
+ await this.processCampaignClick(pushCampaignClickPayload);
624
+ }
625
+ async processCampaignClick(payload) {
626
+ try {
627
+ const response = await fetch(`${this.endpoint}/analytics/clicks`, {
628
+ method: "POST",
629
+ headers: {
630
+ "Content-Type": "application/json",
631
+ "x-api-key": this.apiKey,
632
+ },
633
+ body: JSON.stringify(payload),
634
+ });
635
+ if (!response.ok) {
636
+ throw new Error(`HTTP error! status: ${response.status}`);
637
+ }
638
+ if (this.debug) {
639
+ (0, utils_1.log)(this.debug, `process campaign click dispatched successfully: ${payload.campaignId}`);
640
+ }
641
+ }
642
+ catch (error) {
643
+ (0, utils_1.log)(this.debug, "Error processing campaign click:", error);
644
+ throw error;
645
+ }
646
+ }
647
+ async buildCampaignPayload(campaignId, opened, clicked, dismissed) {
648
+ const timestamp = Date.now();
649
+ const deviceId = this.deviceId;
650
+ const payload = {
651
+ campaignId,
652
+ opened,
653
+ clicked,
654
+ deviceId,
655
+ timestamp,
656
+ };
657
+ if (dismissed !== undefined) {
658
+ payload.dismissed = dismissed;
659
+ }
660
+ return payload;
661
+ }
613
662
  }
614
663
  exports.WebSDK = WebSDK;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordersune/crm-web-sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {