@mhubcloud/tms_core 1.7.5 → 2.0.0-beta.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.
Files changed (42) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.cjs +87 -0
  3. package/.prettierignore +3 -0
  4. package/.prettierrc +10 -0
  5. package/.vscode/extensions.json +12 -0
  6. package/.vscode/settings.json +52 -0
  7. package/index.js +3 -1
  8. package/{lib → js/lib}/defaultPublisher.js +26 -24
  9. package/js/lib/utils.js +349 -0
  10. package/js/publishers/AffiliatePort.js +78 -0
  11. package/{publishers → js/publishers}/CJ.js +7 -5
  12. package/{publishers → js/publishers}/Facebook.js +6 -4
  13. package/{publishers → js/publishers}/GoogleAnalytics4.js +8 -6
  14. package/js/publishers/HttpPublisher.js +64 -0
  15. package/{publishers → js/publishers}/Piano.js +6 -4
  16. package/{publishers → js/publishers}/UniversalAnalytics.js +7 -5
  17. package/package.json +46 -29
  18. package/releasePlan.md +3 -0
  19. package/src/defaultPublisher.ts +132 -0
  20. package/src/index.ts +25 -0
  21. package/src/publishers/affiliatePort.ts +69 -0
  22. package/src/publishers/cj.ts +67 -0
  23. package/src/publishers/facebook.ts +49 -0
  24. package/src/publishers/googleAnalytics4.ts +60 -0
  25. package/src/publishers/piano.ts +71 -0
  26. package/src/publishers/universalAnalytics.ts +63 -0
  27. package/src/types/EventHubMessage.ts +8 -0
  28. package/src/types/EventPublisher.ts +9 -0
  29. package/src/types/PlatformPublisher.ts +19 -0
  30. package/src/types/UtilTypes.ts +1 -0
  31. package/src/types/global.d.ts +3 -0
  32. package/src/utils/utils.ts +358 -0
  33. package/tests/AffiliatePort.js +138 -0
  34. package/tests/CJ.js +144 -119
  35. package/tests/Facebook.js +124 -103
  36. package/tests/GoogleAnalytics4.js +82 -74
  37. package/tests/Piano.js +112 -99
  38. package/tests/UniversalAnalytics.js +90 -77
  39. package/tests/testUtils.js +7 -0
  40. package/tsconfig.eslint.json +43 -0
  41. package/tsconfig.json +26 -0
  42. package/lib/utils.js +0 -307
package/tests/CJ.js CHANGED
@@ -1,135 +1,160 @@
1
- const publisher = require("../index.js").publisher.CJ;
2
- const defaultPublisher = require("../index.js").DefaultPublisher;
3
- const utils = require("../index.js").utils;
1
+ const publisher = require('../dist/index.js').publisher.CJ;
2
+ const defaultPublisher = require('../dist/index.js').DefaultPublisher;
3
+ const utils = require('../dist/index.js').utils;
4
4
  var assert = require('assert');
5
+ const testUtils = require('./testUtils');
5
6
 
6
7
  describe('test CJ publisher', function () {
7
- it('purchase', async function () {
8
- var eventHubMessages = [
9
- {
10
- "currencyCode": "CZK",
11
- "event": {
12
- "region": "MAIN",
13
- "id": "115169112-4870.1657003407533",
14
- "code": "purchase"
15
- },
16
- "session": {
17
- "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
18
- "ip": "89.177.126.237",
19
- "referrer": "https://crossmasters.com/"
20
- },
21
- "referrer": "https://crossmasters.com/",
22
- "permissions": [],
23
- "identity": {
24
- "piano": {
25
- "id": "1657003393413.319297501"
26
- }
27
- },
28
- "publisher": {
29
- "code": "mhubcloud.com",
30
- "tm": "GTM-N7K9L4X",
31
- "tmv": "494"
32
- },
33
- "$schema": "/mhub/purchase",
34
- "id": "20162351",
35
- "revenue": 440,
36
- "products": [
37
- {
38
- "id": "Car",
39
- "price": 440,
40
- "quantity": 1
41
- }
42
- ],
43
- "enqueuedAt": 1657003409370,
44
- "ads": [{"adPlatform":"cjaffiliate","adCodes":{"cjevent":"816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3"},"visitAt":1658392668000}]
45
- },
46
- {
47
- "currencyCode": "CZK",
48
- "event": {
49
- "region": "MAIN",
50
- "id": "115169112-4870.1657003407533",
51
- "code": "purchase"
52
- },
53
- "session": {
54
- "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
55
- "ip": "89.177.126.237",
56
- "referrer": "https://crossmasters.com/"
57
- },
58
- "referrer": "https://crossmasters.com/",
59
- "permissions": [],
60
- "identity": {
61
- "piano": {
62
- "id": "1657003393413.319297501"
63
- }
64
- },
65
- "publisher": {
66
- "code": "mhubcloud.com",
67
- "tm": "GTM-N7K9L4X",
68
- "tmv": "494"
69
- },
70
- "$schema": "/mhub/purchase",
71
- "id": "20162351",
72
- "revenue": 440,
73
- "products": [
74
- {
75
- "id": "Car",
76
- "price": 440,
77
- "quantity": 1
78
- }
79
- ],
80
- "enqueuedAt": 1657003409370,
81
- "ads": [{"adPlatform":"cjaffiliate","adCodes":{"cjevent":"816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3"},"visitAt":1658392668000}]
82
- }
83
- ];
84
- await test(eventHubMessages);
85
- });
86
- })
8
+ it('purchase', async function () {
9
+ var eventHubMessages = [
10
+ {
11
+ currencyCode: 'CZK',
12
+ event: {
13
+ region: 'MAIN',
14
+ id: '115169112-4870.1657003407533',
15
+ code: 'purchase',
16
+ },
17
+ session: {
18
+ userAgent:
19
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
20
+ ip: '89.177.126.237',
21
+ referrer: 'https://crossmasters.com/',
22
+ },
23
+ referrer: 'https://crossmasters.com/',
24
+ permissions: [],
25
+ identity: {
26
+ piano: {
27
+ id: '1657003393413.319297501',
28
+ },
29
+ },
30
+ publisher: {
31
+ code: 'mhubcloud.com',
32
+ tm: 'GTM-N7K9L4X',
33
+ tmv: '494',
34
+ },
35
+ $schema: '/mhub/purchase',
36
+ id: '20162351',
37
+ revenue: 440,
38
+ products: [
39
+ {
40
+ id: 'Car',
41
+ price: 440,
42
+ quantity: 1,
43
+ },
44
+ ],
45
+ enqueuedAt: 1657003409370,
46
+ ads: [
47
+ {
48
+ adPlatform: 'cjaffiliate',
49
+ adCodes: {
50
+ cjevent: '816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3',
51
+ },
52
+ visitAt: 1658392668000,
53
+ },
54
+ ],
55
+ },
56
+ {
57
+ currencyCode: 'CZK',
58
+ event: {
59
+ region: 'MAIN',
60
+ id: '115169112-4870.1657003407533',
61
+ code: 'purchase',
62
+ },
63
+ session: {
64
+ userAgent:
65
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
66
+ ip: '89.177.126.237',
67
+ referrer: 'https://crossmasters.com/',
68
+ },
69
+ referrer: 'https://crossmasters.com/',
70
+ permissions: [],
71
+ identity: {
72
+ piano: {
73
+ id: '1657003393413.319297501',
74
+ },
75
+ },
76
+ publisher: {
77
+ code: 'mhubcloud.com',
78
+ tm: 'GTM-N7K9L4X',
79
+ tmv: '494',
80
+ },
81
+ $schema: '/mhub/purchase',
82
+ id: '20162351',
83
+ revenue: 440,
84
+ products: [
85
+ {
86
+ id: 'Car',
87
+ price: 440,
88
+ quantity: 1,
89
+ },
90
+ ],
91
+ enqueuedAt: 1657003409370,
92
+ ads: [
93
+ {
94
+ adPlatform: 'cjaffiliate',
95
+ adCodes: {
96
+ cjevent: '816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3',
97
+ },
98
+ visitAt: 1658392668000,
99
+ },
100
+ ],
101
+ },
102
+ ];
103
+ await test(eventHubMessages);
104
+ });
105
+ });
87
106
 
88
107
  /**
89
108
  * Test function
90
109
  * @param [object] object obsahující nastavení testovací parametry
91
110
  */
92
111
  async function test(eventHubMessages) {
93
- await defaultPublisher(console, eventHubMessages, "CJ", publisher, trigger);
112
+ await defaultPublisher(
113
+ testUtils.context,
114
+ eventHubMessages,
115
+ 'CJ',
116
+ publisher,
117
+ trigger,
118
+ );
94
119
  }
95
120
 
96
121
  function trigger(data) {
97
- const publishers = [];
98
- if (data.event.code === 'purchase') {
99
- let publisher = CJPurchasePublisher;
122
+ const publishers = [];
123
+ if (data.event.code === 'purchase') {
124
+ let publisher = CJPurchasePublisher;
100
125
 
101
- publisher.register(publishers, { cid: 1, type: 1111111 });
102
- }
103
- return publishers;
104
- };
126
+ publisher.register(publishers, { cid: 1, type: 1111111 });
127
+ }
128
+ return publishers;
129
+ }
105
130
 
106
131
  class CJPurchasePublisher extends utils.EventPublisherBase {
107
- /**
108
- * Converts input data to publishing payload data understood by the publisher
109
- * @param {Object} data `purchase` event data
110
- * @returns {Object} Data mapped from incomming Event Hub message to platform payload
111
- */
112
- map(data) {
113
- const ret = {
114
- cjevent: utils.extractAdCode(data, 'cjaffiliate', 'cjevent')?.code,
115
- cust_status: data.user?.ts?.indexOf("customer") >= 0 ? "return" : "new",
116
- coupon: data.coupon,
117
- discount: data.localDiscount,
118
- method: "S2S",
119
- currency: data.localCurrencyCode,
120
- oid: data.id
121
- // eventTime: "" // This parameters is ignored, as the time is determined by the CJ server
122
- };
123
- if (data.products instanceof Array) {
124
- let i = 0;
125
- for (const product of data.products) {
126
- i++;
127
- ret[`item${i}`] = product.id;
128
- ret[`amt${i}`] = product.localPrice;
129
- ret[`qty${i}`] = product.quantity;
130
- }
131
- }
132
-
133
- return ret;
132
+ /**
133
+ * Converts input data to publishing payload data understood by the publisher
134
+ * @param {Object} data `purchase` event data
135
+ * @returns {Object} Data mapped from incomming Event Hub message to platform payload
136
+ */
137
+ map(data) {
138
+ const ret = {
139
+ cjevent: utils.extractAdCode(data, 'cjaffiliate', 'cjevent')?.code,
140
+ cust_status: data.user?.ts?.indexOf('customer') >= 0 ? 'return' : 'new',
141
+ coupon: data.coupon,
142
+ discount: data.localDiscount,
143
+ method: 'S2S',
144
+ currency: data.localCurrencyCode,
145
+ oid: data.id,
146
+ // eventTime: "" // This parameters is ignored, as the time is determined by the CJ server
147
+ };
148
+ if (data.products instanceof Array) {
149
+ let i = 0;
150
+ for (const product of data.products) {
151
+ i++;
152
+ ret[`item${i}`] = product.id;
153
+ ret[`amt${i}`] = product.localPrice;
154
+ ret[`qty${i}`] = product.quantity;
155
+ }
134
156
  }
135
- };
157
+
158
+ return ret;
159
+ }
160
+ }
package/tests/Facebook.js CHANGED
@@ -1,118 +1,139 @@
1
- const publisher = require("../index.js").publisher.Facebook;
2
- const defaultPublisher = require("../index.js").DefaultPublisher;
3
- const utils = require("../index.js").utils;
1
+ const publisher = require('../dist/index.js').publisher.Facebook;
2
+ const defaultPublisher = require('../dist/index.js').DefaultPublisher;
3
+ const utils = require('../dist/index.js').utils;
4
4
  var assert = require('assert');
5
+ const testUtils = require('./testUtils');
5
6
 
6
7
  describe('test Facebook publisher', function () {
7
- it('purchase', async function () {
8
- var eventHubMessages = [
9
- {
10
- "currencyCode": "CZK",
11
- "event": {
12
- "region": "MAIN",
13
- "id": "115169112-4870.1657003407533",
14
- "code": "purchase"
15
- },
16
- "session": {
17
- "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
18
- "ip": "89.177.126.237",
19
- "referrer": "https://crossmasters.com/"
20
- },
21
- "referrer": "https://crossmasters.com/",
22
- "permissions": [],
23
- "identity": {
24
- "piano": {
25
- "id": "1657003393413.319297501"
26
- }
27
- },
28
- "publisher": {
29
- "code": "mhubcloud.com",
30
- "tm": "GTM-N7K9L4X",
31
- "tmv": "494"
32
- },
33
- "$schema": "/mhub/purchase",
34
- "id": "20162351",
35
- "revenue": 440,
36
- "products": [
37
- {
38
- "id": "Car",
39
- "price": 440,
40
- "quantity": 1
41
- }
42
- ],
43
- "enqueuedAt": 1657003409370,
44
- "ads": []
45
- }
46
- ];
47
- await test(eventHubMessages);
48
- });
49
- })
8
+ it('purchase', async function () {
9
+ var eventHubMessages = [
10
+ {
11
+ currencyCode: 'CZK',
12
+ event: {
13
+ region: 'MAIN',
14
+ id: '115169112-4870.1657003407533',
15
+ code: 'purchase',
16
+ },
17
+ session: {
18
+ userAgent:
19
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
20
+ ip: '89.177.126.237',
21
+ referrer: 'https://crossmasters.com/',
22
+ },
23
+ referrer: 'https://crossmasters.com/',
24
+ permissions: [],
25
+ identity: {
26
+ piano: {
27
+ id: '1657003393413.319297501',
28
+ },
29
+ },
30
+ publisher: {
31
+ code: 'mhubcloud.com',
32
+ tm: 'GTM-N7K9L4X',
33
+ tmv: '494',
34
+ },
35
+ $schema: '/mhub/purchase',
36
+ id: '20162351',
37
+ revenue: 440,
38
+ products: [
39
+ {
40
+ id: 'Car',
41
+ price: 440,
42
+ quantity: 1,
43
+ },
44
+ ],
45
+ enqueuedAt: 1657003409370,
46
+ ads: [],
47
+ },
48
+ ];
49
+ await test(eventHubMessages);
50
+ });
51
+ });
50
52
 
51
53
  /**
52
54
  * Test function
53
55
  * @param [object] object obsahující nastavení testovací parametry
54
56
  */
55
57
  async function test(eventHubMessages) {
56
- try{
57
- await defaultPublisher(console, eventHubMessages, "Facebook", publisher, trigger);
58
- }catch(err){
59
- assert.equal(err.toString().indexOf('Error: HTTP Error code: 400, event: purchase, body: {"error":{"message":"Invalid OAuth access token - Cannot parse access token","type":"OAuthException","code":190'), 0);
60
- }
58
+ try {
59
+ await defaultPublisher(
60
+ testUtils.context,
61
+ eventHubMessages,
62
+ 'Facebook',
63
+ publisher,
64
+ trigger,
65
+ );
66
+ } catch (err) {
67
+ assert.equal(
68
+ err
69
+ .toString()
70
+ .indexOf(
71
+ 'Error: HTTP Error code: 400, event: purchase, body: {"error":{"message":"Invalid OAuth access token - Cannot parse access token","type":"OAuthException","code":190',
72
+ ),
73
+ 0,
74
+ );
75
+ }
61
76
  }
62
77
 
63
78
  function trigger(data) {
64
- const publishers = [];
65
- let publisher;
66
-
67
- //Dummy config, replace with your own
68
- const config = {
69
- id: "fffff",
70
- access_token: "zzzz"
71
- };
79
+ const publishers = [];
80
+ let publisher;
81
+
82
+ // Dummy config, replace with your own
83
+ const config = {
84
+ id: 'fffff',
85
+ // eslint-disable-next-line @typescript-eslint/naming-convention
86
+ access_token: 'zzzz',
87
+ };
72
88
 
73
- switch (data.event?.code) {
74
- case "purchase":
75
- publisher = FBPurchasePublisher;
76
- publisher.register(publishers, config);
77
- break;
78
- }
79
- return publishers;
80
- };
89
+ switch (data.event?.code) {
90
+ case 'purchase':
91
+ publisher = new FBPurchasePublisher(config);
92
+ publishers.push(publisher);
93
+ break;
94
+ }
95
+ return publishers;
96
+ }
81
97
 
82
98
  class FBPurchasePublisher extends utils.EventPublisherBase {
83
- /**
84
- * Maps `purchase` event data to Facebook payload
85
- * @param {Object} data `purchase` event data
86
- * @returns {Object} Data mapped from incomming Event Hub message to platform payload
87
- */
88
- map(data) {
89
- return {
90
- event_name: "Purchase", // Constant
91
- event_time: Math.floor(data.enqueuedAt / 1000), // convert to seconds
92
- action_source: "website", // Constant
93
- event_id: data.id, // e.g. OBJ1248035
94
- event_source_url: data.location, // e.g. https://eshop.com/checkoutSuccess
95
- opt_out: false, // Constant
96
- user_data: {
97
- client_user_agent: data.session?.userAgent, // e.g. Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit
98
- fbc: utils.extractAdCode(data, 'facebook', 'fbclid', ad => `fb.1.${ad.visitAt}.${ad.code}`) || undefined, // e.g. fb.1.1623142020900.IwAR3CoXSdAr4nXyoVGo5H72wAiP8BrgPays0hhH1UgxnW_8D7rDhuHgjvvw0_aem_AXjjss7KoC0VSnBGvyUAkPip4VaZV6qytiIx5s8FQrAy5yzamZNRSC_xaCXEeJ8ecyphjuyQ2zbDAMoaIVayYAHxZgFH8TJsMzysJAIikY0How
99
- fbp: data.identity?.facebook?.id, // e.g. fb.1.1609144040877.1929163682
100
- client_ip_address: data.session?.ip, // e.g. 31.30.171.18
101
- em: utils.encodeEmail(data.user?.email)
102
- },
103
- custom_data:
104
- {
105
- currency: data.currencyCode, // e.g. EUR
106
- value: data.revenue, // e.g. 142.52
107
- contents: data.products.map(pr => ({
108
- id: pr.id, // e.g. PRDCT1248035
109
- quantity: pr.quantity, // e.g. 1
110
- item_price: pr.price // e.g. 109
111
- })),
112
- content_type: "product", // Constant
113
- order_id: data.id, // e.g. OBJ1248035
114
- content_category: "Purchase" // Constant
115
- }
116
- };
117
- }
118
- };
99
+ /**
100
+ * Maps `purchase` event data to Facebook payload
101
+ * @param {Object} data `purchase` event data
102
+ * @returns {Object} Data mapped from incomming Event Hub message to platform payload
103
+ */
104
+ map(data) {
105
+ return {
106
+ event_name: 'Purchase', // Constant
107
+ event_time: Math.floor(data.enqueuedAt / 1000), // convert to seconds
108
+ action_source: 'website', // Constant
109
+ event_id: data.id, // e.g. OBJ1248035
110
+ event_source_url: data.location, // e.g. https://eshop.com/checkoutSuccess
111
+ opt_out: false, // Constant
112
+ user_data: {
113
+ client_user_agent: data.session?.userAgent, // e.g. Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit
114
+ fbc:
115
+ utils.extractAdCode(
116
+ data,
117
+ 'facebook',
118
+ 'fbclid',
119
+ (ad) => `fb.1.${ad.visitAt}.${ad.code}`,
120
+ ) || undefined, // e.g. fb.1.1623142020900.IwAR3CoXSdAr4nXyoVGo5H72wAiP8BrgPays0hhH1UgxnW_8D7rDhuHgjvvw0_aem_AXjjss7KoC0VSnBGvyUAkPip4VaZV6qytiIx5s8FQrAy5yzamZNRSC_xaCXEeJ8ecyphjuyQ2zbDAMoaIVayYAHxZgFH8TJsMzysJAIikY0How
121
+ fbp: data.identity?.facebook?.id, // e.g. fb.1.1609144040877.1929163682
122
+ client_ip_address: data.session?.ip, // e.g. 31.30.171.18
123
+ em: utils.encodeEmail(data.user?.email),
124
+ },
125
+ custom_data: {
126
+ currency: data.currencyCode, // e.g. EUR
127
+ value: data.revenue, // e.g. 142.52
128
+ contents: data.products.map((pr) => ({
129
+ id: pr.id, // e.g. PRDCT1248035
130
+ quantity: pr.quantity, // e.g. 1
131
+ item_price: pr.price, // e.g. 109
132
+ })),
133
+ content_type: 'product', // Constant
134
+ order_id: data.id, // e.g. OBJ1248035
135
+ content_category: 'Purchase', // Constant
136
+ },
137
+ };
138
+ }
139
+ }