medusa-storefront-analytics 1.2.0 → 1.4.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/api/meta-event.d.ts +19 -0
- package/dist/index.d.ts +2 -0
- package/dist/ui-library.js +4861 -290
- package/dist/ui-library.umd.cjs +41 -1
- package/package.json +66 -39
- package/src/api/meta-event.ts +88 -0
- package/src/core/compose.ts +24 -0
- package/src/core/noop-adapter.ts +8 -0
- package/src/core/types.ts +35 -0
- package/src/ecommerce/ga4-ecommerce.ts +261 -0
- package/src/events/forms.ts +34 -0
- package/src/events/meta-events.ts +31 -0
- package/src/events/reviews.ts +25 -0
- package/src/index.ts +74 -0
- package/src/providers/gtm.ts +97 -0
- package/src/providers/meta-pixel-guard.ts +96 -0
- package/src/providers/meta-pixel.ts +77 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Meta Pixel standard events — explicit fbq + dedicated dataLayer pushes.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
META_STANDARD_EVENTS,
|
|
7
|
+
type EcommercePayload,
|
|
8
|
+
type MetaStandardEventName,
|
|
9
|
+
} from "../events/meta-events";
|
|
10
|
+
import { META_EXPLICIT_KEY } from "./meta-pixel-guard";
|
|
11
|
+
|
|
12
|
+
export { META_STANDARD_EVENTS, type MetaStandardEventName, type EcommercePayload };
|
|
13
|
+
|
|
14
|
+
export const META_PIXEL_DATALAYER_EVENT = "meta_pixel_track";
|
|
15
|
+
|
|
16
|
+
type FbqFn = (...args: unknown[]) => void;
|
|
17
|
+
|
|
18
|
+
function normalizeCurrency(currency?: string) {
|
|
19
|
+
return (currency ?? "INR").toUpperCase();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function buildMetaParams(payload: EcommercePayload) {
|
|
23
|
+
const items = payload.items ?? [];
|
|
24
|
+
const numItems = items.reduce((sum, i) => sum + (i.quantity ?? 1), 0);
|
|
25
|
+
return {
|
|
26
|
+
currency: normalizeCurrency(payload.currency),
|
|
27
|
+
value: payload.value,
|
|
28
|
+
content_ids: items.map((i) => String(i.item_id)),
|
|
29
|
+
content_type: "product",
|
|
30
|
+
contents: items.map((i) => ({
|
|
31
|
+
id: String(i.item_id),
|
|
32
|
+
quantity: i.quantity ?? 1,
|
|
33
|
+
item_price: i.price ?? 0,
|
|
34
|
+
})),
|
|
35
|
+
num_items: numItems,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function pushMetaDataLayer(metaEvent: MetaStandardEventName, params: Record<string, unknown>) {
|
|
40
|
+
if (typeof window === "undefined") return;
|
|
41
|
+
const w = window as Window & { dataLayer?: unknown[] };
|
|
42
|
+
w.dataLayer = w.dataLayer || [];
|
|
43
|
+
w.dataLayer.push({
|
|
44
|
+
event: META_PIXEL_DATALAYER_EVENT,
|
|
45
|
+
meta_event: metaEvent,
|
|
46
|
+
[META_EXPLICIT_KEY]: true,
|
|
47
|
+
...params,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function fireMetaPixelEvent(
|
|
52
|
+
metaEvent: MetaStandardEventName,
|
|
53
|
+
params: Record<string, unknown> = {}
|
|
54
|
+
) {
|
|
55
|
+
pushMetaDataLayer(metaEvent, params);
|
|
56
|
+
|
|
57
|
+
if (typeof window === "undefined") return;
|
|
58
|
+
const fbq = (window as Window & { fbq?: FbqFn }).fbq;
|
|
59
|
+
if (typeof fbq === "function") {
|
|
60
|
+
fbq("track", metaEvent, { ...params, [META_EXPLICIT_KEY]: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function fireMetaEcommerceEvent(
|
|
65
|
+
metaEvent: MetaStandardEventName,
|
|
66
|
+
payload: EcommercePayload,
|
|
67
|
+
extra?: Record<string, unknown>
|
|
68
|
+
) {
|
|
69
|
+
fireMetaPixelEvent(metaEvent, { ...buildMetaParams(payload), ...extra });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function fireMetaPurchase(payload: EcommercePayload & { transaction_id: string }) {
|
|
73
|
+
const { transaction_id, ...ecommerce } = payload;
|
|
74
|
+
fireMetaEcommerceEvent(META_STANDARD_EVENTS.Purchase, ecommerce, {
|
|
75
|
+
order_id: transaction_id,
|
|
76
|
+
});
|
|
77
|
+
}
|