@blux.ai/web-sdk 2.2.13 → 2.2.15

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.
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ interface Window {
3
+ BluxClient: any;
4
+ }
5
+ }
6
+ export {};
@@ -0,0 +1,147 @@
1
+ import { BluxClient, AddCustomEvent, AddProductDetailViewEvent, AddSectionViewEvent, ItemRecommendation, } from "@blux.ai/web-sdk";
2
+ const applicationId = "APP_ID";
3
+ const bluxApiKey = "API_KEY";
4
+ let bluxClient = null;
5
+ const getBluxClientPromise = BluxClient.init({ applicationId, bluxApiKey });
6
+ getBluxClientPromise.then((v) => {
7
+ bluxClient = v;
8
+ });
9
+ const getBluxClient = async () => {
10
+ if (bluxClient)
11
+ return bluxClient;
12
+ return await getBluxClientPromise;
13
+ };
14
+ async function initAndHandleEvents() {
15
+ try {
16
+ const cafe24Client = CAFE24API.init({
17
+ client_id: "gRSxaEXKARIpFrRcpAEhJB",
18
+ version: "2023-06-01",
19
+ });
20
+ const customerInfo = await new Promise((resolve, reject) => {
21
+ cafe24Client.getCustomerIDInfo((error, data) => {
22
+ if (error) {
23
+ return reject(error);
24
+ }
25
+ resolve(data.id);
26
+ });
27
+ });
28
+ if (customerInfo.member_id) {
29
+ await bluxClient?.signIn(customerInfo.member_id);
30
+ }
31
+ const { pathname: pathName } = window.location;
32
+ if (pathName.startsWith("/product")) {
33
+ const searchParams = new URLSearchParams(window.location.search);
34
+ const productNo = searchParams.get("product_no") ?? pathName.split("/")[3];
35
+ const recommendationId = searchParams.get("recommendationId");
36
+ const prevPage = searchParams.get("prevPage");
37
+ if (productNo && productNo !== null && productNo !== "null") {
38
+ bluxClient?.sendEvent(new AddProductDetailViewEvent({
39
+ item_id: productNo,
40
+ recommendation_id: recommendationId ?? undefined,
41
+ prev_section: prevPage ?? undefined, // 고객사에 prevPage로 전달 요청드렸으나 collectEvents에서는 prev_section 값을 from에 담음
42
+ }));
43
+ }
44
+ }
45
+ else if (pathName.startsWith("/order/orderform.html")) {
46
+ bluxClient?.sendEvent(new AddCustomEvent({
47
+ event_type: "orderform_view",
48
+ event_properties: {},
49
+ }));
50
+ }
51
+ }
52
+ catch (error) {
53
+ console.error("[BluxClient] Error during initialize and event handling.", error);
54
+ }
55
+ }
56
+ (async function handleSectionView() {
57
+ await getBluxClient();
58
+ await initAndHandleEvents();
59
+ const recViewCollectedRecIds = [];
60
+ registerIntersectionObserver();
61
+ const mutationObserver = new MutationObserver(registerIntersectionObserver);
62
+ const mutationObserverConfig = { subtree: true, childList: true };
63
+ mutationObserver.observe(document.body, mutationObserverConfig);
64
+ // Observer will observe every {BLUX_DOM_ELEMENT_TYPE} element
65
+ const BLUX_DOM_ELEMENT_TYPE = "div";
66
+ // which contains {BLUX_CLASSNAME_PREFIX} in its class name
67
+ const BLUX_CLASSNAME_PREFIX = "blux--";
68
+ // Parse section and recommendationId from class name
69
+ // ex) 'blux--homepage--asdfasdfasdf other-class-name1 other-class-name2' => ['homepage', 'asdfasdfasdf']
70
+ const getRecommendationInfo = (element) => {
71
+ const regex = new RegExp(`${BLUX_CLASSNAME_PREFIX}[^\\s]+`);
72
+ const bluxClassName = RegExp(regex).exec(element.className);
73
+ if (!bluxClassName)
74
+ return [null, null];
75
+ const recommendationInfo = bluxClassName[0]
76
+ .replace(BLUX_CLASSNAME_PREFIX, "")
77
+ .split("--");
78
+ if (recommendationInfo.length == 1)
79
+ return ["null", recommendationInfo[0]];
80
+ else if (recommendationInfo.length == 2)
81
+ return recommendationInfo;
82
+ else
83
+ return [null, null];
84
+ };
85
+ function getSection() {
86
+ const pathName = window.location.pathname;
87
+ if (pathName === "/")
88
+ return "homepage";
89
+ if (pathName.startsWith("/product"))
90
+ return "product_detail_page";
91
+ if (pathName.startsWith("/order/basket.html"))
92
+ return "cartadd_page";
93
+ return "null";
94
+ }
95
+ // IntersectionObserver 콜백 함수
96
+ /*
97
+ Intersection 발생 시 AddSectionViewEvent request 전송
98
+ */
99
+ async function sendSectionViewEvent(section, recommendationId) {
100
+ const recommendationViewEvent = new AddSectionViewEvent({
101
+ section: section === "null" ? getSection() : section,
102
+ recommendation_id: recommendationId,
103
+ });
104
+ bluxClient?.sendEvent(recommendationViewEvent);
105
+ }
106
+ // IntersectionObserver 콜백 함수
107
+ function handleIntersection(entries) {
108
+ entries.forEach((entry) => {
109
+ if (entry.isIntersecting) {
110
+ const [section, recommendationId] = getRecommendationInfo(entry.target);
111
+ if (section &&
112
+ recommendationId &&
113
+ recommendationId !== "undefined" &&
114
+ recommendationId !== "" &&
115
+ recommendationId !== "null" &&
116
+ !recViewCollectedRecIds.includes(recommendationId)) {
117
+ sendSectionViewEvent(section, recommendationId);
118
+ recViewCollectedRecIds.push(recommendationId);
119
+ }
120
+ }
121
+ });
122
+ }
123
+ // Make IntersectionObserver to observe targetElements
124
+ // `entry.isIntersecting` is determined based on `threshold`
125
+ function registerIntersectionObserver() {
126
+ const targetElements = Array.from(document.querySelectorAll(`${BLUX_DOM_ELEMENT_TYPE}[class*="${BLUX_CLASSNAME_PREFIX}"]`));
127
+ if (targetElements.length > 0) {
128
+ targetElements.forEach((targetElement) => {
129
+ const observer = new IntersectionObserver(handleIntersection, {
130
+ threshold: 0,
131
+ }); // Set `threshold` to 0 due to infinite scroll
132
+ observer.observe(targetElement);
133
+ });
134
+ }
135
+ }
136
+ })();
137
+ window.BluxClient = {
138
+ getItemRecommendation: async function ({ call_type, limit, offset, item_ids, }) {
139
+ await getBluxClient();
140
+ return await bluxClient?.getItemRecommendation(new ItemRecommendation({
141
+ call_type,
142
+ limit,
143
+ offset,
144
+ item_ids,
145
+ }));
146
+ },
147
+ };
@@ -0,0 +1 @@
1
+ export {};