@mhubcloud/tms_core 1.4.0 → 1.5.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.
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
- const utils = require('lib/utils');
2
- const DefaultPublisher = require('lib/DefaultPublisher');
3
- const Piano = require('publishers/Piano');
1
+ const utils = require('./lib/utils');
2
+ const DefaultPublisher = require('./lib/DefaultPublisher');
3
+ const Piano = require('./publishers/Piano');
4
4
 
5
5
  module.exports = {
6
6
  utils : utils,
@@ -12,7 +12,7 @@ const utils = require("./utils");
12
12
  */
13
13
  module.exports = async function DefaultPublisher(context, eventHubMessages, source, publisher, trigger) {
14
14
  context.log(`JavaScript eventhub trigger function called for message array [${eventHubMessages.length}]`);
15
- global.console.log = context.log.warn;
15
+ global.console.log = context.log.warn || context.log;
16
16
 
17
17
  let ret = context.test ? [] : undefined;
18
18
  let mi = 0;
package/lib/utils.js CHANGED
@@ -223,6 +223,10 @@ module.exports = {
223
223
  logException: function logException(exception) {
224
224
  console.log(exception);
225
225
  aiClient?.trackException({ exception: exception });
226
+ //raise exception if it is running under mocha test
227
+ if(typeof global.it === 'function'){
228
+ throw(exception);
229
+ }
226
230
  },
227
231
 
228
232
  /**
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@mhubcloud/tms_core",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Core function for mHub Cloud TMS publishers",
5
5
  "homepage": "https://mhubcloud.com/",
6
6
  "license": "Apache 2.0",
7
- "email" : "info@crossmasters.com",
8
- "author" : "Jan Brzobohatý",
7
+ "email": "info@crossmasters.com",
8
+ "author": "Jan Brzobohatý",
9
9
  "main": "index.js",
10
10
  "keywords": [
11
11
  "mHub Cloud",
@@ -15,9 +15,15 @@
15
15
  "server side measurement",
16
16
  "conversion API"
17
17
  ],
18
+ "scripts": {
19
+ "test": "mocha ./tests/*.js"
20
+ },
18
21
  "dependencies": {
19
22
  "ajv": "^8.8.2",
20
23
  "applicationinsights": "^2.2.0",
21
24
  "node-fetch": "^2.6.7"
22
- }
23
- }
25
+ },
26
+ "devDependencies": {
27
+ "mocha": "^10.0.0"
28
+ }
29
+ }
@@ -1,4 +1,4 @@
1
- const utils = require("../utils");
1
+ const utils = require("../lib/utils");
2
2
  const fetch = require('node-fetch');
3
3
  const SOURCE = "f_Piano";
4
4
 
@@ -15,7 +15,7 @@ module.exports = {
15
15
  },
16
16
 
17
17
  /**
18
- * Publishes data to Facebook
18
+ * Publishes data to Piano
19
19
  * @param {{collectionDomain: string, siteId: string|number}} config Publisher configuration
20
20
  * @param {{pianoData: {events: {name: string, data: {[key: string]: any}}[]}, aux: {visitorId: string, ref: string, userAgent?: string, referer?: string, forwardedFor?: string}}} data Data in publisher-friendly format to be published
21
21
  * @returns {*} Data returned by platform, if possible JSON-deserialized (otherwise as string)
@@ -0,0 +1,114 @@
1
+ const publisher = require("../index.js").publisher.Piano;
2
+ const defaultPublisher = require("../index.js").DefaultPublisher;
3
+ const utils = require("../index.js").utils;
4
+ var assert = require('assert');
5
+
6
+ describe('test Piano 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
+ })
50
+
51
+ /**
52
+ * Test function
53
+ * @param [object] object obsahující nastavení testovací parametry
54
+ */
55
+ async function test(eventHubMessages) {
56
+ try{
57
+ await defaultPublisher(console, eventHubMessages, "Piano", publisher, trigger);
58
+ }catch(err){
59
+ assert.equal(err.toString(), "FetchError: request to https://xxxxx.xiti.com/?s=12345&idclient=1657003393413.319297501&ref=https://crossmasters.com/ failed, reason: getaddrinfo ENOTFOUND xxxxx.xiti.com");
60
+ }
61
+ }
62
+
63
+ function trigger(data) {
64
+ const publishers = [];
65
+ const config = {
66
+ collectionDomain: "xxxxx.xiti.com", //Secured collection domain
67
+ siteId: "12345" //site-id
68
+ }
69
+
70
+ if (data.event.code === "purchase") {
71
+ const publisher = PianoPurchasePublisher;
72
+ publisher.register(publishers, config);
73
+ }
74
+ return publishers;
75
+ };
76
+
77
+ class PianoPurchasePublisher extends utils.EventPublisherBase {
78
+ /**
79
+ * Converts input data to publishing payload data understood by the publisher
80
+ * @param {Object} data `purchase` event data
81
+ * @returns {Object} Data mapped from incomming Event Hub message to platform payload
82
+ */
83
+ map(data) {
84
+ return {
85
+ pianoData: { //This willbe sent to Piano as payload
86
+ events: [
87
+ {
88
+ name: "purchase",
89
+ data: {
90
+ event_collection_platform: "api",
91
+ event_collection_version: 2,
92
+ event_time_ts: Math.floor(data.enqueuedAt / 1000), // convert to seconds
93
+ event_id: data.id,
94
+ event_source_url: data.location
95
+ //https://developers.atinternet-solutions.com/piano-analytics-tagging-en/event-tagging-piano-analytics-en/events-pattern-en/#common-contextual-properties_9
96
+ //Any properties can go here (name must be [a-z][a-z_0-9]+; prohibited names m_…, visit_…, events)
97
+ //There are some specifically understood properties
98
+ //Type of property if not obvious from JSON can be forcec by prefix: 's:' string, 'n:' integer, 'f:' float, 'd:' date, 'b:' boolean, 'a:s:' array of strings, 'a:n:' array of integers, 'a:f:' array of floats
99
+ //or by suffix '_date', '_utc', '_ts' date in RFC 3339 format (YYYY-MM-DDTHH:mm:ssZ); '_timestamp' - timestamp in seconds
100
+ }
101
+ }
102
+ ]
103
+ },
104
+ aux: { //Additional data understood by Piano publisher and passed specificaly (in headers and query string)
105
+ visitorId: data.identity?.piano?.id, //Mandatory idclient=<visitor-id>
106
+ ref: data.referrer, //Mandatory ref=<previous-url>
107
+ //Optional:
108
+ userAgent: data.session?.userAgent,
109
+ referer: data.session?.referrer,
110
+ forwardedFor: data.session?.ip,
111
+ }
112
+ };
113
+ }
114
+ };