@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.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +87 -0
- package/.prettierignore +3 -0
- package/.prettierrc +10 -0
- package/.vscode/extensions.json +12 -0
- package/.vscode/settings.json +52 -0
- package/dist/index.js +609 -0
- package/index.js +1 -0
- package/{lib → js/lib}/defaultPublisher.js +26 -24
- package/js/lib/utils.js +349 -0
- package/js/publishers/AffiliatePort.js +78 -0
- package/{publishers → js/publishers}/CJ.js +7 -5
- package/{publishers → js/publishers}/Facebook.js +6 -4
- package/{publishers → js/publishers}/GoogleAnalytics4.js +8 -6
- package/js/publishers/HttpPublisher.js +64 -0
- package/{publishers → js/publishers}/Piano.js +6 -4
- package/{publishers → js/publishers}/UniversalAnalytics.js +7 -5
- package/package.json +46 -29
- package/releasePlan.md +3 -0
- package/src/defaultPublisher.ts +132 -0
- package/src/index.ts +25 -0
- package/src/publishers/affiliatePort.ts +69 -0
- package/src/publishers/cj.ts +67 -0
- package/src/publishers/facebook.ts +49 -0
- package/src/publishers/googleAnalytics4.ts +60 -0
- package/src/publishers/piano.ts +71 -0
- package/src/publishers/universalAnalytics.ts +63 -0
- package/src/types/EventHubMessage.ts +8 -0
- package/src/types/EventPublisher.ts +9 -0
- package/src/types/PlatformPublisher.ts +19 -0
- package/src/types/UtilTypes.ts +1 -0
- package/src/types/global.d.ts +3 -0
- package/src/utils/utils.ts +358 -0
- package/tests/AffiliatePort.js +107 -85
- package/tests/CJ.js +144 -119
- package/tests/Facebook.js +124 -103
- package/tests/GoogleAnalytics4.js +82 -74
- package/tests/Piano.js +112 -99
- package/tests/UniversalAnalytics.js +90 -77
- package/tests/testUtils.js +7 -0
- package/tsconfig.eslint.json +43 -0
- package/tsconfig.json +26 -0
- package/lib/utils.js +0 -307
- package/publishers/AffiliatePort.js +0 -61
package/tests/AffiliatePort.js
CHANGED
|
@@ -1,116 +1,138 @@
|
|
|
1
|
-
const publisher = require(
|
|
2
|
-
const defaultPublisher = require(
|
|
3
|
-
const utils = require(
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
event: {
|
|
12
|
+
region: 'smarty.cz',
|
|
13
|
+
id: '825333270-1011.1659006863812',
|
|
14
|
+
code: 'purchase',
|
|
14
15
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
user: {
|
|
17
|
+
ts: 'customer.HL',
|
|
18
|
+
email: 'jan.brzobohaty@crossmasters.com',
|
|
19
|
+
id: 'a2992fb8-40ac-4b88-9cf5-7fba02f1f611',
|
|
19
20
|
},
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
permissions: [],
|
|
27
|
+
currencyCode: 'CZK',
|
|
28
|
+
publisher: {
|
|
29
|
+
code: 'www.smarty.cz',
|
|
30
|
+
tm: 'GTM-NVNTFG',
|
|
31
|
+
tmv: '193',
|
|
30
32
|
},
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
$schema: 'https://schema.measurementhub.net/mhub/2021-12-27/purchase',
|
|
34
|
+
id: '3511111111',
|
|
35
|
+
revenue: 10,
|
|
36
|
+
products: [
|
|
35
37
|
{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//Dummy config, replace with your own
|
|
80
|
-
const config = {
|
|
81
|
-
offerId: 123,
|
|
82
|
-
};
|
|
94
|
+
const publishers = [];
|
|
95
|
+
let publisher;
|
|
83
96
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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(
|
|
2
|
-
const defaultPublisher = require(
|
|
3
|
-
const utils = require(
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
122
|
+
const publishers = [];
|
|
123
|
+
if (data.event.code === 'purchase') {
|
|
124
|
+
let publisher = CJPurchasePublisher;
|
|
100
125
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
}
|