@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.
- package/README.md +212 -67
- package/lib/index.d.ts +12 -82
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +69 -213
- package/lib/index.js.map +1 -1
- package/lib/logger/consoleTransport.d.ts +37 -0
- package/lib/logger/consoleTransport.d.ts.map +1 -0
- package/lib/logger/consoleTransport.js +81 -0
- package/lib/logger/consoleTransport.js.map +1 -0
- package/lib/logger/datadogTransport.d.ts +8 -0
- package/lib/logger/datadogTransport.d.ts.map +1 -0
- package/lib/logger/datadogTransport.js +21 -0
- package/lib/logger/datadogTransport.js.map +1 -0
- package/lib/logger/index.d.ts +16 -0
- package/lib/logger/index.d.ts.map +1 -0
- package/lib/logger/index.js +148 -0
- package/lib/logger/index.js.map +1 -0
- package/lib/logger/index.test.d.ts +2 -0
- package/lib/logger/index.test.d.ts.map +1 -0
- package/lib/logger/index.test.js +60 -0
- package/lib/logger/index.test.js.map +1 -0
- package/lib/logger/utils.d.ts +15 -0
- package/lib/logger/utils.d.ts.map +1 -0
- package/lib/logger/utils.js +32 -0
- package/lib/logger/utils.js.map +1 -0
- package/lib/reporters/amplifyReporter.d.ts +83 -77
- package/lib/reporters/amplifyReporter.d.ts.map +1 -1
- package/lib/reporters/amplifyReporter.js +162 -151
- package/lib/reporters/amplifyReporter.js.map +1 -1
- package/lib/reporters/amplifyReporter.test.d.ts +1 -1
- package/lib/reporters/amplifyReporter.test.js +50 -50
- package/lib/reporters/datadogReporter.d.ts +76 -74
- package/lib/reporters/datadogReporter.d.ts.map +1 -1
- package/lib/reporters/datadogReporter.js +122 -185
- package/lib/reporters/datadogReporter.js.map +1 -1
- package/lib/reporters/gtmReporter.d.ts +8 -8
- package/lib/reporters/gtmReporter.d.ts.map +1 -1
- package/lib/reporters/gtmReporter.js +56 -61
- package/lib/reporters/gtmReporter.js.map +1 -1
- package/lib/reporters/index.d.ts +66 -3
- package/lib/reporters/index.d.ts.map +1 -1
- package/lib/reporters/index.js +212 -19
- package/lib/reporters/index.js.map +1 -1
- package/lib/reporters/logReporter.d.ts +35 -0
- package/lib/reporters/logReporter.d.ts.map +1 -0
- package/lib/reporters/logReporter.js +62 -0
- package/lib/reporters/logReporter.js.map +1 -0
- package/lib/types/index.d.ts +2 -2
- package/lib/types/index.js +18 -18
- package/lib/types/logger.d.ts +77 -36
- package/lib/types/logger.d.ts.map +1 -1
- package/lib/types/logger.js +10 -10
- package/lib/types/logger.js.map +1 -1
- package/lib/types/reporter.d.ts +154 -102
- package/lib/types/reporter.d.ts.map +1 -1
- package/lib/types/reporter.js +2 -2
- package/lib/utils.d.ts +9 -8
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +31 -51
- package/lib/utils.js.map +1 -1
- package/lib/utils.test.d.ts +1 -1
- package/lib/utils.test.js +31 -18
- package/lib/utils.test.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +38 -208
- package/src/logger/consoleTransport.ts +101 -0
- package/src/logger/datadogTransport.ts +20 -0
- package/src/logger/index.test.ts +68 -0
- package/src/logger/index.ts +139 -0
- package/src/logger/utils.ts +28 -0
- package/src/reporters/amplifyReporter.ts +37 -17
- package/src/reporters/datadogReporter.ts +48 -116
- package/src/reporters/gtmReporter.ts +2 -7
- package/src/reporters/index.ts +216 -3
- package/src/reporters/logReporter.ts +86 -0
- package/src/types/logger.ts +49 -4
- package/src/types/reporter.ts +66 -6
- package/src/utils.test.ts +20 -6
- package/src/utils.ts +37 -62
|
@@ -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;YACrB,eAAe,EAAE;gBACf,WAAW,EAAE,MAAM;aACpB;SACF,CAAC,CAAC;QAEH,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QACrD,UAAU,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACtC,UAAU,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;QAEzE,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;QAClC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;QAC1D,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;QAE3D,IAAA,cAAiB,EAAC,gBAAQ,CAAC,IAAI,CAAC,CAAC;QACjC,IAAA,uBAAW,EAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;QAEpC,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACxC,MAAM,CAAC,IAAI,CAAC,iDAAiD,CAAC,CAAC;QAE/D,IAAA,cAAiB,EAAC,IAAI,CAAC,CAAC;QACxB,IAAA,mBAAO,EAAC;YACN,EAAE,EAAE,KAAK;YACT,KAAK,EAAE,kBAAkB;SAC1B,CAAC,CAAC;QAEH,MAAM,CAAC,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAE1C,SAAS,CAAC,QAAQ,GAAG,gBAAQ,CAAC,IAAI,CAAC;QACnC,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACxC,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;QACnC,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
export declare function
|
|
77
|
-
|
|
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,
|
|
1
|
+
{"version":3,"file":"amplifyReporter.d.ts","sourceRoot":"","sources":["../../src/reporters/amplifyReporter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,QAAQ,EAER,kBAAkB,EAGlB,WAAW,EACZ,MAAM,UAAU,CAAC;AAMlB,aAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC;AAE7D,aAAK,sBAAsB,GAAG,UAAU,GAAG,OAAO,GAAG,SAAS,CAAC;AAE/D,MAAM,WAAW,qBAAsB,SAAQ,kBAAkB;IAC/D;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,cAAc,EAAE,MAAM,GAAG,KAAK,CAAC;IAC/B;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,eAAe,CAAC,CAAC,MAAM,EAAE,sBAAsB,GAAG,QAAQ,CAAC;IAE3D;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,8BAA8B,CAAC;CAC5C;AAED;;;;GAIG;AACH,aAAK,8BAA8B,GAAG;IACpC,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4EAA4E;IAC5E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,qBAAqB,GAAG,SAAS,CAiH3F;AAED;;GAEG;AACH,wBAAgB,cAAc,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
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
});
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
analytics_1.Analytics.updateEndpoint({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
valuesWithStringArrays[combinedKey] =
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
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
|