@misiki/gocommerce-connector 0.16.0 → 0.20.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.
@@ -163,6 +163,17 @@ function currentMe() {
163
163
  return null;
164
164
  }
165
165
  }
166
+ function metaBrowser() {
167
+ const fbp = readCookie("_fbp");
168
+ if (!fbp || typeof window.fbq !== "function") return void 0;
169
+ const fbc = readCookie("_fbc");
170
+ return {
171
+ fbp,
172
+ ...fbc ? { fbc } : {},
173
+ client_user_agent: window.navigator.userAgent,
174
+ event_source_url: window.location.origin + window.location.pathname
175
+ };
176
+ }
166
177
  function storedCartId() {
167
178
  const v = readLocal("cart_id");
168
179
  if (!v || v === "undefined" || v === "null") return null;
@@ -2188,11 +2199,13 @@ var CheckoutService = class extends BaseService {
2188
2199
  ...extra
2189
2200
  };
2190
2201
  const serialised = JSON.stringify(body);
2191
- const key = extras.attempt?.body === `${code}:${serialised}` ? extras.attempt.key : newKey();
2192
- saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}` } });
2202
+ const retry = extras.attempt?.body === `${code}:${serialised}`;
2203
+ const key = retry ? extras.attempt.key : newKey();
2204
+ const meta = retry ? extras.attempt?.meta : metaBrowser();
2205
+ saveCartExtras(id2, { attempt: { key, body: `${code}:${serialised}`, ...meta ? { meta } : {} } });
2193
2206
  const result = await this.data(`/api/checkout/${encodeURIComponent(code)}`, {
2194
2207
  method: "POST",
2195
- body,
2208
+ body: meta ? { ...body, metadata: { ...extra.metadata, meta_capi: meta } } : body,
2196
2209
  headers: { "Idempotency-Key": key }
2197
2210
  });
2198
2211
  if (result.order.access_token) {
@@ -2854,6 +2867,71 @@ var ReviewService = class extends BaseService {
2854
2867
  };
2855
2868
  var reviewService = new ReviewService();
2856
2869
 
2870
+ // src/services/google-review-service.ts
2871
+ var PLUGIN = "google-reviews";
2872
+ var GoogleReviewService = class extends BaseService {
2873
+ facts = new StoreFacts(this._fetch);
2874
+ /**
2875
+ * Google's rating and its most relevant reviews, read live. Google's terms
2876
+ * forbid keeping them, so nothing here caches them either, and every call
2877
+ * is one the store pays Google for: call it when the section is in view.
2878
+ */
2879
+ async place() {
2880
+ if (!await this.switchOn("show_place")) return null;
2881
+ const p = await this.orNull(() => this.data("/x/google-reviews/place"));
2882
+ if (!p?.user_rating_count) return null;
2883
+ return {
2884
+ rating: p.rating,
2885
+ count: p.user_rating_count,
2886
+ mapsUrl: p.google_maps_uri ?? "",
2887
+ reviews: (p.reviews ?? []).map((r) => ({
2888
+ author: r.author?.name ?? "",
2889
+ authorUrl: r.author?.uri ?? "",
2890
+ authorPhoto: r.author?.photo_uri ?? "",
2891
+ rating: r.rating,
2892
+ text: r.text ?? "",
2893
+ language: r.language ?? "",
2894
+ when: r.relative_time ?? "",
2895
+ visited: r.visit_date ?? "",
2896
+ url: r.google_maps_uri ?? "",
2897
+ reportUrl: r.flag_uri ?? ""
2898
+ }))
2899
+ };
2900
+ }
2901
+ /**
2902
+ * What Google Customer Reviews' opt-in is rendered with, for an order placed
2903
+ * in this browser: its access token is kept here, and nowhere else can ask.
2904
+ * Every field is the order's own; the keys are Google's, to pass unchanged
2905
+ * to `gapi.surveyoptin.render`.
2906
+ */
2907
+ async survey(orderNumber) {
2908
+ const kept = orderNumber ? placedOrder(orderNumber) : void 0;
2909
+ if (!kept || !await this.switchOn("customer_reviews")) return null;
2910
+ return this.orNull(
2911
+ () => this.data(`/x/google-reviews/survey/${encodeURIComponent(orderNumber)}`, {
2912
+ query: { token: kept.token }
2913
+ })
2914
+ );
2915
+ }
2916
+ async switchOn(setting) {
2917
+ try {
2918
+ return (await this.facts.plugins()).get(PLUGIN)?.settings?.[setting] === true;
2919
+ } catch {
2920
+ return false;
2921
+ }
2922
+ }
2923
+ /** Off, used up for the day, or refused by Google: nothing to show. Anything else still throws. */
2924
+ async orNull(call) {
2925
+ try {
2926
+ return await call();
2927
+ } catch (err) {
2928
+ if (err instanceof GoCommerceError && [404, 429, 502].includes(err.status)) return null;
2929
+ throw err;
2930
+ }
2931
+ }
2932
+ };
2933
+ var googleReviewService = new GoogleReviewService();
2934
+
2857
2935
  // src/services/menu-service.ts
2858
2936
  function linkFor(item) {
2859
2937
  const t = item.target ?? "";
@@ -3075,9 +3153,21 @@ var StoreService = class extends BaseService {
3075
3153
  ...basePlugins.isProductReviewsAndRatings,
3076
3154
  ...flag(plugins.has("product-reviews"))
3077
3155
  },
3156
+ // Google Customer Reviews (ext/google-reviews): the confirmation page's survey offer,
3157
+ // whose fields the engine builds from the order, and Google's store widget, which
3158
+ // belongs to the same programme and so needs the survey on.
3159
+ googleReviewsOptIn: {
3160
+ ...basePlugins.googleReviewsOptIn,
3161
+ ...flag(googleSetting(plugins, "customer_reviews") === true)
3162
+ },
3163
+ googleStoreWidget: storeWidget(plugins),
3078
3164
  // The footer's sign-up box posts to ext/newsletter, and the theme editor's footer
3079
3165
  // switch (D131) can only show it while that is on.
3080
3166
  newsletter: { ...basePlugins.newsletter, ...flag(plugins.has("newsletter")) },
3167
+ // The ad and analytics tags (D160). The engine decides, not the static record:
3168
+ // one storefront serves every store, and one store's pixel must never
3169
+ // load on another's pages.
3170
+ ...adTags(plugins),
3081
3171
  // Discount codes are core: every GoCommerce store takes them, so
3082
3172
  // the code box is shown whatever the static record says.
3083
3173
  isDiscountCoupons: { ...basePlugins.isDiscountCoupons, active: true },
@@ -3103,6 +3193,22 @@ var StoreService = class extends BaseService {
3103
3193
  }
3104
3194
  };
3105
3195
  var storeService = new StoreService();
3196
+ var AD_TAGS = [
3197
+ ["meta-pixel", "pixel_id", "facebookPixel"],
3198
+ ["microsoft-clarity", "project_id", "microsoftClarity"],
3199
+ ["hotjar", "site_id", "hotjar"],
3200
+ ["tiktok-pixel", "pixel_id", "tiktokPixel"],
3201
+ ["pinterest-tag", "tag_id", "pinterestTag"],
3202
+ ["snapchat-pixel", "pixel_id", "snapchatPixel"]
3203
+ ];
3204
+ function adTags(plugins) {
3205
+ return Object.fromEntries(
3206
+ AD_TAGS.map(([key, field, name]) => {
3207
+ const id2 = String(plugins.get(key)?.settings?.[field] ?? "").trim();
3208
+ return [name, id2 ? { active: true, id: id2 } : { active: false }];
3209
+ })
3210
+ );
3211
+ }
3106
3212
  function deliverable(base, ships) {
3107
3213
  if (ships.everywhere || !ships.countries.length) return {};
3108
3214
  const known = new Map((base.countries ?? []).map((c) => [String(c.code).toUpperCase(), c]));
@@ -3117,6 +3223,12 @@ function regionName(code) {
3117
3223
  return code;
3118
3224
  }
3119
3225
  }
3226
+ var googleSetting = (plugins, key) => plugins.get("google-reviews")?.settings?.[key];
3227
+ function storeWidget(plugins) {
3228
+ const where = googleSetting(plugins, "store_widget");
3229
+ if (googleSetting(plugins, "customer_reviews") !== true || !where) return { active: false };
3230
+ return { active: true, position: where === "Bottom left" ? "LEFT_BOTTOM" : "RIGHT_BOTTOM" };
3231
+ }
3120
3232
  var LOGIN_TYPES = { phone_otp: "PHONE", email_or_phone: "BOTH", email_password: "EMAIL" };
3121
3233
 
3122
3234
  // src/services/page-service.ts
@@ -3822,6 +3934,7 @@ exports.EnquiryService = EnquiryService;
3822
3934
  exports.FaqService = FaqService;
3823
3935
  exports.FeedbackService = FeedbackService;
3824
3936
  exports.GalleryService = GalleryService;
3937
+ exports.GoogleReviewService = GoogleReviewService;
3825
3938
  exports.HomeService = HomeService;
3826
3939
  exports.InitService = InitService;
3827
3940
  exports.LayoutService = LayoutService;
@@ -3876,6 +3989,7 @@ exports.enquiryService = enquiryService;
3876
3989
  exports.faqService = faqService;
3877
3990
  exports.feedbackService = feedbackService;
3878
3991
  exports.galleryService = galleryService;
3992
+ exports.googleReviewService = googleReviewService;
3879
3993
  exports.homeService = homeService;
3880
3994
  exports.initService = initService;
3881
3995
  exports.layoutService = layoutService;