wingbot 3.60.0 → 3.61.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.60.0",
3
+ "version": "3.61.1",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "doc": "npm run doc:gql && node ./bin/makeApiDoc.js && cpy ./CHANGELOG.md ./doc && gitbook install ./doc && gitbook build ./doc && rimraf -rf ./docs && rimraf --rf ./doc/CHANGELOG.md && move-cli ./doc/_book ./docs",
10
10
  "test": "npm run test:lint && npm run test:coverage && npm run test:coverage:threshold",
11
11
  "test:coverage": "nyc --reporter=html mocha ./test && nyc report",
12
- "test:coverage:threshold": "nyc check-coverage --lines 90 --functions 89 --branches 80",
12
+ "test:coverage:threshold": "nyc check-coverage --lines 89 --functions 89 --branches 80",
13
13
  "test:backend": "mocha ./test",
14
14
  "test:lint": "eslint --ext .js src test *.js plugins"
15
15
  },
@@ -1,10 +1,62 @@
1
+ const { default: fetch } = require('node-fetch');
1
2
  const { compileWithState } = require('../../src/utils');
2
3
 
4
+ function transformEvent (c, a, l, v, req, res) {
5
+ switch (c) {
6
+ case 'generate_lead':
7
+ return {
8
+ name: 'generate_lead',
9
+ params: {
10
+ currency: l || 'USD',
11
+ value: v
12
+ }
13
+ };
14
+ case 'view_item':
15
+ return {
16
+ name: 'view_item',
17
+ params: {
18
+ currency: l || 'USD',
19
+ value: v,
20
+ items: [
21
+ {
22
+ item_name: res.currentAction(),
23
+ item_category: a || ''
24
+ }
25
+ ]
26
+ }
27
+ };
28
+ case 'purchase':
29
+ return {
30
+ name: 'purchase',
31
+ params: {
32
+ currency: l || 'USD',
33
+ transaction_id: `${req.pageId}.${req.senderId}.${req.timestamp}`,
34
+ value: v,
35
+ items: [
36
+ {
37
+ item_name: res.currentAction(),
38
+ item_category: a || ''
39
+ }
40
+ ]
41
+ }
42
+ };
43
+ case 'tutorial_begin':
44
+ case 'tutorial_complete':
45
+ case 'sign_up':
46
+ case 'share':
47
+ return {
48
+ name: c
49
+ };
50
+ default:
51
+ return null;
52
+ }
53
+ }
54
+
3
55
  /**
4
56
  * @param {import('../../src/Request')} req
5
57
  * @param {import('../../src/Responder')} res
6
58
  */
7
- function trackingEvent (req, res) {
59
+ async function trackingEvent (req, res) {
8
60
  const {
9
61
  category = '',
10
62
  action = '',
@@ -13,12 +65,53 @@ function trackingEvent (req, res) {
13
65
  type
14
66
  } = req.params;
15
67
 
16
- const c = compileWithState(req, res, category);
17
- const a = compileWithState(req, res, action);
18
- const l = compileWithState(req, res, label);
68
+ const c = compileWithState(req, res, category).trim();
69
+ const a = compileWithState(req, res, action).trim();
70
+ const l = compileWithState(req, res, label).trim();
19
71
  const v = parseFloat(compileWithState(req, res, value)
20
72
  .replace(/[^0-9.]+/, '')) || 0;
21
73
 
74
+ const {
75
+ '§gi': clientId,
76
+ '§gc': gclid
77
+ } = req.state;
78
+
79
+ const {
80
+ gaMeasurementId,
81
+ gaApiSecret
82
+ } = req.configuration;
83
+
84
+ if (clientId && gaMeasurementId && gaApiSecret) {
85
+
86
+ const ev = transformEvent(c, a, l, v, req, res);
87
+
88
+ const body = {
89
+ client_id: clientId,
90
+ timestamp_micros: req.timestamp * 1000,
91
+ non_personalized_ads: false,
92
+ ...(a ? {
93
+ user_properties: {
94
+ [a]: {
95
+ value: v || 1
96
+ }
97
+ }
98
+ } : {}),
99
+ events: ev ? [ev] : []
100
+ };
101
+
102
+ const params = {
103
+ method: 'POST',
104
+ body: JSON.stringify(body)
105
+ };
106
+
107
+ try {
108
+ await fetch(`https://www.google-analytics.com/mp/collect?api_secret=${encodeURIComponent(gaApiSecret)}&measurement_id=${encodeURIComponent(gaMeasurementId)}`, params);
109
+ } catch (e) {
110
+ // eslint-disable-next-line no-console
111
+ console.error('GA FAILED', e, body);
112
+ }
113
+ }
114
+
22
115
  res.trackEvent(type || 'report', c, a, l, v);
23
116
  }
24
117
 
@@ -162,17 +162,17 @@
162
162
  {
163
163
  "name": "category",
164
164
  "type": "text",
165
- "label": "Event category"
165
+ "label": "Event category (generate_lead,view_item,purchase,tutorial_begin,tutorial_complete,sign_up,share)"
166
166
  },
167
167
  {
168
168
  "name": "action",
169
169
  "type": "text",
170
- "label": "Event action"
170
+ "label": "Event action (item category)"
171
171
  },
172
172
  {
173
173
  "name": "label",
174
174
  "type": "text",
175
- "label": "Event label"
175
+ "label": "Event label (currency)"
176
176
  },
177
177
  {
178
178
  "name": "value",
@@ -109,6 +109,12 @@ const {
109
109
  * @prop {string} [osName]
110
110
  * @prop {string} [skill]
111
111
  * @prop {string} [prevSkill]
112
+ * @prop {string|null} cs
113
+ * @prop {string|null} cm
114
+ * @prop {string|null} cn
115
+ * @prop {string|null} ck
116
+ * @prop {string|null} cc
117
+ * @prop {string|null} dr
112
118
  */
113
119
 
114
120
  /**
@@ -333,6 +339,15 @@ function onInteractionHandler (
333
339
  const useSkill = (skill && webalize(skill)) || noneAction;
334
340
  const usePrevSkill = (prevSkill && webalize(prevSkill)) || noneAction;
335
341
 
342
+ const {
343
+ '§cs': cs = null,
344
+ '§cm': cm = null,
345
+ '§cn': cn = null,
346
+ '§ck': ck = null,
347
+ '§cc': cc = null,
348
+ '§dr': dr = null
349
+ } = req.state;
350
+
336
351
  const metadata = {
337
352
  sessionCount,
338
353
  lang,
@@ -351,7 +366,16 @@ function onInteractionHandler (
351
366
  deviceType: ua.device.type || null,
352
367
  osName: ua.os.name || null,
353
368
  skill: useSkill,
354
- prevSkill: usePrevSkill
369
+ prevSkill: usePrevSkill,
370
+
371
+ cs,
372
+ cm,
373
+ cn,
374
+ ck,
375
+ cc,
376
+ dr: dr
377
+ ? `${dr}`.toLowerCase().replace(/^[a-z0-9]:\/\//, '').replace(/(?<=\.[a-z]+)\/.*$/, '')
378
+ : null
355
379
  };
356
380
 
357
381
  let sessionPromise;