@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
@@ -0,0 +1,67 @@
1
+ import * as utils from './../utils/utils';
2
+ import {
3
+ PlatformPublisher,
4
+ PlatformRequestOptions,
5
+ } from '../types/PlatformPublisher';
6
+ import { TypeSafeObject } from '../types/UtilTypes';
7
+
8
+ interface CjData {
9
+ cjevent?: unknown;
10
+ [x: string]: unknown;
11
+ }
12
+
13
+ export const cj: PlatformPublisher = {
14
+ source: 'CJAffiliate',
15
+ /**
16
+ * Publishes data to CJ
17
+ * @param log Azure Function context.log
18
+ * @param config Publisher configuration
19
+ * @param data Data in publisher-friendly format to be published
20
+ * @param event Event for which the function is triggered
21
+ */
22
+ async publish(log, config, data, event) {
23
+ if (!(data as CjData).cjevent) {
24
+ utils.log(log, 1, `CJ - ${event} - skipped because CJEVENT is missing`);
25
+ // ? Tady se returnoval false, ale nemyslim, ze to neco pouzivalo
26
+ } else {
27
+ if (typeof config.cid === 'string' && typeof config.type === 'string') {
28
+ const cjData = utils.removeUndefined(data) as TypeSafeObject;
29
+ let query = '?cid=' + encodeURI(config.cid);
30
+ query += '&type=' + encodeURI(config.type);
31
+ for (const property in data) {
32
+ if (
33
+ Object.prototype.hasOwnProperty.call(cjData, property) &&
34
+ cjData[property] !== undefined
35
+ ) {
36
+ query +=
37
+ '&' +
38
+ encodeURI(property) +
39
+ '=' +
40
+ encodeURI(
41
+ cjData[property] === null
42
+ ? ''
43
+ : // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
44
+ (cjData[property]?.toString() as string),
45
+ );
46
+ }
47
+ }
48
+ const baseUrl = 'https://www.kdukvh.com/u';
49
+
50
+ const requestOptions: PlatformRequestOptions = {
51
+ method: 'GET',
52
+ };
53
+
54
+ await utils.handlePlatformRequest({
55
+ source: this.source,
56
+ baseUrl,
57
+ query,
58
+ requestOptions,
59
+ event,
60
+ logger: log,
61
+ });
62
+ } else {
63
+ throw new Error('Invalid config for platform ' + this.source);
64
+ }
65
+ }
66
+ },
67
+ };
@@ -0,0 +1,49 @@
1
+ import * as utils from './../utils/utils';
2
+ import {
3
+ PlatformPublisher,
4
+ PlatformRequestOptions,
5
+ } from '../types/PlatformPublisher';
6
+
7
+ export const facebook: PlatformPublisher = {
8
+ source: 'Faceboook',
9
+ /**
10
+ *Publishes data to Facebook
11
+ * @param log Azure Function context.log
12
+ * @param config Publisher configuration
13
+ * @param data Data in publisher-friendly format to be published
14
+ * @param event Event for which the function is triggered
15
+ */
16
+ async publish(log, config, data, event) {
17
+ if (
18
+ typeof config.id === 'string' &&
19
+ typeof config.access_token === 'string'
20
+ ) {
21
+ const requestOptions: PlatformRequestOptions = {
22
+ method: 'POST',
23
+ body: JSON.stringify({ data: [utils.removeUndefined(data)] }),
24
+ headers: {
25
+ // eslint-disable-next-line @typescript-eslint/naming-convention
26
+ 'Content-Type': 'application/json',
27
+ },
28
+ };
29
+
30
+ // https://graph.facebook.com/v11.0/<id>/events
31
+ const baseUrl = `https://graph.facebook.com/v11.0/${encodeURIComponent(
32
+ config.id,
33
+ )}/events`;
34
+
35
+ const query = `?access_token=${encodeURI(config.access_token)}`;
36
+
37
+ await utils.handlePlatformRequest({
38
+ source: this.source,
39
+ baseUrl,
40
+ query,
41
+ requestOptions,
42
+ event,
43
+ logger: log,
44
+ });
45
+ } else {
46
+ throw new Error('Invalid config for platform ' + this.source);
47
+ }
48
+ },
49
+ };
@@ -0,0 +1,60 @@
1
+ import * as utils from './../utils/utils';
2
+ import {
3
+ PlatformPublisher,
4
+ PlatformRequestOptions,
5
+ } from '../types/PlatformPublisher';
6
+ import { TypeSafeObject } from '../types/UtilTypes';
7
+
8
+ export const googleAnalytics4: PlatformPublisher = {
9
+ source: 'GoogleAnalytics4',
10
+ /**
11
+ * Publishes data to Google Analytics
12
+ * @param log zure Function context.log
13
+ * @param config Publisher configuration
14
+ * @param data Data in publisher-friendly format to be published
15
+ * @param event Event for which the function is triggered
16
+ */
17
+ async publish(log, config, data, event) {
18
+ if (config.trackingId) {
19
+ const ga4Data = utils.removeUndefined(data) as TypeSafeObject;
20
+ ga4Data.v = 2;
21
+ ga4Data.seg = 1;
22
+ ga4Data.gcs = 111;
23
+ ga4Data.tid = config.trackingId;
24
+
25
+ let payload = '';
26
+
27
+ for (const property in ga4Data) {
28
+ if (Object.prototype.hasOwnProperty.call(ga4Data, property)) {
29
+ if (payload) {
30
+ payload += '&';
31
+ }
32
+ payload += encodeURI(property) + '=' + encodeURI(ga4Data[property]);
33
+ }
34
+ }
35
+
36
+ const requestOptions: PlatformRequestOptions = {
37
+ method: 'POST',
38
+ headers: {
39
+ // eslint-disable-next-line @typescript-eslint/naming-convention
40
+ 'User-Agent': 'mHub Cloud',
41
+ },
42
+ };
43
+
44
+ const baseUrl = 'https://www.google-analytics.com/g/collect';
45
+
46
+ const query = `?${payload}`;
47
+
48
+ await utils.handlePlatformRequest({
49
+ source: this.source,
50
+ baseUrl,
51
+ query,
52
+ requestOptions,
53
+ event,
54
+ logger: log,
55
+ });
56
+ } else {
57
+ throw new Error('Invalid config for platform ' + this.source);
58
+ }
59
+ },
60
+ };
@@ -0,0 +1,71 @@
1
+ import * as utils from './../utils/utils';
2
+ import {
3
+ PlatformPublisher,
4
+ PlatformRequestOptions,
5
+ } from '../types/PlatformPublisher';
6
+
7
+ interface Aux {
8
+ visitorId: string;
9
+ ref: string;
10
+ userAgent?: string;
11
+ referer?: string;
12
+ forwardedFor?: string;
13
+ }
14
+
15
+ interface PianoData {
16
+ aux: Aux;
17
+ }
18
+
19
+ export const piano: PlatformPublisher = {
20
+ source: 'Piano',
21
+ /**
22
+ * Publishes data to Piano
23
+ * @param log zure Function context.log
24
+ * @param config Publisher configuration
25
+ * @param data Data in publisher-friendly format to be published
26
+ * @param event Event for which the function is triggered
27
+ */
28
+ async publish(log, config, data, event) {
29
+ if (
30
+ typeof config.siteId === 'string' &&
31
+ typeof config.collectionDomain === 'string'
32
+ ) {
33
+ const pianoData = utils.removeUndefined(data) as PianoData;
34
+ let query = '';
35
+ // https://developers.atinternet-solutions.com/piano-analytics-tagging-en/event-tagging-piano-analytics-en/events-pattern-en/#post-request_5
36
+ // https://<collection-domain>/<path>?s=<site-id>&idclient=<visitor-id>&ref=<previous-url>
37
+ query = '?s=' + encodeURI(config.siteId);
38
+ query += '&idclient=' + encodeURI(pianoData.aux.visitorId);
39
+ query += '&ref=' + encodeURI(pianoData.aux.ref);
40
+
41
+ const requestOptions: PlatformRequestOptions = {
42
+ method: 'POST',
43
+ };
44
+
45
+ requestOptions.headers = {};
46
+
47
+ if (pianoData.aux.userAgent) {
48
+ requestOptions.headers['User-Agent'] = pianoData.aux.userAgent;
49
+ }
50
+ if (pianoData.aux.referer) {
51
+ requestOptions.headers.Referer = pianoData.aux.referer;
52
+ }
53
+ if (pianoData.aux.forwardedFor) {
54
+ requestOptions.headers['X-Forwarded-For'] = pianoData.aux.forwardedFor;
55
+ }
56
+
57
+ const baseUrl = `https://${config.collectionDomain}/`;
58
+
59
+ await utils.handlePlatformRequest({
60
+ source: this.source,
61
+ baseUrl,
62
+ query,
63
+ requestOptions,
64
+ event,
65
+ logger: log,
66
+ });
67
+ } else {
68
+ throw new Error('Invalid config for platform ' + this.source);
69
+ }
70
+ },
71
+ };
@@ -0,0 +1,63 @@
1
+ import * as utils from './../utils/utils';
2
+ import {
3
+ PlatformPublisher,
4
+ PlatformRequestOptions,
5
+ } from '../types/PlatformPublisher';
6
+ import { TypeSafeObject } from '../types/UtilTypes';
7
+
8
+ export const universalAnalytics: PlatformPublisher = {
9
+ source: 'UniversalAnalytics',
10
+ /**
11
+ * Publishes data to UniversalAnalytics
12
+ * @param log zure Function context.log
13
+ * @param config Publisher configuration
14
+ * @param data Data in publisher-friendly format to be published
15
+ * @param event Event for which the function is triggered
16
+ */
17
+ async publish(log, config, data, event) {
18
+ if (config.trackingId) {
19
+ const uaData = utils.removeUndefined(data) as TypeSafeObject;
20
+ uaData.v = 1;
21
+ uaData.tid = config.trackingId;
22
+
23
+ let payload = '';
24
+
25
+ for (const property in uaData) {
26
+ if (Object.prototype.hasOwnProperty.call(uaData, property)) {
27
+ if (payload) {
28
+ payload += '&';
29
+ }
30
+ if (uaData[property]) {
31
+ payload +=
32
+ encodeURI(property) + '=' + encodeURI(uaData[property] ?? '');
33
+ }
34
+ }
35
+ }
36
+
37
+ const requestOptions: PlatformRequestOptions = {
38
+ method: 'POST',
39
+ body: payload,
40
+ headers: {
41
+ // eslint-disable-next-line @typescript-eslint/naming-convention
42
+ 'Content-Type': 'application/x-www-form-urlencoded',
43
+ // eslint-disable-next-line @typescript-eslint/naming-convention
44
+ 'User-Agent': 'mHub Cloud',
45
+ },
46
+ };
47
+ const baseUrl = 'https://www.google-analytics.com/collect';
48
+
49
+ const query = `?${payload}`;
50
+
51
+ await utils.handlePlatformRequest({
52
+ source: this.source,
53
+ baseUrl,
54
+ query,
55
+ requestOptions,
56
+ event,
57
+ logger: log,
58
+ });
59
+ } else {
60
+ throw new Error('Invalid config for platform ' + this.source);
61
+ }
62
+ },
63
+ };
@@ -0,0 +1,8 @@
1
+ export interface EventHubMessage {
2
+ enqueuedAt: number;
3
+ event: {
4
+ code: string;
5
+ name?: string;
6
+ };
7
+ [x: string]: unknown;
8
+ }
@@ -0,0 +1,9 @@
1
+ import { EventHubMessage } from './EventHubMessage';
2
+
3
+ export interface EventPublisherBaseClass {
4
+ config: Record<string, unknown>;
5
+ }
6
+
7
+ export interface EventPublisher extends EventPublisherBaseClass {
8
+ map: (data: EventHubMessage) => Promise<Record<string, unknown>>;
9
+ }
@@ -0,0 +1,19 @@
1
+ import { HttpMethod, Logger } from '@azure/functions';
2
+ import { BodyInit } from 'node-fetch';
3
+ import { TypeSafeObject } from './UtilTypes';
4
+
5
+ export interface PlatformPublisher {
6
+ source: string;
7
+ publish: (
8
+ log: Logger,
9
+ config: TypeSafeObject,
10
+ data: TypeSafeObject | unknown[],
11
+ event: string,
12
+ ) => Promise<void>;
13
+ }
14
+
15
+ export interface PlatformRequestOptions {
16
+ method: HttpMethod | undefined;
17
+ body?: BodyInit | undefined;
18
+ headers?: Record<string, string>;
19
+ }
@@ -0,0 +1 @@
1
+ export type TypeSafeObject = Record<string, unknown>;
@@ -0,0 +1,3 @@
1
+ declare namespace globalThis {
2
+ function it(): unknown;
3
+ }