@tolinku/web-sdk 0.7.0 → 0.8.0

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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/types.ts
2
- var SDK_VERSION = "0.7.0";
2
+ var SDK_VERSION = "0.8.0";
3
3
 
4
4
  // src/client.ts
5
5
  var MAX_RETRIES = 3;
@@ -860,6 +860,35 @@ var Banners = class {
860
860
  this.currentStyle = "pinned";
861
861
  this.currentPosition = "top";
862
862
  }
863
+ /**
864
+ * Tell the server a banner was tapped.
865
+ *
866
+ * sendBeacon, because the navigation that follows cancels a plain fetch. It
867
+ * is queued by the browser and delivered regardless, which is the only way to
868
+ * report something that happens as the page is being left.
869
+ *
870
+ * Never throws and never awaits: a banner that cannot report a tap must still
871
+ * open the link.
872
+ */
873
+ reportClick(bannerId, target) {
874
+ try {
875
+ const url = this.client.baseUrl + "/v1/api/banner/click";
876
+ const body = JSON.stringify({ banner_id: bannerId, target });
877
+ const nav = navigator;
878
+ if (typeof nav.sendBeacon === "function") {
879
+ nav.sendBeacon(url, new Blob([body], { type: "application/json" }));
880
+ } else {
881
+ void fetch(url, {
882
+ method: "POST",
883
+ headers: { "Content-Type": "application/json" },
884
+ body,
885
+ keepalive: true
886
+ }).catch(() => {
887
+ });
888
+ }
889
+ } catch {
890
+ }
891
+ }
863
892
  /** Fetch banner config and show the highest-priority banner */
864
893
  async show(options = {}, userId) {
865
894
  const params = {};
@@ -1093,6 +1122,9 @@ var Banners = class {
1093
1122
  cta.className = "tolk-cta";
1094
1123
  cta.href = isSafeUrl(installUrl) ? installUrl : "#";
1095
1124
  cta.textContent = ctaText;
1125
+ cta.addEventListener("click", () => {
1126
+ this.reportClick(banner.id, cta.href);
1127
+ });
1096
1128
  inner.appendChild(cta);
1097
1129
  container.appendChild(inner);
1098
1130
  document.head.appendChild(style);