@shoppexio/storefront 1.0.82 → 1.0.84

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.84
4
+
5
+ - `touchStorefrontPresence()` sends `event_type: "presence"`. The server refreshes the live session but no longer counts the ping as a shop view, so an idle tab with `sx-online-users` (or any theme that touches presence on a timer) stops adding a view every few minutes.
6
+ - New `trackCartActivity(cartValue, itemCount)`: reports a cart change for the live visitor map as a presence ping, without counting a page view. A cart state that any tab of the shop already reported is not sent again, so the cross-tab `storage` echo of one change is not a second ping.
7
+ - On code storefronts (platform page tracker active), `trackPageView(cartValue, itemCount)` sends the cart as cart activity instead of a second counted view.
8
+ - `trackPageView()` is otherwise unchanged: one call counts one view.
9
+
3
10
  ## 1.0.82
4
11
 
5
12
  - Page views and presence send the page path (query and fragment dropped, long paths capped), so the dashboard can show the top live page.
package/README.md CHANGED
@@ -151,6 +151,8 @@ await refreshLiveUsers();
151
151
  setInterval(refreshLiveUsers, 30_000);
152
152
  ```
153
153
 
154
+ `touchStorefrontPresence()` keeps the visitor online but never counts as a page view, so it is safe on a timer. Report page views with `trackPageView()` (once per page load or navigation) and later cart changes with `trackCartActivity(cartValue, itemCount)`, which updates the live visitor map without counting a view.
155
+
154
156
  ## Not this package
155
157
 
156
158
  If you want the authenticated Developer API wrapper for backend integrations, use `@shoppexio/sdk`. Do not ship `@shoppexio/sdk` in a browser bundle; use this Storefront SDK for headless storefronts.
package/dist/index.cjs CHANGED
@@ -109,6 +109,7 @@ __export(src_exports, {
109
109
  subscriptionBillingHistory: () => subscriptionBillingHistory,
110
110
  ticket: () => ticket,
111
111
  touchStorefrontPresence: () => touchStorefrontPresence,
112
+ trackCartActivity: () => trackCartActivity,
112
113
  trackPageView: () => trackPageView,
113
114
  updateAvatar: () => updateAvatar,
114
115
  updateEmailPreferences: () => updateEmailPreferences,
@@ -26634,13 +26635,27 @@ function isPlatformPageTrackerActive() {
26634
26635
  }
26635
26636
 
26636
26637
  // ../sdk/src/modules/analytics.ts
26638
+ var REPORTED_CART_STORAGE_PREFIX = "reported_cart_";
26639
+ function reportedCartKey(storeSlug) {
26640
+ return `${REPORTED_CART_STORAGE_PREFIX}${storeSlug}`;
26641
+ }
26642
+ function cartSnapshot(cartValue, itemCount) {
26643
+ return `${cartValue ?? ""}|${itemCount ?? ""}`;
26644
+ }
26645
+ function rememberReportedCart(storeSlug, cartValue, itemCount) {
26646
+ setItem(reportedCartKey(storeSlug), cartSnapshot(cartValue, itemCount));
26647
+ }
26637
26648
  async function trackPageView(cartValue, itemCount) {
26638
26649
  if (!isInitialized()) return;
26639
26650
  if (typeof document === "undefined") return;
26640
26651
  const carriesCart = cartValue !== void 0 || itemCount !== void 0;
26641
- if (isPlatformPageTrackerActive() && !carriesCart) return;
26652
+ if (isPlatformPageTrackerActive()) {
26653
+ if (carriesCart) await sendCartActivity(cartValue ?? 0, itemCount ?? 0);
26654
+ return;
26655
+ }
26642
26656
  const config2 = getConfig();
26643
26657
  const connectionId = getStorefrontConnectionId(config2.storeSlug);
26658
+ if (carriesCart) rememberReportedCart(config2.storeSlug, cartValue, itemCount);
26644
26659
  try {
26645
26660
  const endpoint = buildEndpoint("/v1/storefront/shops/:storeSlug/ping", {
26646
26661
  storeSlug: config2.storeSlug
@@ -26660,6 +26675,33 @@ async function trackPageView(cartValue, itemCount) {
26660
26675
  } catch {
26661
26676
  }
26662
26677
  }
26678
+ async function trackCartActivity(cartValue, itemCount) {
26679
+ if (!isInitialized()) return;
26680
+ if (typeof document === "undefined") return;
26681
+ if (getItem(reportedCartKey(getConfig().storeSlug)) === cartSnapshot(cartValue, itemCount)) return;
26682
+ await sendCartActivity(cartValue, itemCount);
26683
+ }
26684
+ async function sendCartActivity(cartValue, itemCount) {
26685
+ const config2 = getConfig();
26686
+ rememberReportedCart(config2.storeSlug, cartValue, itemCount);
26687
+ try {
26688
+ const endpoint = buildEndpoint("/v1/storefront/shops/:storeSlug/ping", {
26689
+ storeSlug: config2.storeSlug
26690
+ });
26691
+ await fetch(`${config2.apiBaseUrl}${endpoint}`, {
26692
+ method: "POST",
26693
+ headers: { "Content-Type": "application/json" },
26694
+ body: JSON.stringify({
26695
+ event_type: "presence",
26696
+ cart_value: cartValue,
26697
+ item_count: itemCount,
26698
+ connection_id: getStorefrontConnectionId(config2.storeSlug) ?? void 0,
26699
+ path: resolveStorefrontPagePath()
26700
+ })
26701
+ });
26702
+ } catch {
26703
+ }
26704
+ }
26663
26705
 
26664
26706
  // ../sdk/src/modules/presence.ts
26665
26707
  async function getStorefrontRecentSales() {
@@ -26710,6 +26752,7 @@ async function touchStorefrontPresence() {
26710
26752
  return post(
26711
26753
  endpoint,
26712
26754
  {
26755
+ event_type: "presence",
26713
26756
  referer: document.referrer || void 0,
26714
26757
  connection_id: getStorefrontConnectionId(config2.storeSlug) ?? void 0,
26715
26758
  path: resolveStorefrontPagePath(),
@@ -26929,6 +26972,7 @@ var shoppex = {
26929
26972
  getMenuSlotTitles,
26930
26973
  // Analytics
26931
26974
  trackPageView,
26975
+ trackCartActivity,
26932
26976
  getStorefrontOnlineUsers,
26933
26977
  getStorefrontRecentSales,
26934
26978
  touchStorefrontPresence,
@@ -27042,6 +27086,7 @@ if (typeof window !== "undefined") {
27042
27086
  subscriptionBillingHistory,
27043
27087
  ticket,
27044
27088
  touchStorefrontPresence,
27089
+ trackCartActivity,
27045
27090
  trackPageView,
27046
27091
  updateAvatar,
27047
27092
  updateEmailPreferences,