@hszrdm/create-commerce 0.0.0-stage → 1.0.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/README.md +50 -2
- package/bin/create-commerce.mjs +51 -0
- package/package.json +23 -4
- package/src/cli.mjs +585 -0
- package/templates/backend/.env.example +113 -0
- package/templates/backend/README.md +7 -0
- package/templates/backend/instrumentation.ts +24 -0
- package/templates/backend/medusa-config.ts +11 -0
- package/templates/backend/package.json +42 -0
- package/templates/backend/scripts/prepare-project.mjs +49 -0
- package/templates/backend/src/project/README.md +13 -0
- package/templates/backend/src/project/api/.gitkeep +0 -0
- package/templates/backend/src/project/jobs/.gitkeep +0 -0
- package/templates/backend/src/project/modules/.gitkeep +0 -0
- package/templates/backend/src/project/subscribers/.gitkeep +0 -0
- package/templates/backend/src/project/workflows/.gitkeep +0 -0
- package/templates/backend/src/scripts/core-init.ts +21 -0
- package/templates/backend/src/scripts/create-storefront-key.ts +12 -0
- package/templates/backend/src/scripts/verify-bootstrap.ts +1 -0
- package/templates/backend/tsconfig.json +33 -0
- package/templates/storefront/.env.example +5 -0
- package/templates/storefront/README.md +108 -0
- package/templates/storefront/next-env.d.ts +4 -0
- package/templates/storefront/next.config.ts +8 -0
- package/templates/storefront/package.json +24 -0
- package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +38 -0
- package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +51 -0
- package/templates/storefront/src/app/[countryCode]/products/page.tsx +47 -0
- package/templates/storefront/src/app/account/orders/[id]/page.tsx +8 -0
- package/templates/storefront/src/app/account/page.tsx +11 -0
- package/templates/storefront/src/app/cart/page.tsx +12 -0
- package/templates/storefront/src/app/checkout/page.tsx +15 -0
- package/templates/storefront/src/app/checkout/success/page.tsx +72 -0
- package/templates/storefront/src/app/error.tsx +14 -0
- package/templates/storefront/src/app/globals.css +250 -0
- package/templates/storefront/src/app/layout.tsx +59 -0
- package/templates/storefront/src/app/legal/page.tsx +26 -0
- package/templates/storefront/src/app/loading.tsx +3 -0
- package/templates/storefront/src/app/not-found.tsx +14 -0
- package/templates/storefront/src/app/page.tsx +47 -0
- package/templates/storefront/src/app/reset-password/page.tsx +14 -0
- package/templates/storefront/src/app/robots.txt/route.ts +13 -0
- package/templates/storefront/src/app/sitemap.xml/route.ts +13 -0
- package/templates/storefront/src/components/account-dashboard.tsx +325 -0
- package/templates/storefront/src/components/add-to-cart.tsx +71 -0
- package/templates/storefront/src/components/analytics.tsx +90 -0
- package/templates/storefront/src/components/cart-provider.tsx +236 -0
- package/templates/storefront/src/components/cart-view.tsx +135 -0
- package/templates/storefront/src/components/checkout-flow.tsx +534 -0
- package/templates/storefront/src/components/consent-banner.tsx +131 -0
- package/templates/storefront/src/components/order-detail.tsx +273 -0
- package/templates/storefront/src/components/product-card.tsx +38 -0
- package/templates/storefront/src/components/product-view-tracker.tsx +37 -0
- package/templates/storefront/src/components/reset-password-form.tsx +65 -0
- package/templates/storefront/src/components/storefront-header.tsx +42 -0
- package/templates/storefront/src/components/stripe-payment.tsx +90 -0
- package/templates/storefront/src/lib/core/account.ts +170 -0
- package/templates/storefront/src/lib/core/after-sales.ts +50 -0
- package/templates/storefront/src/lib/core/catalog.ts +56 -0
- package/templates/storefront/src/lib/core/checkout.ts +330 -0
- package/templates/storefront/src/lib/core/client.ts +107 -0
- package/templates/storefront/src/lib/core/commerce.ts +148 -0
- package/templates/storefront/src/lib/core/consent.ts +111 -0
- package/templates/storefront/src/lib/core/legal.ts +33 -0
- package/templates/storefront/src/lib/core/seo.ts +88 -0
- package/templates/storefront/src/lib/core/tracking.ts +584 -0
- package/templates/storefront/src/lib/core/types.ts +123 -0
- package/templates/storefront/tsconfig.json +23 -0
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
import { readConsentPreferences } from "./consent"
|
|
2
|
+
import type { CoreConfig } from "./types"
|
|
3
|
+
|
|
4
|
+
export type EcommerceItem = {
|
|
5
|
+
item_id: string
|
|
6
|
+
item_name: string
|
|
7
|
+
affiliation?: string
|
|
8
|
+
coupon?: string
|
|
9
|
+
discount?: number
|
|
10
|
+
index?: number
|
|
11
|
+
item_brand?: string
|
|
12
|
+
item_category?: string
|
|
13
|
+
item_category2?: string
|
|
14
|
+
item_category3?: string
|
|
15
|
+
item_category4?: string
|
|
16
|
+
item_category5?: string
|
|
17
|
+
item_list_id?: string
|
|
18
|
+
item_list_name?: string
|
|
19
|
+
item_variant?: string
|
|
20
|
+
location_id?: string
|
|
21
|
+
price?: number
|
|
22
|
+
quantity?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type EcommercePayload = {
|
|
26
|
+
currency: string
|
|
27
|
+
value?: number
|
|
28
|
+
coupon?: string
|
|
29
|
+
shipping_tier?: string
|
|
30
|
+
payment_type?: string
|
|
31
|
+
transaction_id?: string
|
|
32
|
+
shipping?: number
|
|
33
|
+
tax?: number
|
|
34
|
+
items: EcommerceItem[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type EcommerceEventName =
|
|
38
|
+
| "view_item"
|
|
39
|
+
| "add_to_cart"
|
|
40
|
+
| "remove_from_cart"
|
|
41
|
+
| "view_cart"
|
|
42
|
+
| "begin_checkout"
|
|
43
|
+
| "add_shipping_info"
|
|
44
|
+
| "add_payment_info"
|
|
45
|
+
| "purchase"
|
|
46
|
+
| "refund"
|
|
47
|
+
|
|
48
|
+
type FirstPartyEventName = EcommerceEventName | "session_start" | "page_view"
|
|
49
|
+
|
|
50
|
+
type FirstPartyExtras = {
|
|
51
|
+
cartId?: string
|
|
52
|
+
orderId?: string
|
|
53
|
+
productId?: string
|
|
54
|
+
productName?: string
|
|
55
|
+
metadata?: Record<string, unknown>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const VISITOR_KEY = "hszrdm:analytics:visitor"
|
|
59
|
+
const SESSION_KEY = "hszrdm:analytics:session"
|
|
60
|
+
const ATTRIBUTION_KEY = "hszrdm:analytics:attribution"
|
|
61
|
+
const SESSION_TIMEOUT_MS = 30 * 60 * 1000
|
|
62
|
+
|
|
63
|
+
export function analyticsTrackingMode(
|
|
64
|
+
analytics: CoreConfig["analytics"],
|
|
65
|
+
): "gtm" | "ga4" | "none" {
|
|
66
|
+
if (analytics.google_tag_manager.enabled && analytics.google_tag_manager.container_id) {
|
|
67
|
+
return "gtm"
|
|
68
|
+
}
|
|
69
|
+
if (analytics.google_analytics.enabled && analytics.google_analytics.measurement_id) {
|
|
70
|
+
return "ga4"
|
|
71
|
+
}
|
|
72
|
+
return "none"
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function trackWebshopPageView() {
|
|
76
|
+
if (typeof window === "undefined") return false
|
|
77
|
+
const path = `${window.location.pathname}${window.location.search}`
|
|
78
|
+
return trackFirstParty("page_view", undefined, {
|
|
79
|
+
metadata: { title: document.title, path },
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function trackEcommerce(
|
|
84
|
+
analytics: CoreConfig["analytics"],
|
|
85
|
+
event: EcommerceEventName,
|
|
86
|
+
ecommerce: EcommercePayload,
|
|
87
|
+
options: {
|
|
88
|
+
dedupeKey?: string
|
|
89
|
+
sessionDedupe?: boolean
|
|
90
|
+
cartId?: string
|
|
91
|
+
orderId?: string
|
|
92
|
+
productId?: string
|
|
93
|
+
productName?: string
|
|
94
|
+
} = {},
|
|
95
|
+
) {
|
|
96
|
+
if (typeof window === "undefined") return false
|
|
97
|
+
|
|
98
|
+
const firstPartySent = trackFirstParty(event, options.dedupeKey, {
|
|
99
|
+
cartId: options.cartId,
|
|
100
|
+
orderId: options.orderId,
|
|
101
|
+
productId: options.productId,
|
|
102
|
+
productName: options.productName,
|
|
103
|
+
metadata: { ecommerce },
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// Google tracking remains consent-gated and optional. The first-party webshop
|
|
107
|
+
// event above is independent from GA4/GTM and powers the Core dashboard.
|
|
108
|
+
if (!readConsentPreferences()?.analytics) return firstPartySent
|
|
109
|
+
|
|
110
|
+
const mode = analyticsTrackingMode(analytics)
|
|
111
|
+
if (mode === "none") return firstPartySent
|
|
112
|
+
|
|
113
|
+
const dedupeStorage = options.sessionDedupe ? window.sessionStorage : window.localStorage
|
|
114
|
+
const dedupeKey = options.dedupeKey
|
|
115
|
+
? `core:analytics:event:${event}:${options.dedupeKey}`
|
|
116
|
+
: null
|
|
117
|
+
if (dedupeKey && dedupeStorage.getItem(dedupeKey) === "sent") return firstPartySent
|
|
118
|
+
|
|
119
|
+
const target = window as typeof window & {
|
|
120
|
+
dataLayer?: Array<Record<string, unknown> | unknown[]>
|
|
121
|
+
gtag?: (...args: unknown[]) => void
|
|
122
|
+
}
|
|
123
|
+
target.dataLayer = target.dataLayer || []
|
|
124
|
+
|
|
125
|
+
let googleSent = false
|
|
126
|
+
if (mode === "gtm") {
|
|
127
|
+
target.dataLayer.push({ ecommerce: null })
|
|
128
|
+
target.dataLayer.push({ event, ecommerce })
|
|
129
|
+
googleSent = true
|
|
130
|
+
} else if (typeof target.gtag === "function") {
|
|
131
|
+
target.gtag("event", event, ecommerce)
|
|
132
|
+
googleSent = true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (googleSent && dedupeKey) dedupeStorage.setItem(dedupeKey, "sent")
|
|
136
|
+
return firstPartySent || googleSent
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function trackViewItem(
|
|
140
|
+
analytics: CoreConfig["analytics"],
|
|
141
|
+
input: {
|
|
142
|
+
item: EcommerceItem
|
|
143
|
+
currency: string
|
|
144
|
+
value?: number
|
|
145
|
+
dedupeKey?: string
|
|
146
|
+
},
|
|
147
|
+
) {
|
|
148
|
+
return trackEcommerce(
|
|
149
|
+
analytics,
|
|
150
|
+
"view_item",
|
|
151
|
+
{
|
|
152
|
+
currency: normalizeCurrency(input.currency),
|
|
153
|
+
value: finite(input.value),
|
|
154
|
+
items: [input.item],
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
dedupeKey: input.dedupeKey || input.item.item_id,
|
|
158
|
+
sessionDedupe: true,
|
|
159
|
+
productId: input.item.item_id,
|
|
160
|
+
productName: input.item.item_name,
|
|
161
|
+
},
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function trackCartEvent(
|
|
166
|
+
analytics: CoreConfig["analytics"],
|
|
167
|
+
event: "view_cart" | "begin_checkout",
|
|
168
|
+
cart: Record<string, unknown>,
|
|
169
|
+
) {
|
|
170
|
+
return trackEcommerce(analytics, event, ecommerceFromCart(cart), {
|
|
171
|
+
cartId: text(cart.id),
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function trackCartLineChange(
|
|
176
|
+
analytics: CoreConfig["analytics"],
|
|
177
|
+
event: "add_to_cart" | "remove_from_cart",
|
|
178
|
+
cart: Record<string, unknown>,
|
|
179
|
+
line: Record<string, unknown>,
|
|
180
|
+
quantity: number,
|
|
181
|
+
) {
|
|
182
|
+
const payload = ecommerceFromCart(cart)
|
|
183
|
+
const item = ecommerceItemFromLine(line, 0, quantity)
|
|
184
|
+
const value = (item.price || 0) * (item.quantity || 1)
|
|
185
|
+
return trackEcommerce(analytics, event, {
|
|
186
|
+
currency: payload.currency,
|
|
187
|
+
value,
|
|
188
|
+
items: [item],
|
|
189
|
+
}, {
|
|
190
|
+
cartId: text(cart.id),
|
|
191
|
+
productId: item.item_id,
|
|
192
|
+
productName: item.item_name,
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function trackShippingInfo(
|
|
197
|
+
analytics: CoreConfig["analytics"],
|
|
198
|
+
cart: Record<string, unknown>,
|
|
199
|
+
shippingTier: string,
|
|
200
|
+
) {
|
|
201
|
+
return trackEcommerce(analytics, "add_shipping_info", {
|
|
202
|
+
...ecommerceFromCart(cart),
|
|
203
|
+
shipping_tier: shippingTier,
|
|
204
|
+
}, { cartId: text(cart.id) })
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function trackPaymentInfo(
|
|
208
|
+
analytics: CoreConfig["analytics"],
|
|
209
|
+
cart: Record<string, unknown>,
|
|
210
|
+
paymentType: string,
|
|
211
|
+
) {
|
|
212
|
+
return trackEcommerce(analytics, "add_payment_info", {
|
|
213
|
+
...ecommerceFromCart(cart),
|
|
214
|
+
payment_type: paymentType,
|
|
215
|
+
}, { cartId: text(cart.id) })
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function trackPurchase(
|
|
219
|
+
analytics: CoreConfig["analytics"],
|
|
220
|
+
order: Record<string, unknown>,
|
|
221
|
+
) {
|
|
222
|
+
const transactionId = text(order.id) || text(order.display_id)
|
|
223
|
+
if (!transactionId) return false
|
|
224
|
+
|
|
225
|
+
const items = lineItems(order).map((line, index) => ecommerceItemFromLine(line, index))
|
|
226
|
+
const currency = normalizeCurrency(text(order.currency_code) || "HUF")
|
|
227
|
+
const value = itemValue(items)
|
|
228
|
+
|
|
229
|
+
return trackEcommerce(
|
|
230
|
+
analytics,
|
|
231
|
+
"purchase",
|
|
232
|
+
{
|
|
233
|
+
currency,
|
|
234
|
+
value,
|
|
235
|
+
transaction_id: transactionId,
|
|
236
|
+
shipping: finite(order.shipping_total),
|
|
237
|
+
tax: finite(order.tax_total),
|
|
238
|
+
coupon: couponFrom(order),
|
|
239
|
+
items,
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
dedupeKey: transactionId,
|
|
243
|
+
orderId: transactionId,
|
|
244
|
+
cartId: text(order.cart_id),
|
|
245
|
+
},
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function trackRefund(
|
|
250
|
+
analytics: CoreConfig["analytics"],
|
|
251
|
+
input: {
|
|
252
|
+
transactionId: string
|
|
253
|
+
currency: string
|
|
254
|
+
value?: number
|
|
255
|
+
shipping?: number
|
|
256
|
+
tax?: number
|
|
257
|
+
items?: EcommerceItem[]
|
|
258
|
+
refundId?: string
|
|
259
|
+
},
|
|
260
|
+
) {
|
|
261
|
+
const dedupeKey = input.refundId
|
|
262
|
+
? `${input.transactionId}:${input.refundId}`
|
|
263
|
+
: `${input.transactionId}:${input.value ?? "full"}`
|
|
264
|
+
|
|
265
|
+
return trackEcommerce(
|
|
266
|
+
analytics,
|
|
267
|
+
"refund",
|
|
268
|
+
{
|
|
269
|
+
transaction_id: input.transactionId,
|
|
270
|
+
currency: normalizeCurrency(input.currency),
|
|
271
|
+
value: finite(input.value),
|
|
272
|
+
shipping: finite(input.shipping),
|
|
273
|
+
tax: finite(input.tax),
|
|
274
|
+
items: input.items || [],
|
|
275
|
+
},
|
|
276
|
+
{ dedupeKey, orderId: input.transactionId },
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function ecommerceFromCart(cart: Record<string, unknown>): EcommercePayload {
|
|
281
|
+
const items = lineItems(cart).map((line, index) => ecommerceItemFromLine(line, index))
|
|
282
|
+
return {
|
|
283
|
+
currency: normalizeCurrency(text(cart.currency_code) || "HUF"),
|
|
284
|
+
value: itemValue(items),
|
|
285
|
+
coupon: couponFrom(cart),
|
|
286
|
+
items,
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function ecommerceItemFromLine(
|
|
291
|
+
line: Record<string, unknown>,
|
|
292
|
+
index = 0,
|
|
293
|
+
quantityOverride?: number,
|
|
294
|
+
): EcommerceItem {
|
|
295
|
+
const quantity = Math.max(1, Math.trunc(quantityOverride ?? number(line.quantity) ?? 1))
|
|
296
|
+
const unitPrice =
|
|
297
|
+
number(line.unit_price) ??
|
|
298
|
+
divide(number(line.subtotal), number(line.quantity)) ??
|
|
299
|
+
divide(number(line.total), number(line.quantity))
|
|
300
|
+
const discount = divide(number(line.discount_total), number(line.quantity))
|
|
301
|
+
|
|
302
|
+
return clean({
|
|
303
|
+
item_id:
|
|
304
|
+
text(line.variant_sku) ||
|
|
305
|
+
text(line.variant_id) ||
|
|
306
|
+
text(line.product_id) ||
|
|
307
|
+
text(line.id) ||
|
|
308
|
+
"unknown-item",
|
|
309
|
+
item_name: text(line.product_title) || text(line.title) || "Termék",
|
|
310
|
+
item_variant: text(line.variant_title),
|
|
311
|
+
item_brand: text(line.product_type),
|
|
312
|
+
price: unitPrice,
|
|
313
|
+
discount,
|
|
314
|
+
quantity,
|
|
315
|
+
index,
|
|
316
|
+
}) as EcommerceItem
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function ecommerceItemFromProduct(input: {
|
|
320
|
+
product: Record<string, unknown>
|
|
321
|
+
variant?: Record<string, unknown> | null
|
|
322
|
+
currency: string
|
|
323
|
+
}): { item: EcommerceItem; currency: string; value?: number } {
|
|
324
|
+
const variant = input.variant || null
|
|
325
|
+
const calculatedPrice = record(variant?.calculated_price)
|
|
326
|
+
const price = number(calculatedPrice?.calculated_amount)
|
|
327
|
+
const item = clean({
|
|
328
|
+
item_id:
|
|
329
|
+
text(variant?.sku) ||
|
|
330
|
+
text(variant?.id) ||
|
|
331
|
+
text(input.product.id) ||
|
|
332
|
+
text(input.product.handle) ||
|
|
333
|
+
"unknown-item",
|
|
334
|
+
item_name: text(input.product.title) || "Termék",
|
|
335
|
+
item_variant: text(variant?.title),
|
|
336
|
+
item_brand: text(input.product.type),
|
|
337
|
+
price,
|
|
338
|
+
quantity: 1,
|
|
339
|
+
}) as EcommerceItem
|
|
340
|
+
|
|
341
|
+
return {
|
|
342
|
+
item,
|
|
343
|
+
currency: normalizeCurrency(
|
|
344
|
+
text(calculatedPrice?.currency_code) || input.currency,
|
|
345
|
+
),
|
|
346
|
+
value: price,
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function trackFirstParty(
|
|
351
|
+
eventName: FirstPartyEventName,
|
|
352
|
+
dedupeKey?: string,
|
|
353
|
+
extras: FirstPartyExtras = {},
|
|
354
|
+
) {
|
|
355
|
+
if (typeof window === "undefined") return false
|
|
356
|
+
const context = firstPartyContext()
|
|
357
|
+
if (!context) return false
|
|
358
|
+
|
|
359
|
+
if (eventName !== "session_start" && context.isNewSession) {
|
|
360
|
+
void postFirstParty({
|
|
361
|
+
event_key: `session_start:${context.sessionId}`,
|
|
362
|
+
event_name: "session_start",
|
|
363
|
+
...baseFirstPartyPayload(context),
|
|
364
|
+
metadata: { new_visitor: context.isNewVisitor },
|
|
365
|
+
})
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const ecommerce = record(extras.metadata?.ecommerce)
|
|
369
|
+
const items = Array.isArray(ecommerce?.items) ? ecommerce.items : []
|
|
370
|
+
const firstItem = items.find(isRecord)
|
|
371
|
+
const globallyDeduped = eventName === "purchase" || eventName === "refund"
|
|
372
|
+
const eventKey = dedupeKey
|
|
373
|
+
? globallyDeduped
|
|
374
|
+
? `${eventName}:${dedupeKey}`
|
|
375
|
+
: `${eventName}:${context.sessionId}:${dedupeKey}`
|
|
376
|
+
: `${eventName}:${context.sessionId}:${randomId()}`
|
|
377
|
+
|
|
378
|
+
void postFirstParty({
|
|
379
|
+
event_key: eventKey,
|
|
380
|
+
event_name: eventName,
|
|
381
|
+
...baseFirstPartyPayload(context),
|
|
382
|
+
cart_id: extras.cartId,
|
|
383
|
+
order_id: extras.orderId,
|
|
384
|
+
product_id: extras.productId || text(firstItem?.item_id),
|
|
385
|
+
product_name: extras.productName || text(firstItem?.item_name),
|
|
386
|
+
currency: text(ecommerce?.currency),
|
|
387
|
+
value: number(ecommerce?.value),
|
|
388
|
+
metadata: extras.metadata,
|
|
389
|
+
})
|
|
390
|
+
return true
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function baseFirstPartyPayload(context: NonNullable<ReturnType<typeof firstPartyContext>>) {
|
|
394
|
+
return {
|
|
395
|
+
session_id: context.sessionId,
|
|
396
|
+
visitor_id: context.visitorId,
|
|
397
|
+
path: `${window.location.pathname}${window.location.search}`,
|
|
398
|
+
referrer: context.attribution.referrer,
|
|
399
|
+
source: context.attribution.source,
|
|
400
|
+
medium: context.attribution.medium,
|
|
401
|
+
campaign: context.attribution.campaign,
|
|
402
|
+
content: context.attribution.content,
|
|
403
|
+
term: context.attribution.term,
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function firstPartyContext() {
|
|
408
|
+
if (typeof window === "undefined") return null
|
|
409
|
+
const now = Date.now()
|
|
410
|
+
|
|
411
|
+
let visitorId = window.localStorage.getItem(VISITOR_KEY) || ""
|
|
412
|
+
const isNewVisitor = !visitorId
|
|
413
|
+
if (!visitorId) {
|
|
414
|
+
visitorId = randomId()
|
|
415
|
+
window.localStorage.setItem(VISITOR_KEY, visitorId)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
let session = readJson<{ id: string; lastActivity: number }>(window.localStorage.getItem(SESSION_KEY))
|
|
419
|
+
const isNewSession = !session?.id || !Number.isFinite(session.lastActivity) || now - session.lastActivity > SESSION_TIMEOUT_MS
|
|
420
|
+
if (isNewSession) {
|
|
421
|
+
session = { id: randomId(), lastActivity: now }
|
|
422
|
+
window.localStorage.setItem(SESSION_KEY, JSON.stringify(session))
|
|
423
|
+
window.sessionStorage.removeItem(ATTRIBUTION_KEY)
|
|
424
|
+
} else if (session) {
|
|
425
|
+
session.lastActivity = now
|
|
426
|
+
window.localStorage.setItem(SESSION_KEY, JSON.stringify(session))
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (!session) return null
|
|
430
|
+
|
|
431
|
+
let attribution = readJson<Attribution>(window.sessionStorage.getItem(ATTRIBUTION_KEY))
|
|
432
|
+
if (!attribution) {
|
|
433
|
+
attribution = captureAttribution()
|
|
434
|
+
window.sessionStorage.setItem(ATTRIBUTION_KEY, JSON.stringify(attribution))
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
visitorId,
|
|
439
|
+
sessionId: session.id,
|
|
440
|
+
attribution,
|
|
441
|
+
isNewSession,
|
|
442
|
+
isNewVisitor,
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
type Attribution = {
|
|
447
|
+
source: string
|
|
448
|
+
medium: string
|
|
449
|
+
campaign?: string
|
|
450
|
+
content?: string
|
|
451
|
+
term?: string
|
|
452
|
+
referrer?: string
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function captureAttribution(): Attribution {
|
|
456
|
+
const params = new URLSearchParams(window.location.search)
|
|
457
|
+
const referrer = safeReferrer(document.referrer)
|
|
458
|
+
const source = params.get("utm_source")?.trim()
|
|
459
|
+
const medium = params.get("utm_medium")?.trim()
|
|
460
|
+
if (source || medium) {
|
|
461
|
+
return clean({
|
|
462
|
+
source: source || "unknown",
|
|
463
|
+
medium: medium || "unknown",
|
|
464
|
+
campaign: params.get("utm_campaign")?.trim() || undefined,
|
|
465
|
+
content: params.get("utm_content")?.trim() || undefined,
|
|
466
|
+
term: params.get("utm_term")?.trim() || undefined,
|
|
467
|
+
referrer,
|
|
468
|
+
}) as Attribution
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (referrer) {
|
|
472
|
+
try {
|
|
473
|
+
const hostname = new URL(referrer).hostname.replace(/^www\./, "")
|
|
474
|
+
if (hostname && hostname !== window.location.hostname.replace(/^www\./, "")) {
|
|
475
|
+
return { source: hostname, medium: "referral", referrer }
|
|
476
|
+
}
|
|
477
|
+
} catch {}
|
|
478
|
+
}
|
|
479
|
+
return { source: "direct", medium: "none", referrer }
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async function postFirstParty(payload: Record<string, unknown>) {
|
|
483
|
+
const backend = String(process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || "").replace(/\/$/, "")
|
|
484
|
+
const publishableKey = String(process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "").trim()
|
|
485
|
+
if (!backend || !publishableKey) return
|
|
486
|
+
const headers: Record<string, string> = {
|
|
487
|
+
"content-type": "application/json",
|
|
488
|
+
"x-publishable-api-key": publishableKey,
|
|
489
|
+
}
|
|
490
|
+
try {
|
|
491
|
+
await fetch(`${backend}/store/analytics`, {
|
|
492
|
+
method: "POST",
|
|
493
|
+
headers,
|
|
494
|
+
credentials: "include",
|
|
495
|
+
keepalive: true,
|
|
496
|
+
body: JSON.stringify(payload),
|
|
497
|
+
})
|
|
498
|
+
} catch {
|
|
499
|
+
// First-party analytics must never break the shopping experience.
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function safeReferrer(value: string) {
|
|
504
|
+
if (!value) return undefined
|
|
505
|
+
try {
|
|
506
|
+
const url = new URL(value)
|
|
507
|
+
return `${url.protocol}//${url.host}${url.pathname}`.slice(0, 1000)
|
|
508
|
+
} catch {
|
|
509
|
+
return undefined
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function randomId() {
|
|
514
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID()
|
|
515
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function readJson<T>(value: string | null): T | null {
|
|
519
|
+
if (!value) return null
|
|
520
|
+
try { return JSON.parse(value) as T } catch { return null }
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function lineItems(source: Record<string, unknown>) {
|
|
524
|
+
return Array.isArray(source.items)
|
|
525
|
+
? source.items.filter(isRecord)
|
|
526
|
+
: []
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function couponFrom(source: Record<string, unknown>) {
|
|
530
|
+
if (!Array.isArray(source.promotions)) return undefined
|
|
531
|
+
const first = source.promotions.find(isRecord)
|
|
532
|
+
return first ? text(first.code) : undefined
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function itemValue(items: EcommerceItem[]) {
|
|
536
|
+
const value = items.reduce(
|
|
537
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
538
|
+
0,
|
|
539
|
+
)
|
|
540
|
+
return Number(value.toFixed(6))
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function normalizeCurrency(value: string) {
|
|
544
|
+
return String(value || "HUF").trim().toUpperCase()
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function finite(value: unknown) {
|
|
548
|
+
const result = number(value)
|
|
549
|
+
return result === undefined || !Number.isFinite(result) ? undefined : result
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function number(value: unknown): number | undefined {
|
|
553
|
+
if (typeof value === "number" && Number.isFinite(value)) return value
|
|
554
|
+
if (typeof value === "string" && value.trim()) {
|
|
555
|
+
const parsed = Number(value)
|
|
556
|
+
if (Number.isFinite(parsed)) return parsed
|
|
557
|
+
}
|
|
558
|
+
return undefined
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function divide(a?: number, b?: number) {
|
|
562
|
+
if (a === undefined || b === undefined || b === 0) return undefined
|
|
563
|
+
return a / b
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function text(value: unknown) {
|
|
567
|
+
if (typeof value === "string" && value.trim()) return value.trim()
|
|
568
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value)
|
|
569
|
+
return undefined
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
573
|
+
return isRecord(value) ? value : undefined
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
577
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value))
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function clean<T extends Record<string, unknown>>(value: T) {
|
|
581
|
+
return Object.fromEntries(
|
|
582
|
+
Object.entries(value).filter(([, item]) => item !== undefined && item !== null),
|
|
583
|
+
)
|
|
584
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
export type EndpointMap = Record<string, string>
|
|
2
|
+
|
|
3
|
+
export type CoreConfig = {
|
|
4
|
+
schema_version: 1
|
|
5
|
+
store: {
|
|
6
|
+
name: string
|
|
7
|
+
slug: string
|
|
8
|
+
locale: string
|
|
9
|
+
default_currency: string
|
|
10
|
+
default_country: string
|
|
11
|
+
support_email: string | null
|
|
12
|
+
}
|
|
13
|
+
features: {
|
|
14
|
+
account: boolean
|
|
15
|
+
legal_documents: boolean
|
|
16
|
+
after_sales: boolean
|
|
17
|
+
payments: boolean
|
|
18
|
+
shipping: boolean
|
|
19
|
+
seo: boolean
|
|
20
|
+
analytics: boolean
|
|
21
|
+
merchant_feed: boolean
|
|
22
|
+
}
|
|
23
|
+
account: {
|
|
24
|
+
enabled: boolean
|
|
25
|
+
managed_by: Record<string, string>
|
|
26
|
+
endpoints: EndpointMap
|
|
27
|
+
}
|
|
28
|
+
payments: {
|
|
29
|
+
stripe: { enabled: boolean; publishable_key: string | null }
|
|
30
|
+
simplepay: { enabled: boolean }
|
|
31
|
+
bank_transfer: { enabled: boolean }
|
|
32
|
+
cash_on_delivery: {
|
|
33
|
+
managed_by: "medusa"
|
|
34
|
+
dynamic: true
|
|
35
|
+
marker: "shipping_method.data.cod"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
shipping: Record<string, { enabled: boolean }>
|
|
39
|
+
after_sales: {
|
|
40
|
+
enabled: boolean
|
|
41
|
+
returns: boolean
|
|
42
|
+
exchanges: boolean
|
|
43
|
+
claims: boolean
|
|
44
|
+
policy_url: string | null
|
|
45
|
+
endpoints: EndpointMap
|
|
46
|
+
}
|
|
47
|
+
legal: {
|
|
48
|
+
enabled: boolean
|
|
49
|
+
authority: "core_legal_documents"
|
|
50
|
+
required_at_checkout_dynamic: true
|
|
51
|
+
documents_endpoint: string
|
|
52
|
+
accept_endpoint: string
|
|
53
|
+
pdf_endpoint: string
|
|
54
|
+
fogyasztobarat: { enabled: boolean; data_id: string | null }
|
|
55
|
+
}
|
|
56
|
+
seo: {
|
|
57
|
+
enabled: boolean
|
|
58
|
+
authority: "core_seo"
|
|
59
|
+
metadata_endpoint: string
|
|
60
|
+
structured_data_endpoint: string
|
|
61
|
+
robots_source_endpoint: string
|
|
62
|
+
sitemap_source_endpoint: string
|
|
63
|
+
storefront_url: string | null
|
|
64
|
+
store_name: string
|
|
65
|
+
organization_name: string
|
|
66
|
+
organization_logo_url: string | null
|
|
67
|
+
product_path_template: string
|
|
68
|
+
category_path_template: string
|
|
69
|
+
default_country_code: string
|
|
70
|
+
allow_indexing: boolean
|
|
71
|
+
search_console: {
|
|
72
|
+
enabled: boolean
|
|
73
|
+
meta_name: "google-site-verification"
|
|
74
|
+
verification_token: string | null
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
analytics: {
|
|
78
|
+
consent_required: true
|
|
79
|
+
google_analytics: { enabled: boolean; measurement_id: string | null }
|
|
80
|
+
google_tag_manager: { enabled: boolean; container_id: string | null }
|
|
81
|
+
}
|
|
82
|
+
marketing: {
|
|
83
|
+
google_merchant: { enabled: boolean; feed_endpoint: string }
|
|
84
|
+
}
|
|
85
|
+
checkout: {
|
|
86
|
+
payment_availability: "medusa_region"
|
|
87
|
+
shipping_availability: "medusa_cart"
|
|
88
|
+
totals_authority: "medusa_cart"
|
|
89
|
+
promotions_authority: "medusa_cart"
|
|
90
|
+
managed_by: Record<string, string>
|
|
91
|
+
endpoints: EndpointMap
|
|
92
|
+
completion: Record<string, string>
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type SeoContractResponse = {
|
|
97
|
+
type: "product" | "category" | "page"
|
|
98
|
+
resource: { id: string; handle?: string | null; path: string }
|
|
99
|
+
customized: boolean
|
|
100
|
+
seo: Record<string, unknown>
|
|
101
|
+
contract: {
|
|
102
|
+
title: string | null
|
|
103
|
+
description: string | null
|
|
104
|
+
canonical: string | null
|
|
105
|
+
robots: { index: boolean; follow: boolean }
|
|
106
|
+
openGraph: {
|
|
107
|
+
title: string | null
|
|
108
|
+
description: string | null
|
|
109
|
+
images: string[]
|
|
110
|
+
url: string | null
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type LegalDocument = {
|
|
116
|
+
key: string
|
|
117
|
+
title?: string
|
|
118
|
+
requiredAtCheckout?: boolean
|
|
119
|
+
required_at_checkout?: boolean
|
|
120
|
+
pdfEndpoint?: string | null
|
|
121
|
+
acceptEndpoint?: string | null
|
|
122
|
+
[key: string]: unknown
|
|
123
|
+
}
|