@patternfly/chatbot 2.2.0-prerelease.43 → 2.2.0-prerelease.45

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 (52) hide show
  1. package/dist/cjs/SourcesCard/SourcesCard.d.ts +6 -1
  2. package/dist/cjs/SourcesCard/SourcesCard.js +14 -9
  3. package/dist/cjs/SourcesCard/SourcesCard.test.js +25 -11
  4. package/dist/cjs/tracking/console_tracking_provider.d.ts +4 -5
  5. package/dist/cjs/tracking/console_tracking_provider.js +22 -15
  6. package/dist/cjs/tracking/posthog_tracking_provider.d.ts +2 -2
  7. package/dist/cjs/tracking/posthog_tracking_provider.js +21 -12
  8. package/dist/cjs/tracking/segment_tracking_provider.d.ts +2 -2
  9. package/dist/cjs/tracking/segment_tracking_provider.js +21 -12
  10. package/dist/cjs/tracking/trackingProviderProxy.d.ts +1 -1
  11. package/dist/cjs/tracking/trackingProviderProxy.js +2 -2
  12. package/dist/cjs/tracking/tracking_api.d.ts +1 -1
  13. package/dist/cjs/tracking/tracking_registry.js +46 -12
  14. package/dist/cjs/tracking/tracking_spi.d.ts +15 -5
  15. package/dist/cjs/tracking/tracking_spi.js +9 -0
  16. package/dist/cjs/tracking/umami_tracking_provider.d.ts +6 -2
  17. package/dist/cjs/tracking/umami_tracking_provider.js +66 -22
  18. package/dist/css/main.css +3 -7
  19. package/dist/css/main.css.map +1 -1
  20. package/dist/esm/SourcesCard/SourcesCard.d.ts +6 -1
  21. package/dist/esm/SourcesCard/SourcesCard.js +15 -10
  22. package/dist/esm/SourcesCard/SourcesCard.test.js +25 -11
  23. package/dist/esm/tracking/console_tracking_provider.d.ts +4 -5
  24. package/dist/esm/tracking/console_tracking_provider.js +22 -15
  25. package/dist/esm/tracking/posthog_tracking_provider.d.ts +2 -2
  26. package/dist/esm/tracking/posthog_tracking_provider.js +21 -12
  27. package/dist/esm/tracking/segment_tracking_provider.d.ts +2 -2
  28. package/dist/esm/tracking/segment_tracking_provider.js +21 -12
  29. package/dist/esm/tracking/trackingProviderProxy.d.ts +1 -1
  30. package/dist/esm/tracking/trackingProviderProxy.js +2 -2
  31. package/dist/esm/tracking/tracking_api.d.ts +1 -1
  32. package/dist/esm/tracking/tracking_registry.js +46 -12
  33. package/dist/esm/tracking/tracking_spi.d.ts +15 -5
  34. package/dist/esm/tracking/tracking_spi.js +8 -1
  35. package/dist/esm/tracking/umami_tracking_provider.d.ts +6 -2
  36. package/dist/esm/tracking/umami_tracking_provider.js +66 -22
  37. package/package.json +1 -1
  38. package/patternfly-docs/content/extensions/chatbot/examples/Analytics/Analytics.md +18 -14
  39. package/patternfly-docs/content/extensions/chatbot/examples/Messages/MessageWithSources.tsx +34 -5
  40. package/patternfly-docs/content/extensions/chatbot/examples/Messages/Messages.md +1 -1
  41. package/patternfly-docs/content/extensions/chatbot/examples/demos/Chatbot.tsx +4 -3
  42. package/src/SourcesCard/SourcesCard.scss +3 -7
  43. package/src/SourcesCard/SourcesCard.test.tsx +30 -17
  44. package/src/SourcesCard/SourcesCard.tsx +41 -11
  45. package/src/tracking/console_tracking_provider.ts +21 -17
  46. package/src/tracking/posthog_tracking_provider.ts +20 -13
  47. package/src/tracking/segment_tracking_provider.ts +20 -13
  48. package/src/tracking/trackingProviderProxy.ts +2 -2
  49. package/src/tracking/tracking_api.ts +1 -1
  50. package/src/tracking/tracking_registry.ts +46 -13
  51. package/src/tracking/tracking_spi.ts +18 -7
  52. package/src/tracking/umami_tracking_provider.ts +76 -20
@@ -5,13 +5,14 @@ import { InitProps, TrackingSpi } from './tracking_spi';
5
5
 
6
6
  export class SegmentTrackingProvider implements TrackingSpi, TrackingApi {
7
7
  private analytics: AnalyticsBrowser | undefined;
8
- getKey(): string {
9
- return 'segmentKey';
10
- }
8
+ private verbose = false;
11
9
 
12
10
  initialize(props: InitProps): void {
13
- // eslint-disable-next-line no-console
14
- console.log('SegmentProvider initialize');
11
+ this.verbose = props.verbose;
12
+ if (this.verbose) {
13
+ // eslint-disable-next-line no-console
14
+ console.log('SegmentProvider initialize');
15
+ }
15
16
  const segmentKey = props.segmentKey as string;
16
17
 
17
18
  // We need to create an object here, as ts lint is unhappy otherwise
@@ -32,17 +33,21 @@ export class SegmentTrackingProvider implements TrackingSpi, TrackingApi {
32
33
  );
33
34
  }
34
35
 
35
- identify(userID: string): void {
36
- // eslint-disable-next-line no-console
37
- console.log('SegmentProvider userID: ' + userID);
36
+ identify(userID: string, userProperties: TrackingEventProperties = {}): void {
37
+ if (this.verbose) {
38
+ // eslint-disable-next-line no-console
39
+ console.log('SegmentProvider userID: ' + userID);
40
+ }
38
41
  if (this.analytics) {
39
- this.analytics.identify(userID);
42
+ this.analytics.identify(userID, userProperties);
40
43
  }
41
44
  }
42
45
 
43
46
  trackPageView(url: string | undefined): void {
44
- // eslint-disable-next-line no-console
45
- console.log('SegmentProvider url', url);
47
+ if (this.verbose) {
48
+ // eslint-disable-next-line no-console
49
+ console.log('SegmentProvider url ', url);
50
+ }
46
51
  if (this.analytics) {
47
52
  if (url) {
48
53
  this.analytics.page(url);
@@ -53,8 +58,10 @@ export class SegmentTrackingProvider implements TrackingSpi, TrackingApi {
53
58
  }
54
59
 
55
60
  trackSingleItem(item: string, properties?: TrackingEventProperties): void {
56
- // eslint-disable-next-line no-console
57
- console.log('SegmentProvider: trackSingleItem' + item, properties);
61
+ if (this.verbose) {
62
+ // eslint-disable-next-line no-console
63
+ console.log('SegmentProvider: trackSingleItem ' + item, properties);
64
+ }
58
65
  if (this.analytics) {
59
66
  this.analytics.track(item, { properties });
60
67
  }
@@ -6,9 +6,9 @@ class TrackingProviderProxy implements TrackingApi {
6
6
  this.providers = providers;
7
7
  }
8
8
 
9
- identify(userID: string): void {
9
+ identify(userID: string, userProperties: TrackingEventProperties = {}): void {
10
10
  for (const provider of this.providers) {
11
- provider.identify(userID);
11
+ provider.identify(userID, userProperties);
12
12
  }
13
13
  }
14
14
 
@@ -3,7 +3,7 @@ export interface TrackingEventProperties {
3
3
  }
4
4
 
5
5
  export interface TrackingApi {
6
- identify: (userID: string) => void;
6
+ identify: (userID: string, userProperties: TrackingEventProperties) => void;
7
7
 
8
8
  trackPageView: (url: string | undefined) => void;
9
9
 
@@ -1,4 +1,4 @@
1
- import { InitProps, TrackingSpi } from './tracking_spi';
1
+ import { InitProps, Providers, TrackingSpi } from './tracking_spi';
2
2
  import { TrackingApi } from './tracking_api';
3
3
  import TrackingProviderProxy from './trackingProviderProxy';
4
4
  import { ConsoleTrackingProvider } from './console_tracking_provider';
@@ -8,26 +8,59 @@ import { UmamiTrackingProvider } from './umami_tracking_provider';
8
8
 
9
9
  export const getTrackingProviders = (initProps: InitProps): TrackingApi => {
10
10
  const providers: TrackingSpi[] = [];
11
- providers.push(new SegmentTrackingProvider());
12
- providers.push(new PosthogTrackingProvider());
13
- providers.push(new UmamiTrackingProvider());
14
11
 
15
- // TODO dynamically find and register providers
12
+ if (initProps.activeProviders) {
13
+ let tmpProps: string[] = initProps.activeProviders;
14
+
15
+ // Theoretically we get an array of provider names, but it could also be a CSV string...
16
+ if (!Array.isArray(initProps.activeProviders)) {
17
+ const tmpString = initProps.activeProviders as string;
18
+ if (tmpString && tmpString.indexOf(',') !== -1) {
19
+ tmpProps = tmpString.split(',');
20
+ } else {
21
+ tmpProps = [tmpString];
22
+ }
23
+ }
24
+
25
+ tmpProps.forEach((provider) => {
26
+ switch (Providers[provider]) {
27
+ case Providers.Segment:
28
+ providers.push(new SegmentTrackingProvider());
29
+ break;
30
+ case Providers.Umami:
31
+ providers.push(new UmamiTrackingProvider());
32
+ break;
33
+ case Providers.Posthog:
34
+ providers.push(new PosthogTrackingProvider());
35
+ break;
36
+ case Providers.Console:
37
+ providers.push(new ConsoleTrackingProvider());
38
+ break;
39
+ case Providers.None: // Do nothing, just a placeholder
40
+ break;
41
+ default:
42
+ if (providers.length > 1) {
43
+ if (initProps.verbose) {
44
+ // eslint-disable-next-line no-console
45
+ console.error("Unknown provider '" + provider);
46
+ }
47
+ }
48
+ break;
49
+ }
50
+ });
51
+ }
16
52
 
17
53
  // Initialize them
18
- const enabledProviders: TrackingSpi[] = [];
19
54
  for (const provider of providers) {
20
- const key = provider.getKey();
21
- if (Object.keys(initProps).indexOf(key) > -1) {
55
+ try {
22
56
  provider.initialize(initProps);
23
- enabledProviders.push(provider);
57
+ } catch (e) {
58
+ // eslint-disable-next-line no-console
59
+ console.error(e);
24
60
  }
25
61
  }
26
- // Add the console provider
27
- const consoleTrackingProvider = new ConsoleTrackingProvider();
28
- enabledProviders.push(consoleTrackingProvider); // TODO noop- provider?
29
62
 
30
- return new TrackingProviderProxy(enabledProviders);
63
+ return new TrackingProviderProxy(providers);
31
64
  };
32
65
 
33
66
  export default getTrackingProviders;
@@ -1,14 +1,25 @@
1
- import { TrackingApi, TrackingEventProperties } from './tracking_api';
1
+ import { TrackingApi } from './tracking_api';
2
2
 
3
- export interface InitProps {
4
- [key: string]: string | number | boolean;
3
+ export enum Providers {
4
+ None,
5
+ Segment,
6
+ Umami,
7
+ Posthog,
8
+ Console
9
+ }
10
+
11
+ export type ProviderAsString = keyof typeof Providers;
12
+
13
+ export interface BaseProps {
14
+ verbose: boolean;
15
+ activeProviders: [ProviderAsString];
5
16
  }
6
17
 
18
+ export type InitProps = {
19
+ [key: string]: string | number | boolean;
20
+ } & BaseProps;
21
+
7
22
  export interface TrackingSpi extends TrackingApi {
8
- // Return a key in InitProps to check if the provided should be enabled
9
- getKey: () => string;
10
23
  // Initialize the provider
11
24
  initialize: (props: InitProps) => void;
12
- // Track a single item
13
- trackSingleItem: (item: string, properties?: TrackingEventProperties) => void;
14
25
  }
@@ -8,15 +8,25 @@ declare global {
8
8
  }
9
9
  }
10
10
 
11
+ // Items in a queue.
12
+ // We need to queue up requests until the script is fully loaded
13
+ interface queueT {
14
+ what: 'i' | 't' | 'p'; // identify, track, pageview
15
+ name: string;
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ payload?: any;
18
+ }
19
+
11
20
  export class UmamiTrackingProvider implements TrackingSpi, TrackingApi {
12
- getKey(): string {
13
- return 'umamiKey';
14
- }
21
+ private verbose = false;
22
+ private websiteId: string | undefined;
23
+ private queue: queueT[] = [];
15
24
 
16
25
  initialize(props: InitProps): void {
17
- // eslint-disable-next-line no-console
18
- console.log('UmamiProvider initialize');
19
- const umamiKey = props.umamiKey as string;
26
+ this.verbose = props.verbose;
27
+ this.log('UmamiProvider initialize');
28
+
29
+ this.websiteId = props.umamiKey as string;
20
30
  const hostUrl = props.umamiHostUrl as string;
21
31
 
22
32
  const script = document.createElement('script');
@@ -25,30 +35,76 @@ export class UmamiTrackingProvider implements TrackingSpi, TrackingApi {
25
35
  script.defer = true;
26
36
 
27
37
  // Configure Umami properties
28
- script.setAttribute('data-website-id', umamiKey);
29
- script.setAttribute('data-domains', 'localhost'); // TODO ?
38
+ script.setAttribute('data-website-id', this.websiteId);
39
+ script.setAttribute('data-host-url', hostUrl);
30
40
  script.setAttribute('data-auto-track', 'false');
31
- script.setAttribute('data-host-url', hostUrl); // TODO ?
32
- script.setAttribute('data-exclude-search', 'false'); // TODO ?
41
+ script.setAttribute('data-exclude-search', 'false');
42
+
43
+ // Now get from config, which may override some of the above.
44
+ const UMAMI_PREFIX = 'umami-';
45
+ for (const prop in props) {
46
+ if (prop.startsWith(UMAMI_PREFIX)) {
47
+ const att = 'data-' + prop.substring(UMAMI_PREFIX.length);
48
+ const val = props[prop];
49
+ script.setAttribute(att, String(val));
50
+ }
51
+ }
52
+ script.onload = () => {
53
+ this.log('UmamiProvider script loaded');
54
+ this.flushQueue();
55
+ };
33
56
 
34
57
  document.body.appendChild(script);
35
58
  }
36
59
 
37
- identify(userID: string): void {
38
- // eslint-disable-next-line no-console
39
- console.log('UmamiProvider userID: ' + userID);
40
- window.umami?.identify({ userID });
60
+ identify(userID: string, userProperties: TrackingEventProperties = {}): void {
61
+ this.log('UmamiProvider userID: ' + userID + ' => ' + JSON.stringify(userProperties));
62
+ if (window.umami) {
63
+ window.umami.identify({ userID, userProperties });
64
+ } else {
65
+ this.queue.push({ what: 'i', name: userID, payload: userProperties });
66
+ }
41
67
  }
42
68
 
43
69
  trackPageView(url: string | undefined): void {
44
- // eslint-disable-next-line no-console
45
- console.log('UmamiProvider url', url);
46
- window.umami?.track({ url });
70
+ this.log('UmamiProvider url ' + url);
71
+ if (window.umami) {
72
+ window.umami.track({ url, website: this.websiteId });
73
+ } else {
74
+ this.queue.push({ what: 'p', name: String(url) });
75
+ }
47
76
  }
48
77
 
49
78
  trackSingleItem(item: string, properties?: TrackingEventProperties): void {
50
- // eslint-disable-next-line no-console
51
- console.log('UmamiProvider: trackSingleItem' + item, properties);
52
- window.umami?.track(item, properties);
79
+ this.log('UmamiProvider: trackSingleItem ' + item + JSON.stringify(properties));
80
+ if (window.umami) {
81
+ window.umami.track(item, properties);
82
+ } else {
83
+ this.queue.push({ what: 't', name: item, payload: properties });
84
+ }
85
+ }
86
+
87
+ flushQueue(): void {
88
+ for (const item of this.queue) {
89
+ this.log('Queue flush ' + JSON.stringify(item));
90
+ switch (item.what) {
91
+ case 'i':
92
+ this.identify(item.name, item.payload);
93
+ break;
94
+ case 't':
95
+ this.trackSingleItem(item.name, item.payload);
96
+ break;
97
+ case 'p':
98
+ this.trackPageView(item.name);
99
+ break;
100
+ }
101
+ }
102
+ }
103
+
104
+ log(msg: string): void {
105
+ if (this.verbose) {
106
+ // eslint-disable-next-line no-console
107
+ console.debug('UmamiProvider: ', msg);
108
+ }
53
109
  }
54
110
  }