@gofynd/fdk-client-javascript 1.4.9 → 1.4.10

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 CHANGED
@@ -127,9 +127,9 @@ getData();
127
127
 
128
128
  ---
129
129
 
130
- ### Sample Usage - Fetch function.
130
+ ### Sample Usage - Request function.
131
131
 
132
- The fetch function allows you to make custom API requests with ease. It is available on both `platform` and `application` client.
132
+ The request function allows you to make custom API requests with ease. It is available on both `platform` and `application` client.
133
133
 
134
134
  ```javascript
135
135
 
@@ -140,7 +140,7 @@ let auditLog = await client.request({
140
140
 
141
141
  ```
142
142
 
143
- The `fetch` function accepts an object with the following possible keys:
143
+ The `request` function accepts an object with the following possible keys:
144
144
 
145
145
  - **method** (string): The HTTP method to use (e.g., 'GET', 'POST', 'PUT', 'DELETE').
146
146
  - **url** (string): The URL endpoint for the request.
@@ -237,7 +237,7 @@ console.log("Active Theme: ", response.information.name);
237
237
  The above code will log the curl command in the console
238
238
 
239
239
  ```bash
240
- curl --request GET "https://api.fynd.com/service/application/theme/v1.0/applied-theme" --header 'authorization: Bearer <authorization-token>' --header 'x-fp-sdk-version: 1.4.9' --header 'x-fp-date: 20230222T115108Z' --header 'x-fp-signature: v1.1:1e3ab3b02b5bc626e3c32a37ee844266ade02bbcbaafc28fc7a0e46a76a7a1a8'
240
+ curl --request GET "https://api.fynd.com/service/application/theme/v1.0/applied-theme" --header 'authorization: Bearer <authorization-token>' --header 'x-fp-sdk-version: 1.4.10' --header 'x-fp-date: 20230222T115108Z' --header 'x-fp-signature: v1.1:1e3ab3b02b5bc626e3c32a37ee844266ade02bbcbaafc28fc7a0e46a76a7a1a8'
241
241
  Active Theme: Emerge
242
242
  ```
243
243
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gofynd/fdk-client-javascript",
3
- "version": "1.4.9",
3
+ "version": "1.4.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,21 +30,59 @@ const fetchPositionAndQuery = function (utm_params) {
30
30
  position: null,
31
31
  };
32
32
  }
33
- const decodedUri = utm_params.utm_content
34
- ? atob(utm_params.utm_content)
35
- : null;
36
- if (decodedUri && utm_params.utm_medium === "search") {
37
- const parts = decodedUri.split(":::");
38
- return {
39
- query: parts[0] || null,
40
- position: parts[1] || null,
41
- };
33
+
34
+ if (utm_params.utm_medium === "search") {
35
+ try {
36
+ const decodedUri = utm_params.utm_content
37
+ ? atob(utm_params.utm_content)
38
+ : null;
39
+ if (decodedUri) {
40
+ const parts = decodedUri.split(":::");
41
+ return {
42
+ query: parts[0] || null,
43
+ position: parts[1] || null,
44
+ };
45
+ }
46
+ } catch (err) {
47
+ return {
48
+ query: null,
49
+ position: null,
50
+ };
51
+ }
42
52
  }
43
53
  return {
44
54
  query: null,
45
55
  position: null,
46
56
  };
47
57
  };
58
+ /**
59
+ * Safely extracts company details from window object
60
+ *
61
+ * @author Hitendra Singh
62
+ * @returns {Object} - Company_id, company_mode and company_created_on
63
+ */
64
+ const fetchCompanyDetails = function () {
65
+ //getting the current date in UTC
66
+ const now = new Date();
67
+ const formattedDate = now.toISOString();
68
+ const application = sg(() => window.config.application, null);
69
+ if (application && Object.keys(application).length) {
70
+ return {
71
+ company_id: sg(() => application.company_id, 0),
72
+ company_mode: sg(() => application.mode, "live"),
73
+ company_created_on: sg(
74
+ () => application.company_created_on,
75
+ formattedDate
76
+ ),
77
+ };
78
+ }
79
+ return {
80
+ company_id: 0,
81
+ company_mode: "live",
82
+ company_created_on: formattedDate,
83
+ };
84
+ };
85
+
48
86
  if (typeof window != "undefined") {
49
87
  window.FPI.event.on("user.login", (eventData) => {
50
88
  Logger({ level: "DEBUG", message: eventData });
@@ -54,6 +92,7 @@ if (typeof window != "undefined") {
54
92
  phone: sg(() => eventData["phone_number"]),
55
93
  login_value: sg(() => eventData["login_value"]),
56
94
  method: sg(() => eventData["method"]),
95
+ ...fetchCompanyDetails(),
57
96
  }).catch((err) => {
58
97
  Logger({ level: "ERROR", message: err });
59
98
  });
@@ -66,6 +105,7 @@ if (typeof window != "undefined") {
66
105
  email: sg(() => eventData["email"]),
67
106
  phone: sg(() => eventData["phone_number"]),
68
107
  method: sg(() => eventData["method"]),
108
+ ...fetchCompanyDetails(),
69
109
  }).catch((err) => {
70
110
  Logger({ level: "ERROR", message: err });
71
111
  });
@@ -73,11 +113,12 @@ if (typeof window != "undefined") {
73
113
 
74
114
  window.FPI.event.on("user.logout", (eventData) => {
75
115
  Logger({ level: "DEBUG", message: eventData });
76
- Clickstream.sendEvent("user_logout", { event_type: "identity" }).catch(
77
- (err) => {
78
- Logger({ level: "ERROR", message: err });
79
- }
80
- );
116
+ Clickstream.sendEvent("user_logout", {
117
+ event_type: "identity",
118
+ ...fetchCompanyDetails(),
119
+ }).catch((err) => {
120
+ Logger({ level: "ERROR", message: err });
121
+ });
81
122
  Clickstream.reset().catch((err) => {
82
123
  Logger({ level: "ERROR", message: err });
83
124
  });
@@ -98,6 +139,7 @@ if (typeof window != "undefined") {
98
139
  currency: sg(() => eventData.item.price.effective.currency_code),
99
140
  value: "TODO",
100
141
  source_url: "TODO",
142
+ ...fetchCompanyDetails(),
101
143
  };
102
144
  const queryResult = fetchPositionAndQuery(eventData.utm_params);
103
145
  Clickstream.sendEvent("product_wishlist_add", {
@@ -127,6 +169,7 @@ if (typeof window != "undefined") {
127
169
  currency: sg(() => eventData.item.price.effective.currency_code),
128
170
  value: "TODO",
129
171
  source_url: "TODO",
172
+ ...fetchCompanyDetails(),
130
173
  })
131
174
  .then((resp) => {
132
175
  Logger({ level: "DEBUG", message: "Click event sent" });
@@ -153,6 +196,7 @@ if (typeof window != "undefined") {
153
196
  position: null,
154
197
  query: null,
155
198
  value: "TODO",
199
+ ...fetchCompanyDetails(),
156
200
  };
157
201
  const queryResult = fetchPositionAndQuery(eventData.utm_params);
158
202
  Clickstream.sendEvent("add_to_cart", { ...payload, ...queryResult })
@@ -179,6 +223,7 @@ if (typeof window != "undefined") {
179
223
  source_url: "TODO",
180
224
  position: null,
181
225
  value: "TODO",
226
+ ...fetchCompanyDetails(),
182
227
  })
183
228
  .then((resp) => {
184
229
  Logger({ level: "DEBUG", message: "Click event sent" });
@@ -193,6 +238,7 @@ if (typeof window != "undefined") {
193
238
  Clickstream.sendEvent("order_complete", {
194
239
  event_type: "conversion",
195
240
  ...eventData,
241
+ ...fetchCompanyDetails(),
196
242
  })
197
243
  .then((resp) => {
198
244
  Logger({ level: "DEBUG", message: "Click event sent" });
@@ -207,6 +253,7 @@ if (typeof window != "undefined") {
207
253
  Clickstream.sendEvent("order_refunded", {
208
254
  event_type: "conversion",
209
255
  ...eventData,
256
+ ...fetchCompanyDetails(),
210
257
  })
211
258
  .then((resp) => {
212
259
  Logger({ level: "DEBUG", message: "Click event sent" });
@@ -239,6 +286,40 @@ if (typeof window != "undefined") {
239
286
  article_id: sg(() => p.article["uid"]),
240
287
  };
241
288
  }),
289
+ ...fetchCompanyDetails(),
290
+ })
291
+ .then((resp) => {
292
+ Logger({ level: "DEBUG", message: "Click event sent" });
293
+ })
294
+ .catch((err) => {
295
+ Logger({ level: "ERROR", message: err });
296
+ });
297
+ });
298
+ window.FPI.event.on("order.checkedout", (eventData) => {
299
+ Logger({ level: "DEBUG", message: eventData });
300
+ Clickstream.sendEvent("order_checkedout", {
301
+ event_type: "conversion",
302
+ cart_id: eventData.cart_id,
303
+ cart_total: sg(() => eventData.breakup_values_raw["mrp_total"]),
304
+ item_total: sg(() => eventData.products.length),
305
+ shipping: sg(() => eventData.breakup_values_raw["delivery_charge"]),
306
+ tax: sg(() => eventData.breakup_values_raw["gst_charges"]),
307
+ order_total: sg(() => eventData.breakup_values_raw["total"]),
308
+ currency: sg(() => eventData.products[0]["price"]["currency_code"]),
309
+ order_id: sg(() => eventData.order_id),
310
+ products: eventData.products.map((p) => {
311
+ return {
312
+ product_id: p.uid,
313
+ l3_category: sg(() => p.category["name"]),
314
+ l1_category: "TODO",
315
+ quantity: sg(() => p.quantity["current"]),
316
+ price: sg(() => p.price["marked"]),
317
+ value: sg(() => p.price["effective"]),
318
+ currency: sg(() => p.price["currency_code"]),
319
+ article_id: sg(() => p.article["uid"]),
320
+ };
321
+ }),
322
+ ...fetchCompanyDetails(),
242
323
  })
243
324
  .then((resp) => {
244
325
  Logger({ level: "DEBUG", message: "Click event sent" });
@@ -266,6 +347,7 @@ if (typeof window != "undefined") {
266
347
  }),
267
348
 
268
349
  item_total: sg(() => eventData["page"]["item_total"]),
350
+ ...fetchCompanyDetails(),
269
351
  });
270
352
  });
271
353
 
@@ -286,7 +368,11 @@ if (typeof window != "undefined") {
286
368
  query: null,
287
369
  };
288
370
  const queryResult = fetchPositionAndQuery(eventData.utm_params);
289
- Clickstream.sendEvent("product_view", { ...payload, ...queryResult })
371
+ Clickstream.sendEvent("product_view", {
372
+ ...payload,
373
+ ...queryResult,
374
+ ...fetchCompanyDetails(),
375
+ })
290
376
  .then((resp) => {
291
377
  Logger({ level: "DEBUG", message: "Click event sent" });
292
378
  })
@@ -304,21 +390,28 @@ if (typeof window != "undefined") {
304
390
  item_total: null,
305
391
  };
306
392
  //filter eventData.data to find the products array and item total
307
- const products = eventData.data
308
- .filter((item) => {
309
- if ((item["type"] === "product" && item.categories) || item.product)
310
- return true;
311
- })
312
- .map((product) => {
313
- return product.display || product.name;
314
- });
315
- const item_total = sg(() => eventData.page["item_total"]);
393
+ let products = [];
394
+ if (eventData.data && eventData.data.length > 0) {
395
+ products = eventData.data
396
+ .filter((item) => {
397
+ if ((item["type"] === "product" && item.categories) || item.product)
398
+ return true;
399
+ })
400
+ .map((product) => {
401
+ return product.display || product.name;
402
+ });
403
+ }
404
+
405
+ const item_total = sg(() =>
406
+ eventData.page["item_total"] ? eventData.page["item_total"] : 0
407
+ );
316
408
  if (payload.query) {
317
409
  //only if query is present
318
410
  Clickstream.sendEvent("product_search", {
319
411
  ...payload,
320
412
  products,
321
413
  item_total,
414
+ ...fetchCompanyDetails(),
322
415
  })
323
416
  .then((resp) => {
324
417
  Logger({ level: "DEBUG", message: "Click event sent" });