@crimson-education/browser-logger 2.0.2 → 3.0.1-pinpoint-middleware.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 (79) hide show
  1. package/README.md +212 -67
  2. package/lib/index.d.ts +12 -82
  3. package/lib/index.d.ts.map +1 -1
  4. package/lib/index.js +69 -213
  5. package/lib/index.js.map +1 -1
  6. package/lib/logger/consoleTransport.d.ts +37 -0
  7. package/lib/logger/consoleTransport.d.ts.map +1 -0
  8. package/lib/logger/consoleTransport.js +81 -0
  9. package/lib/logger/consoleTransport.js.map +1 -0
  10. package/lib/logger/datadogTransport.d.ts +8 -0
  11. package/lib/logger/datadogTransport.d.ts.map +1 -0
  12. package/lib/logger/datadogTransport.js +21 -0
  13. package/lib/logger/datadogTransport.js.map +1 -0
  14. package/lib/logger/index.d.ts +16 -0
  15. package/lib/logger/index.d.ts.map +1 -0
  16. package/lib/logger/index.js +148 -0
  17. package/lib/logger/index.js.map +1 -0
  18. package/lib/logger/index.test.d.ts +2 -0
  19. package/lib/logger/index.test.d.ts.map +1 -0
  20. package/lib/logger/index.test.js +60 -0
  21. package/lib/logger/index.test.js.map +1 -0
  22. package/lib/logger/utils.d.ts +15 -0
  23. package/lib/logger/utils.d.ts.map +1 -0
  24. package/lib/logger/utils.js +32 -0
  25. package/lib/logger/utils.js.map +1 -0
  26. package/lib/reporters/amplifyReporter.d.ts +83 -77
  27. package/lib/reporters/amplifyReporter.d.ts.map +1 -1
  28. package/lib/reporters/amplifyReporter.js +162 -151
  29. package/lib/reporters/amplifyReporter.js.map +1 -1
  30. package/lib/reporters/amplifyReporter.test.d.ts +1 -1
  31. package/lib/reporters/amplifyReporter.test.js +50 -50
  32. package/lib/reporters/datadogReporter.d.ts +76 -74
  33. package/lib/reporters/datadogReporter.d.ts.map +1 -1
  34. package/lib/reporters/datadogReporter.js +122 -185
  35. package/lib/reporters/datadogReporter.js.map +1 -1
  36. package/lib/reporters/gtmReporter.d.ts +8 -8
  37. package/lib/reporters/gtmReporter.d.ts.map +1 -1
  38. package/lib/reporters/gtmReporter.js +56 -61
  39. package/lib/reporters/gtmReporter.js.map +1 -1
  40. package/lib/reporters/index.d.ts +66 -3
  41. package/lib/reporters/index.d.ts.map +1 -1
  42. package/lib/reporters/index.js +212 -19
  43. package/lib/reporters/index.js.map +1 -1
  44. package/lib/reporters/logReporter.d.ts +35 -0
  45. package/lib/reporters/logReporter.d.ts.map +1 -0
  46. package/lib/reporters/logReporter.js +62 -0
  47. package/lib/reporters/logReporter.js.map +1 -0
  48. package/lib/types/index.d.ts +2 -2
  49. package/lib/types/index.js +18 -18
  50. package/lib/types/logger.d.ts +77 -36
  51. package/lib/types/logger.d.ts.map +1 -1
  52. package/lib/types/logger.js +10 -10
  53. package/lib/types/logger.js.map +1 -1
  54. package/lib/types/reporter.d.ts +154 -102
  55. package/lib/types/reporter.d.ts.map +1 -1
  56. package/lib/types/reporter.js +2 -2
  57. package/lib/utils.d.ts +9 -8
  58. package/lib/utils.d.ts.map +1 -1
  59. package/lib/utils.js +31 -51
  60. package/lib/utils.js.map +1 -1
  61. package/lib/utils.test.d.ts +1 -1
  62. package/lib/utils.test.js +31 -18
  63. package/lib/utils.test.js.map +1 -1
  64. package/package.json +1 -1
  65. package/src/index.ts +38 -208
  66. package/src/logger/consoleTransport.ts +101 -0
  67. package/src/logger/datadogTransport.ts +20 -0
  68. package/src/logger/index.test.ts +68 -0
  69. package/src/logger/index.ts +139 -0
  70. package/src/logger/utils.ts +28 -0
  71. package/src/reporters/amplifyReporter.ts +37 -17
  72. package/src/reporters/datadogReporter.ts +48 -116
  73. package/src/reporters/gtmReporter.ts +2 -7
  74. package/src/reporters/index.ts +216 -3
  75. package/src/reporters/logReporter.ts +86 -0
  76. package/src/types/logger.ts +49 -4
  77. package/src/types/reporter.ts +66 -6
  78. package/src/utils.test.ts +20 -6
  79. package/src/utils.ts +37 -62
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/logger/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const _1 = require(".");
4
+ const __1 = require("..");
5
+ const reporters_1 = require("../reporters");
6
+ const types_1 = require("../types");
7
+ // This isn't actually asserting anything.
8
+ // But it ensures that the logs look correct, and no errors get thrown.
9
+ describe('logger', () => {
10
+ const logger = (0, _1.createLogger)({
11
+ metadata: {
12
+ service: 'test',
13
+ },
14
+ });
15
+ // Manually set the log transports.
16
+ _1.logTransports.length = 0;
17
+ const transport = (0, _1.consoleTransport)({
18
+ // So we can see everything.
19
+ ignoreMetadataPatterns: [],
20
+ });
21
+ _1.logTransports.push(transport);
22
+ it('should log', async () => {
23
+ logger.log({
24
+ level: 'info',
25
+ message: 'data',
26
+ });
27
+ logger.log({ data: 'obj' });
28
+ logger.log(types_1.LogLevel.Debug, 'level, message', { data: 'obj' });
29
+ logger.log(types_1.LogLevel.Debug, 'level, message, metadata', { data: 'obj' });
30
+ (0, __1.init)({
31
+ service: 'test-service',
32
+ environment: 'test',
33
+ version: 'no-version',
34
+ defaultMetadata: {
35
+ application: 'test',
36
+ },
37
+ });
38
+ const infoLogger = logger.child({ isLevel: 'info' });
39
+ infoLogger.info('info call, message');
40
+ infoLogger.info('info call, message, metadata', { data: 'metadataobj' });
41
+ const timer = logger.startTimer();
42
+ await new Promise((resolve) => setTimeout(resolve, 1000));
43
+ timer.done({ message: 'timer done', metadata: { id: 1 } });
44
+ (0, _1.setLogLevel)(types_1.LogLevel.Info);
45
+ (0, reporters_1.addMetadata)({ global: 'metadata' });
46
+ logger.debug('You should not see this');
47
+ logger.info('You should see this with { global: "metadata" }');
48
+ (0, _1.setLogLevel)(null);
49
+ (0, reporters_1.setUser)({
50
+ id: '123',
51
+ email: 'test@example.com',
52
+ });
53
+ logger.debug('debug call, user metadata');
54
+ transport.logLevel = types_1.LogLevel.Info;
55
+ logger.debug('You should not see this');
56
+ logger.info('You should see this');
57
+ transport.logLevel = undefined;
58
+ });
59
+ });
60
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/logger/index.test.ts"],"names":[],"mappings":";;AAAA,wBAAoG;AACpG,0BAA0B;AAC1B,4CAAoD;AACpD,oCAAoC;AAEpC,0CAA0C;AAC1C,uEAAuE;AACvE,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,MAAM,MAAM,GAAG,IAAA,eAAY,EAAC;QAC1B,QAAQ,EAAE;YACR,OAAO,EAAE,MAAM;SAChB;KACF,CAAC,CAAC;IAEH,mCAAmC;IACnC,gBAAa,CAAC,MAAM,GAAG,CAAC,CAAC;IACzB,MAAM,SAAS,GAAG,IAAA,mBAAgB,EAAC;QACjC,4BAA4B;QAC5B,sBAAsB,EAAE,EAAE;KAC3B,CAAC,CAAC;IACH,gBAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAE9B,EAAE,CAAC,YAAY,EAAE,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,GAAG,CAAC;YACT,KAAK,EAAE,MAAM;YACb,OAAO,EAAE,MAAM;SAChB,CAAC,CAAC;QACH,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5B,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,gBAAgB,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9D,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,0BAA0B,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAExE,IAAA,QAAI,EAAC;YACH,OAAO,EAAE,cAAc;YACvB,WAAW,EAAE,MAAM;YACnB,OAAO,EAAE,YAAY;Y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CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { LogLevel } from '../types';
2
+ /**
3
+ * Checks if a log level is above the global log level.
4
+ * @param level The log level to check
5
+ * @param checkLevel THe log level to check against
6
+ * @returns Is above the checkLevel?
7
+ */
8
+ export declare function isAboveLevel(level: LogLevel, checkLevel: LogLevel): boolean;
9
+ /**
10
+ * Ensures a message output is a string.
11
+ * @param message The message to assert is/convert to a string
12
+ * @returns The message as a string
13
+ */
14
+ export declare function getLogMessage(message: any): string;
15
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/logger/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,GAAG,OAAO,CAG3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,CASlD"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLogMessage = exports.isAboveLevel = void 0;
4
+ const types_1 = require("../types");
5
+ /**
6
+ * Checks if a log level is above the global log level.
7
+ * @param level The log level to check
8
+ * @param checkLevel THe log level to check against
9
+ * @returns Is above the checkLevel?
10
+ */
11
+ function isAboveLevel(level, checkLevel) {
12
+ const levels = [types_1.LogLevel.Debug, types_1.LogLevel.Info, types_1.LogLevel.Warn, types_1.LogLevel.Error];
13
+ return levels.indexOf(level) >= levels.indexOf(checkLevel);
14
+ }
15
+ exports.isAboveLevel = isAboveLevel;
16
+ /**
17
+ * Ensures a message output is a string.
18
+ * @param message The message to assert is/convert to a string
19
+ * @returns The message as a string
20
+ */
21
+ function getLogMessage(message) {
22
+ if (typeof message === 'string') {
23
+ return message;
24
+ }
25
+ if (typeof message === 'object' && message !== null) {
26
+ const data = { ...message };
27
+ return JSON.stringify(data);
28
+ }
29
+ return String(message);
30
+ }
31
+ exports.getLogMessage = getLogMessage;
32
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/logger/utils.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AAEpC;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAAe,EAAE,UAAoB;IAChE,MAAM,MAAM,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,gBAAQ,CAAC,IAAI,EAAE,gBAAQ,CAAC,IAAI,EAAE,gBAAQ,CAAC,KAAK,CAAC,CAAC;IAC9E,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAC7D,CAAC;AAHD,oCAGC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,OAAY;IACxC,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC/B,OAAO,OAAO,CAAC;KAChB;IACD,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE;QACnD,MAAM,IAAI,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC;QAC5B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;KAC7B;IACD,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC;AATD,sCASC"}
@@ -1,78 +1,84 @@
1
- import { IReporter, ReporterConfigBase, ServiceInfo } from '../types';
2
- import { AttributeMap } from '../utils';
3
- export interface AmplifyReporterConfig extends ReporterConfigBase {
4
- /**
5
- * AWS Region for Amplify.
6
- */
7
- region: string;
8
- /**
9
- * The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
10
- * This must be called manually for the reporter to work.
11
- */
12
- identityPoolId: string | false;
13
- /**
14
- * The Pinpoint App Id to report to.
15
- */
16
- analyticsAppId: string;
17
- /**
18
- * The Cognito User Pool to configure in Auth.configure.
19
- * If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
20
- */
21
- userPoolId?: string;
22
- /**
23
- * The Cognito Web Client Id to configure in Auth.configure.
24
- * If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
25
- */
26
- userPoolWebClientId?: string;
27
- /**
28
- * If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
29
- * It will automatically send events containing url information when the page is visited.
30
- */
31
- autoTrackPageViews?: boolean;
32
- /**
33
- * If you want to track user interactions with elements on the page, you can use this feature.
34
- * All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
35
- */
36
- autoTrackEvents?: boolean;
37
- /**
38
- * A web session can be defined in different ways.
39
- * To keep it simple we define that the web session is active when the page is not hidden and inactive when the page is hidden.
40
- */
41
- autoTrackSessions?: boolean;
42
- /**
43
- * The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
44
- */
45
- selectorPrefix?: string;
46
- /**
47
- * Modify how the reporter sends events to Amplify.
48
- */
49
- buffering?: AmplifyReporterBufferingConfig;
50
- }
51
- /**
52
- * Configuration options for the buffering behavior of Pinpoint's event tracker.
53
- *
54
- * @see https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js/#set-up-existing-analytics-backend
55
- */
56
- declare type AmplifyReporterBufferingConfig = {
57
- /** Number of items to buffer for sending. */
58
- bufferSize?: number;
59
- /** Number of events sent each time Pinpoint flushes. */
60
- flushSize?: number;
61
- /** Interval Pinpoint flushes analytics events. Measured in milliseconds. */
62
- flushInterval?: number;
63
- /** The maximum number of times Pinpoint will retry to send an event. */
64
- resendLimit?: number;
65
- };
66
- export declare function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig): IReporter;
67
- /**
68
- * Pinpoint has strict attribute name and value length limits
69
- */
70
- export declare function asAttributeMap(values: Record<string, unknown>, groupValues?: boolean, ignorePatterns?: (string | RegExp)[]): AttributeMap;
71
- /**
72
- * Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
73
- * values which are string arrays. This function takes in an object and ensures
74
- * all of its values are of type `string[]` to appease Pinpoint.
75
- */
76
- export declare function buildAttributeMap(values: Record<string, any>, parentKey?: string | undefined, groupValues?: boolean): AttributeMap;
77
- export {};
1
+ import { IReporter, Metadata, ReporterConfigBase, ServiceInfo } from '../types';
2
+ declare type AttributeMap = Record<string, string[] | string | null>;
3
+ declare type AmplifyAutoTrackSource = 'pageView' | 'event' | 'session';
4
+ export interface AmplifyReporterConfig extends ReporterConfigBase {
5
+ /**
6
+ * AWS Region for Amplify.
7
+ */
8
+ region: string;
9
+ /**
10
+ * The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
11
+ * This must be called manually for the reporter to work.
12
+ */
13
+ identityPoolId: string | false;
14
+ /**
15
+ * The Pinpoint App Id to report to.
16
+ */
17
+ analyticsAppId: string;
18
+ /**
19
+ * The Cognito User Pool to configure in Auth.configure.
20
+ * If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
21
+ */
22
+ userPoolId?: string;
23
+ /**
24
+ * The Cognito Web Client Id to configure in Auth.configure.
25
+ * If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
26
+ */
27
+ userPoolWebClientId?: string;
28
+ /**
29
+ * If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
30
+ * It will automatically send events containing url information when the page is visited.
31
+ */
32
+ autoTrackPageViews?: boolean;
33
+ /**
34
+ * If you want to track user interactions with elements on the page, you can use this feature.
35
+ * All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
36
+ */
37
+ autoTrackEvents?: boolean;
38
+ /**
39
+ * A web session can be defined in different ways.
40
+ * To keep it simple we define that the web session is active when the page is not hidden and inactive when the page is hidden.
41
+ */
42
+ autoTrackSessions?: boolean;
43
+ /**
44
+ * Optional function to run before autotracked analytics events are sent out.
45
+ * The returned metadata is attached to the event.
46
+ */
47
+ beforeAutoTrack?(source: AmplifyAutoTrackSource): Metadata;
48
+ /**
49
+ * The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
50
+ */
51
+ selectorPrefix?: string;
52
+ /**
53
+ * Modify how the reporter sends events to Amplify.
54
+ */
55
+ buffering?: AmplifyReporterBufferingConfig;
56
+ }
57
+ /**
58
+ * Configuration options for the buffering behavior of Pinpoint's event tracker.
59
+ *
60
+ * @see https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js/#set-up-existing-analytics-backend
61
+ */
62
+ declare type AmplifyReporterBufferingConfig = {
63
+ /** Number of items to buffer for sending. */
64
+ bufferSize?: number;
65
+ /** Number of events sent each time Pinpoint flushes. */
66
+ flushSize?: number;
67
+ /** Interval Pinpoint flushes analytics events. Measured in milliseconds. */
68
+ flushInterval?: number;
69
+ /** The maximum number of times Pinpoint will retry to send an event. */
70
+ resendLimit?: number;
71
+ };
72
+ export declare function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig): IReporter;
73
+ /**
74
+ * Pinpoint has strict attribute name and value length limits
75
+ */
76
+ export declare function asAttributeMap(values: Record<string, unknown>, groupValues?: boolean): AttributeMap;
77
+ /**
78
+ * Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
79
+ * values which are string arrays. This function takes in an object and ensures
80
+ * all of its values are of type `string[]` to appease Pinpoint.
81
+ */
82
+ export declare function buildAttributeMap(values: Record<string, any>, parentKey?: string | undefined, groupValues?: boolean): AttributeMap;
83
+ export {};
78
84
  //# sourceMappingURL=amplifyReporter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"amplifyReporter.d.ts","sourceRoot":"","sources":["../../src/reporters/amplifyReporter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAGT,kBAAkB,EAGlB,WAAW,EACZ,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,YAAY,EAAsB,MAAM,UAAU,CAAC;AAE5D,MAAM,WAAW,qBAAsB,SAAQ,kBAAkB;IAC/D;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,cAAc,EAAE,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c,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,UAAO,GAAG,YAAY,CAqBhG;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,SAAS,GAAE,MAAM,GAAG,SAAqB,EACzC,WAAW,UAAO,GACjB,YAAY,CAsBd"}
@@ -1,152 +1,163 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildAttributeMap = exports.asAttributeMap = exports.amplifyReporter = void 0;
4
- const auth_1 = require("@aws-amplify/auth");
5
- const analytics_1 = require("@aws-amplify/analytics");
6
- const utils_1 = require("../utils");
7
- function amplifyReporter(info, config) {
8
- var _a;
9
- if (config.identityPoolId !== false) {
10
- auth_1.Auth.configure({
11
- region: config.region,
12
- identityPoolId: config.identityPoolId,
13
- userPoolId: config.userPoolId,
14
- userPoolWebClientId: config.userPoolWebClientId,
15
- });
16
- }
17
- const allMetadata = asAttributeMap({
18
- appName: info.service,
19
- service: info.service,
20
- domain: window.location.host,
21
- environment: info.environment,
22
- version: info.version,
23
- });
24
- analytics_1.Analytics.configure({
25
- region: config.region,
26
- appId: config.analyticsAppId,
27
- autoSessionRecord: config.autoTrackSessions,
28
- endpoint: {
29
- attributes: allMetadata,
30
- },
31
- ...config.buffering,
32
- });
33
- if (config.autoTrackPageViews) {
34
- analytics_1.Analytics.autoTrack('pageView', {
35
- enable: true,
36
- eventName: 'pageView',
37
- type: 'SPA',
38
- provider: 'AWSPinpoint',
39
- });
40
- }
41
- if (config.autoTrackEvents) {
42
- analytics_1.Analytics.autoTrack('event', {
43
- enable: true,
44
- selectorPrefix: (_a = config.selectorPrefix) !== null && _a !== void 0 ? _a : 'data-analytics-',
45
- });
46
- }
47
- const reporter = {
48
- trackEvent: function (event) {
49
- analytics_1.Analytics.record({
50
- name: event.message,
51
- attributes: asAttributeMap({
52
- ...event.metadata,
53
- ...event.tags,
54
- }, false, config.ignoreMetadataPatterns),
55
- metrics: event.metrics,
56
- });
57
- },
58
- addBreadcrumb: function (breadcrumb) {
59
- var _a;
60
- if (breadcrumb.category && ((_a = config.ignoreBreadcrumbCategories) === null || _a === void 0 ? void 0 : _a.includes(breadcrumb.category))) {
61
- return;
62
- }
63
- reporter.trackEvent({
64
- message: breadcrumb.message,
65
- metadata: {
66
- category: breadcrumb.category,
67
- ...breadcrumb.metadata,
68
- },
69
- });
70
- },
71
- addMetadata: function (metadata) {
72
- Object.assign(allMetadata, asAttributeMap(metadata, true, config.ignoreMetadataPatterns));
73
- analytics_1.Analytics.updateEndpoint({
74
- attributes: allMetadata,
75
- }).catch(() => {
76
- // Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
77
- });
78
- },
79
- setUser: function (user) {
80
- var _a, _b;
81
- analytics_1.Analytics.updateEndpoint({
82
- userId: (_a = user === null || user === void 0 ? void 0 : user.id) !== null && _a !== void 0 ? _a : '',
83
- userAttributes: user
84
- ? asAttributeMap({
85
- id: user.id,
86
- email: user.email,
87
- name: (_b = user.name) !== null && _b !== void 0 ? _b : user.email,
88
- username: user.username,
89
- })
90
- : {},
91
- }).catch(() => {
92
- // Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
93
- });
94
- },
95
- setRouteName: function (routeName) {
96
- reporter.addMetadata({ routeName });
97
- },
98
- setPageName: function (pageName) {
99
- reporter.addMetadata({ pageName });
100
- },
101
- };
102
- return reporter;
103
- }
104
- exports.amplifyReporter = amplifyReporter;
105
- /**
106
- * Pinpoint has strict attribute name and value length limits
107
- */
108
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
109
- function asAttributeMap(values, groupValues = true, ignorePatterns = []) {
110
- const attributeMap = buildAttributeMap(values, undefined, groupValues);
111
- const filteredAttributeMap = (0, utils_1.filterAttributeMap)(attributeMap, ignorePatterns);
112
- const checkedEntries = Object.entries(filteredAttributeMap).map(([key, value]) => {
113
- var _a, _b;
114
- const truncatedKey = key.length > 50 ? `___${key.slice(-47)}` : key;
115
- const truncatedValue = Array.isArray(value)
116
- ? (_a = value === null || value === void 0 ? void 0 : value.map((val) => val.slice(0, 100))) !== null && _a !== void 0 ? _a : null
117
- : (_b = value === null || value === void 0 ? void 0 : value.slice(0, 100)) !== null && _b !== void 0 ? _b : null;
118
- return [truncatedKey, truncatedValue];
119
- });
120
- return Object.fromEntries(checkedEntries);
121
- }
122
- exports.asAttributeMap = asAttributeMap;
123
- /**
124
- * Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
125
- * values which are string arrays. This function takes in an object and ensures
126
- * all of its values are of type `string[]` to appease Pinpoint.
127
- */
128
- function buildAttributeMap(
129
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
- values, parentKey = undefined, groupValues = true) {
131
- const valuesWithStringArrays = {};
132
- Object.entries(values).forEach(([key, value]) => {
133
- const combinedKey = parentKey ? [parentKey, key].join('.') : key;
134
- if (!value) {
135
- valuesWithStringArrays[combinedKey] = null;
136
- }
137
- else if (groupValues && Array.isArray(value)) {
138
- valuesWithStringArrays[combinedKey] = value.map((element) => typeof element === 'string' ? element : JSON.stringify(element));
139
- }
140
- else if (typeof value === 'object') {
141
- const flattenedAttribute = buildAttributeMap(value, combinedKey, groupValues);
142
- Object.assign(valuesWithStringArrays, flattenedAttribute);
143
- }
144
- else {
145
- const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
146
- valuesWithStringArrays[combinedKey] = groupValues ? [stringValue] : stringValue;
147
- }
148
- });
149
- return valuesWithStringArrays;
150
- }
151
- exports.buildAttributeMap = buildAttributeMap;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildAttributeMap = exports.asAttributeMap = exports.amplifyReporter = void 0;
4
+ const auth_1 = require("@aws-amplify/auth");
5
+ const analytics_1 = require("@aws-amplify/analytics");
6
+ const logger_1 = require("../logger");
7
+ function amplifyReporter(info, config) {
8
+ var _a;
9
+ if (config.identityPoolId !== false) {
10
+ auth_1.Auth.configure({
11
+ region: config.region,
12
+ identityPoolId: config.identityPoolId,
13
+ userPoolId: config.userPoolId,
14
+ userPoolWebClientId: config.userPoolWebClientId,
15
+ });
16
+ }
17
+ const wrapAutoTrackMiddleware = (source) => {
18
+ const { beforeAutoTrack } = config;
19
+ return typeof beforeAutoTrack === 'function' ? () => beforeAutoTrack(source) : undefined;
20
+ };
21
+ const allMetadata = asAttributeMap({
22
+ appName: info.service,
23
+ service: info.service,
24
+ domain: window.location.host,
25
+ environment: info.environment,
26
+ version: info.version,
27
+ });
28
+ analytics_1.Analytics.configure({
29
+ region: config.region,
30
+ appId: config.analyticsAppId,
31
+ endpoint: {
32
+ attributes: allMetadata,
33
+ },
34
+ ...config.buffering,
35
+ });
36
+ // Session autotracking is enabled by default for backwards compatibility reasons, so we _must_
37
+ // call this unconditionally to ensure we opt out of session tracking when `autoTrackSessions` isn't set
38
+ // See: https://docs.amplify.aws/lib/analytics/autotrack/q/platform/js/#session-tracking
39
+ analytics_1.Analytics.autoTrack('session', {
40
+ enable: config.autoTrackSessions === true,
41
+ attributes: wrapAutoTrackMiddleware('session'),
42
+ });
43
+ if (config.autoTrackPageViews) {
44
+ analytics_1.Analytics.autoTrack('pageView', {
45
+ enable: true,
46
+ eventName: 'pageView',
47
+ type: 'SPA',
48
+ provider: 'AWSPinpoint',
49
+ attributes: wrapAutoTrackMiddleware('pageView'),
50
+ });
51
+ }
52
+ if (config.autoTrackEvents) {
53
+ analytics_1.Analytics.autoTrack('event', {
54
+ enable: true,
55
+ selectorPrefix: (_a = config.selectorPrefix) !== null && _a !== void 0 ? _a : 'data-analytics-',
56
+ attributes: wrapAutoTrackMiddleware('event'),
57
+ });
58
+ }
59
+ const reporter = {
60
+ trackEvent: function (event) {
61
+ analytics_1.Analytics.record({
62
+ name: event.message,
63
+ attributes: asAttributeMap({
64
+ ...event.metadata,
65
+ ...event.tags,
66
+ }, false),
67
+ metrics: event.metrics,
68
+ });
69
+ },
70
+ addBreadcrumb: function (breadcrumb) {
71
+ reporter.trackEvent({
72
+ message: breadcrumb.message,
73
+ metadata: {
74
+ category: breadcrumb.category,
75
+ ...breadcrumb.metadata,
76
+ },
77
+ });
78
+ },
79
+ addMetadata: function (metadata) {
80
+ Object.assign(allMetadata, asAttributeMap(metadata, true));
81
+ analytics_1.Analytics.updateEndpoint({
82
+ attributes: allMetadata,
83
+ }).catch(() => {
84
+ // Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
85
+ });
86
+ },
87
+ setUser: function (user) {
88
+ var _a, _b;
89
+ analytics_1.Analytics.updateEndpoint({
90
+ userId: (_a = user === null || user === void 0 ? void 0 : user.id) !== null && _a !== void 0 ? _a : '',
91
+ userAttributes: user
92
+ ? asAttributeMap({
93
+ id: user.id,
94
+ email: user.email,
95
+ name: (_b = user.name) !== null && _b !== void 0 ? _b : user.email,
96
+ username: user.username,
97
+ })
98
+ : {},
99
+ }).catch(() => {
100
+ // Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
101
+ });
102
+ },
103
+ setRouteName: function (routeName) {
104
+ reporter.addMetadata({ routeName });
105
+ },
106
+ setPageName: function (pageName) {
107
+ reporter.addMetadata({ pageName });
108
+ },
109
+ };
110
+ return reporter;
111
+ }
112
+ exports.amplifyReporter = amplifyReporter;
113
+ /**
114
+ * Pinpoint has strict attribute name and value length limits
115
+ */
116
+ function asAttributeMap(values, groupValues = true) {
117
+ const attributeMap = buildAttributeMap(values, undefined, groupValues);
118
+ const checkedEntries = Object.entries(attributeMap).map(([key, value]) => {
119
+ var _a, _b;
120
+ const truncatedKey = key.length > 50 ? `___${key.slice(-47)}` : key;
121
+ const truncatedValue = Array.isArray(value)
122
+ ? (_a = value === null || value === void 0 ? void 0 : value.map((val) => val.slice(0, 100))) !== null && _a !== void 0 ? _a : null
123
+ : (_b = value === null || value === void 0 ? void 0 : value.slice(0, 100)) !== null && _b !== void 0 ? _b : null;
124
+ return [truncatedKey, truncatedValue];
125
+ });
126
+ // Pinpoint only accepts 40 attributes
127
+ if (checkedEntries.length > 40) {
128
+ logger_1.logger.error(`Amplify only allows 40 attributes per event, truncating to 40 attributes`, {
129
+ attributes: checkedEntries,
130
+ });
131
+ checkedEntries.length = 40;
132
+ }
133
+ return Object.fromEntries(checkedEntries);
134
+ }
135
+ exports.asAttributeMap = asAttributeMap;
136
+ /**
137
+ * Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
138
+ * values which are string arrays. This function takes in an object and ensures
139
+ * all of its values are of type `string[]` to appease Pinpoint.
140
+ */
141
+ function buildAttributeMap(values, parentKey = undefined, groupValues = true) {
142
+ const valuesWithStringArrays = {};
143
+ Object.entries(values).forEach(([key, value]) => {
144
+ const combinedKey = parentKey ? [parentKey, key].join('.') : key;
145
+ if (!value) {
146
+ valuesWithStringArrays[combinedKey] = null;
147
+ }
148
+ else if (groupValues && Array.isArray(value)) {
149
+ valuesWithStringArrays[combinedKey] = value.map((element) => typeof element === 'string' ? element : JSON.stringify(element));
150
+ }
151
+ else if (typeof value === 'object') {
152
+ const flattenedAttribute = buildAttributeMap(value, combinedKey, groupValues);
153
+ Object.assign(valuesWithStringArrays, flattenedAttribute);
154
+ }
155
+ else {
156
+ const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
157
+ valuesWithStringArrays[combinedKey] = groupValues ? [stringValue] : stringValue;
158
+ }
159
+ });
160
+ return valuesWithStringArrays;
161
+ }
162
+ exports.buildAttributeMap = buildAttributeMap;
152
163
  //# sourceMappingURL=amplifyReporter.js.map