@mhubcloud/tms_core 1.7.6 → 2.0.0-beta.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.
Files changed (44) 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/dist/index.js +609 -0
  8. package/index.js +1 -0
  9. package/{lib → js/lib}/defaultPublisher.js +26 -24
  10. package/js/lib/utils.js +349 -0
  11. package/js/publishers/AffiliatePort.js +78 -0
  12. package/{publishers → js/publishers}/CJ.js +7 -5
  13. package/{publishers → js/publishers}/Facebook.js +6 -4
  14. package/{publishers → js/publishers}/GoogleAnalytics4.js +8 -6
  15. package/js/publishers/HttpPublisher.js +64 -0
  16. package/{publishers → js/publishers}/Piano.js +6 -4
  17. package/{publishers → js/publishers}/UniversalAnalytics.js +7 -5
  18. package/package.json +46 -29
  19. package/releasePlan.md +3 -0
  20. package/src/defaultPublisher.ts +132 -0
  21. package/src/index.ts +25 -0
  22. package/src/publishers/affiliatePort.ts +69 -0
  23. package/src/publishers/cj.ts +67 -0
  24. package/src/publishers/facebook.ts +49 -0
  25. package/src/publishers/googleAnalytics4.ts +60 -0
  26. package/src/publishers/piano.ts +71 -0
  27. package/src/publishers/universalAnalytics.ts +63 -0
  28. package/src/types/EventHubMessage.ts +8 -0
  29. package/src/types/EventPublisher.ts +9 -0
  30. package/src/types/PlatformPublisher.ts +19 -0
  31. package/src/types/UtilTypes.ts +1 -0
  32. package/src/types/global.d.ts +3 -0
  33. package/src/utils/utils.ts +358 -0
  34. package/tests/AffiliatePort.js +107 -85
  35. package/tests/CJ.js +144 -119
  36. package/tests/Facebook.js +124 -103
  37. package/tests/GoogleAnalytics4.js +82 -74
  38. package/tests/Piano.js +112 -99
  39. package/tests/UniversalAnalytics.js +90 -77
  40. package/tests/testUtils.js +7 -0
  41. package/tsconfig.eslint.json +43 -0
  42. package/tsconfig.json +26 -0
  43. package/lib/utils.js +0 -307
  44. package/publishers/AffiliatePort.js +0 -61
@@ -1,116 +1,138 @@
1
- const publisher = require("../index.js").publisher.AffiliatePort;
2
- const defaultPublisher = require("../index.js").DefaultPublisher;
3
- const utils = require("../index.js").utils;
1
+ const publisher = require('../dist/index.js').publisher.AffiliatePort;
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 AffiliatePort publisher', function () {
7
8
  it('purchase', async function () {
8
9
  var eventHubMessages = [
9
10
  {
10
- "event": {
11
- "region": "smarty.cz",
12
- "id": "825333270-1011.1659006863812",
13
- "code": "purchase"
11
+ event: {
12
+ region: 'smarty.cz',
13
+ id: '825333270-1011.1659006863812',
14
+ code: 'purchase',
14
15
  },
15
- "user": {
16
- "ts": "customer.HL",
17
- "email": "jan.brzobohaty@crossmasters.com",
18
- "id": "a2992fb8-40ac-4b88-9cf5-7fba02f1f611"
16
+ user: {
17
+ ts: 'customer.HL',
18
+ email: 'jan.brzobohaty@crossmasters.com',
19
+ id: 'a2992fb8-40ac-4b88-9cf5-7fba02f1f611',
19
20
  },
20
- "session": {
21
- "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15",
22
- "ip": "217.195.174.253"
21
+ session: {
22
+ userAgent:
23
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15',
24
+ ip: '217.195.174.253',
23
25
  },
24
- "permissions": [],
25
- "currencyCode": "CZK",
26
- "publisher": {
27
- "code": "www.smarty.cz",
28
- "tm": "GTM-NVNTFG",
29
- "tmv": "193"
26
+ permissions: [],
27
+ currencyCode: 'CZK',
28
+ publisher: {
29
+ code: 'www.smarty.cz',
30
+ tm: 'GTM-NVNTFG',
31
+ tmv: '193',
30
32
  },
31
- "$schema": "https://schema.measurementhub.net/mhub/2021-12-27/purchase",
32
- "id": "3511111111",
33
- "revenue": 10,
34
- "products": [
33
+ $schema: 'https://schema.measurementhub.net/mhub/2021-12-27/purchase',
34
+ id: '3511111111',
35
+ revenue: 10,
36
+ products: [
35
37
  {
36
- "id": "67137",
37
- "price": 10,
38
- "quantity": 1,
39
- "category": "Playstation",
40
- "masterType": "Konzole"
41
- },{
42
- "id": "67137",
43
- "price": 20,
44
- "quantity": 1,
45
- "category": "Monitor",
46
- "masterType": "Video"
47
- }
38
+ id: '67137',
39
+ price: 10,
40
+ quantity: 1,
41
+ category: 'Playstation',
42
+ masterType: 'Konzole',
43
+ },
44
+ {
45
+ id: '67137',
46
+ price: 20,
47
+ quantity: 1,
48
+ category: 'Monitor',
49
+ masterType: 'Video',
50
+ },
51
+ ],
52
+ location: 'https://www.smarty.cz/checkoutSuccess',
53
+ referrer:
54
+ 'https://www.smarty.cz/Order/AddReservation?productId=57873&shopAddressId=24798',
55
+ enqueuedAt: 1659006864038,
56
+ ads: [
57
+ {
58
+ adPlatform: 'affiliateport',
59
+ adCodes: {
60
+ transaction_id:
61
+ '816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3',
62
+ },
63
+ visitAt: 1658392668000,
64
+ },
48
65
  ],
49
- "location": "https://www.smarty.cz/checkoutSuccess",
50
- "referrer": "https://www.smarty.cz/Order/AddReservation?productId=57873&shopAddressId=24798",
51
- "enqueuedAt": 1659006864038,
52
- "ads": [{"adPlatform":"affiliateport","adCodes":{"transaction_id":"816f5670dbf52c3dc27a7e849903c327d705ef643993fbab3"},"visitAt":1658392668000}],
53
- "EventProcessedUtcTime": "2022-07-28T11:54:50.2528883Z",
54
- "PartitionId": 0,
55
- "EventEnqueuedUtcTime": "2022-07-28T11:14:24.1170000Z"
56
- }
66
+ EventProcessedUtcTime: '2022-07-28T11:54:50.2528883Z',
67
+ PartitionId: 0,
68
+ EventEnqueuedUtcTime: '2022-07-28T11:14:24.1170000Z',
69
+ },
57
70
  ];
58
71
  await test(eventHubMessages);
59
72
  });
60
- })
61
-
73
+ });
62
74
 
63
75
  /**
64
76
  * Test function
65
77
  * @param [object] object obsahující nastavení testovací parametry
66
78
  */
67
79
  async function test(eventHubMessages) {
68
- try{
69
- await defaultPublisher(console, eventHubMessages, "AffiliatePort", publisher, trigger);
70
- }catch(err){
71
- console.log(err);
72
- }
80
+ try {
81
+ await defaultPublisher(
82
+ testUtils.context,
83
+ eventHubMessages,
84
+ 'AffiliatePort',
85
+ publisher,
86
+ trigger,
87
+ );
88
+ } catch (err) {
89
+ console.log(err);
90
+ }
73
91
  }
74
92
 
75
93
  function trigger(data) {
76
- const publishers = [];
77
- let publisher;
78
-
79
- //Dummy config, replace with your own
80
- const config = {
81
- offerId: 123,
82
- };
94
+ const publishers = [];
95
+ let publisher;
83
96
 
84
- switch (data.event?.code) {
85
- case "purchase":
86
- publisher = AffiliatePortPurchasePublisher;
87
- publisher.register(publishers, config);
88
- break;
89
- }
90
- return publishers;
91
- };
97
+ // Dummy config, replace with your own
98
+ const config = {
99
+ offerId: 123,
100
+ };
101
+
102
+ switch (data.event?.code) {
103
+ case 'purchase':
104
+ publisher = AffiliatePortPurchasePublisher;
105
+ publisher.register(publishers, config);
106
+ break;
107
+ }
108
+ return publishers;
109
+ }
92
110
 
93
111
  /**
94
112
  * Implements Purchase event publisher for Facebook
95
113
  * @implements {EventPublisher<{id: string, access_token: string}>}
96
114
  */
97
115
  class AffiliatePortPurchasePublisher extends utils.EventPublisherBase {
98
- /**
99
- * Maps `purchase` event data to AffiliatePort payload
100
- * @param {Object} data `purchase` event data
101
- * @returns {Object} Data mapped from incomming Event Hub message to platform payload
102
- */
103
- map(data) {
104
- return {
105
- orderId: data.id,
106
- revenue: data.revenue, //should be without VAT and shipping, payment fees
107
- products: data.products.map(pr => ({
108
- quantity: pr.quantity, // e.g. 1
109
- price: pr.price, // e.g. 109
110
- category: pr.category.replace(/ /g, "-"), // e.g. Playstation
111
- masterType: pr.masterType // e.g. Konzole
112
- })),
113
- transactionID: utils.extractAdCode(data, 'affiliateport', 'transaction_id')?.code // AffiliatePort identificator taken automatically from URL
114
- };
115
- }
116
- };
116
+ /**
117
+ * Maps `purchase` event data to AffiliatePort payload
118
+ * @param {Object} data `purchase` event data
119
+ * @returns {Object} Data mapped from incomming Event Hub message to platform payload
120
+ */
121
+ map(data) {
122
+ return {
123
+ orderId: data.id,
124
+ revenue: data.revenue, // should be without VAT and shipping, payment fees
125
+ products: data.products.map((pr) => ({
126
+ quantity: pr.quantity, // e.g. 1
127
+ price: pr.price, // e.g. 109
128
+ category: pr.category.replace(/ /g, '-'), // e.g. Playstation
129
+ masterType: pr.masterType, // e.g. Konzole
130
+ })),
131
+ transactionID: utils.extractAdCode(
132
+ data,
133
+ 'affiliateport',
134
+ 'transaction_id',
135
+ )?.code, // AffiliatePort identificator taken automatically from URL
136
+ };
137
+ }
138
+ }
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
+ }