@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
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
+ }
@@ -1,91 +1,99 @@
1
- const publisher = require("../index.js").publisher.GoogleAnalytics4;
2
- const defaultPublisher = require("../index.js").DefaultPublisher;
3
- const utils = require("../index.js").utils;
1
+ const publisher = require('../dist/index.js').publisher.GoogleAnalytics4;
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 GoogleAnalytics4 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": "pageview"
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: 'pageview',
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
- await defaultPublisher(console, eventHubMessages, "GoogleAnalytics4", publisher, trigger);
58
+ await defaultPublisher(
59
+ testUtils.context,
60
+ eventHubMessages,
61
+ 'GoogleAnalytics4',
62
+ publisher,
63
+ trigger,
64
+ );
57
65
  }
58
66
 
59
67
  function trigger(data) {
60
- const publishers = [];
61
- const config = {trackingId: "G-xxxxxxxxxxx"};
68
+ const publishers = [];
69
+ const config = { trackingId: 'G-289495622' };
62
70
 
63
- if (data.event?.code === "pageview") {
64
- const publisher = GAPageviewPublisher;
65
- publisher.register(publishers, config);
66
- }
71
+ if (data.event?.code === 'pageview') {
72
+ const publisher = GAPageviewPublisher;
73
+ publisher.register(publishers, config);
74
+ }
67
75
 
68
- return publishers;
69
- };
76
+ return publishers;
77
+ }
70
78
 
71
79
  class GAPageviewPublisher extends utils.EventPublisherBase {
72
- /**
73
- * Maps `pageview` event data to GA payload
74
- */
75
- map(data) {
76
- return {
77
- //mandatory for all events
78
- cid: "456", //client id
79
- en: "eventMhubCloud",//event name
80
-
81
- //mandatory for pageview
82
- dl: "/", //URL
80
+ /**
81
+ * Maps `pageview` event data to GA payload
82
+ */
83
+ map(data) {
84
+ return {
85
+ // mandatory for all events
86
+ cid: '456', // client id
87
+ en: 'eventMhubCloud', // event name
88
+
89
+ // mandatory for pageview
90
+ dl: '/', // URL
83
91
 
84
- //optional
85
- //ul : "en-us",
86
- //sr: "1536x864", //screan resolution
87
- //dr: "ccc", //refferer
88
- //dt: "xx" //page title
89
- };
90
- }
91
- };
92
+ // optional
93
+ // ul : "en-us",
94
+ // sr: "1536x864", //screan resolution
95
+ // dr: "ccc", //refferer
96
+ // dt: "xx" //page title
97
+ };
98
+ }
99
+ }