@crimson-education/browser-logger 5.0.0-beta.17 → 5.0.0-beta.19
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/lib/reporters/amplifyReporter.js +1 -1
- package/lib/reporters/amplifyReporter.js.map +1 -1
- package/package.json +5 -5
- package/lib/logger/index.test.d.ts +0 -2
- package/lib/logger/index.test.d.ts.map +0 -1
- package/lib/logger/index.test.js +0 -58
- package/lib/logger/index.test.js.map +0 -1
- package/lib/reporters/amplifyReporter.test.d.ts +0 -2
- package/lib/reporters/amplifyReporter.test.d.ts.map +0 -1
- package/lib/reporters/amplifyReporter.test.js +0 -48
- package/lib/reporters/amplifyReporter.test.js.map +0 -1
- package/lib/utils.test.d.ts +0 -2
- package/lib/utils.test.d.ts.map +0 -1
- package/lib/utils.test.js +0 -30
- package/lib/utils.test.js.map +0 -1
- package/src/index.ts +0 -64
- package/src/logger/consoleTransport.ts +0 -101
- package/src/logger/datadogTransport.ts +0 -20
- package/src/logger/index.test.ts +0 -68
- package/src/logger/index.ts +0 -139
- package/src/logger/utils.ts +0 -28
- package/src/reporters/amplifyReporter.test.ts +0 -61
- package/src/reporters/amplifyReporter.ts +0 -460
- package/src/reporters/datadogReporter.ts +0 -359
- package/src/reporters/gtmReporter.ts +0 -74
- package/src/reporters/index.ts +0 -232
- package/src/reporters/logReporter.ts +0 -86
- package/src/types/index.ts +0 -2
- package/src/types/logger.ts +0 -85
- package/src/types/reporter.ts +0 -180
- package/src/utils.test.ts +0 -32
- package/src/utils.ts +0 -42
package/src/logger/utils.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { LogLevel } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Checks if a log level is above the global log level.
|
|
5
|
-
* @param level The log level to check
|
|
6
|
-
* @param checkLevel THe log level to check against
|
|
7
|
-
* @returns Is above the checkLevel?
|
|
8
|
-
*/
|
|
9
|
-
export function isAboveLevel(level: LogLevel, checkLevel: LogLevel): boolean {
|
|
10
|
-
const levels = [LogLevel.Debug, LogLevel.Info, LogLevel.Warn, LogLevel.Error];
|
|
11
|
-
return levels.indexOf(level) >= levels.indexOf(checkLevel);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Ensures a message output is a string.
|
|
16
|
-
* @param message The message to assert is/convert to a string
|
|
17
|
-
* @returns The message as a string
|
|
18
|
-
*/
|
|
19
|
-
export function getLogMessage(message: any): string {
|
|
20
|
-
if (typeof message === 'string') {
|
|
21
|
-
return message;
|
|
22
|
-
}
|
|
23
|
-
if (typeof message === 'object' && message !== null) {
|
|
24
|
-
const data = { ...message };
|
|
25
|
-
return JSON.stringify(data);
|
|
26
|
-
}
|
|
27
|
-
return String(message);
|
|
28
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { asAttributeMap } from './amplifyReporter';
|
|
2
|
-
|
|
3
|
-
describe('amplifyReporter', () => {
|
|
4
|
-
it('should convert all attribute values to arrays of strings', () => {
|
|
5
|
-
const testMetadata = { a: 'a', d: ['e', 'f'] };
|
|
6
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
7
|
-
|
|
8
|
-
expect(attributeMap.a).toEqual(['a']);
|
|
9
|
-
expect(attributeMap.d).toEqual(['e', 'f']);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should handle undefined / null attributes', () => {
|
|
13
|
-
const testMetadata = { a: null, d: undefined };
|
|
14
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
15
|
-
|
|
16
|
-
expect(attributeMap.a).toBeNull();
|
|
17
|
-
expect(attributeMap.d).toBeNull();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should flatten hierarchies', () => {
|
|
21
|
-
const testMetadata = { foo: { bar: 'baz' } };
|
|
22
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
23
|
-
|
|
24
|
-
expect(attributeMap['foo.bar']).toEqual(['baz']);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should stringify non-string array members', () => {
|
|
28
|
-
const testMetadata = { foo: 5, bar: [{ baz: 'maz' }] };
|
|
29
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
30
|
-
|
|
31
|
-
expect(attributeMap.foo).toEqual(['5']);
|
|
32
|
-
expect(typeof attributeMap.bar?.[0]).toEqual('string');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should truncate attribute names', () => {
|
|
36
|
-
const testMetadata = { thisIsAVeryVeryLongAttributeNameThatNeedsToBeTruncated: 5 };
|
|
37
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
38
|
-
|
|
39
|
-
expect(attributeMap.___VeryVeryLongAttributeNameThatNeedsToBeTruncated).toEqual(['5']);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should truncate attribute values', () => {
|
|
43
|
-
const testMetadata = {
|
|
44
|
-
a: 'ThisIsAVeryLongStringThatNeedsToBeTruncatedTo100CharsOrElseThereWillBeAProblemWithSendingTheBeautifulDataToPinpoint',
|
|
45
|
-
};
|
|
46
|
-
const attributeMap = asAttributeMap(testMetadata);
|
|
47
|
-
|
|
48
|
-
expect(attributeMap.a).toEqual([
|
|
49
|
-
'ThisIsAVeryLongStringThatNeedsToBeTruncatedTo100CharsOrElseThereWillBeAProblemWithSendingTheBeautifu',
|
|
50
|
-
]);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should not group values into arrays when groupValues===false', () => {
|
|
54
|
-
const testMetadata = {
|
|
55
|
-
a: '5',
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const attributeMap = asAttributeMap(testMetadata, false);
|
|
59
|
-
expect(attributeMap.a).toEqual('5');
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,460 +0,0 @@
|
|
|
1
|
-
import { identifyUser, record } from '@aws-amplify/analytics';
|
|
2
|
-
// import { Endpoint, FinalizeRequestMiddleware } from '@aws-sdk/types';
|
|
3
|
-
// import { HttpRequest } from '@aws-sdk/protocol-http';
|
|
4
|
-
// import { PinpointClient, ServiceInputTypes, ServiceOutputTypes } from '@aws-sdk/client-pinpoint';
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
IReporter,
|
|
8
|
-
Metadata,
|
|
9
|
-
ReporterBreadcrumb,
|
|
10
|
-
ReporterConfigBase,
|
|
11
|
-
ReporterEvent,
|
|
12
|
-
ReportUser,
|
|
13
|
-
ServiceInfo,
|
|
14
|
-
} from '../types';
|
|
15
|
-
import { logger } from '../logger';
|
|
16
|
-
// Note: fetchAuthSession was previously imported but unused; removing to satisfy linter
|
|
17
|
-
import { Amplify } from 'aws-amplify';
|
|
18
|
-
|
|
19
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
20
|
-
type AttributeMap = Record<string, string[] | string | null>;
|
|
21
|
-
|
|
22
|
-
type AmplifyAutoTrackSource = 'pageView' | 'event' | 'session';
|
|
23
|
-
|
|
24
|
-
// Auto-tracking implementation for Gen2
|
|
25
|
-
class AmplifyAutoTracker {
|
|
26
|
-
private config: AmplifyReporterConfig;
|
|
27
|
-
private allMetadata: AttributeMap;
|
|
28
|
-
private currentUser: ReportUser | null = null;
|
|
29
|
-
private sessionStartTime: number = Date.now();
|
|
30
|
-
private pageViewCount: number = 0;
|
|
31
|
-
private lastPageUrl: string = window.location.href;
|
|
32
|
-
private isPageVisible: boolean = !document.hidden;
|
|
33
|
-
|
|
34
|
-
constructor(config: AmplifyReporterConfig, allMetadata: AttributeMap) {
|
|
35
|
-
this.config = config;
|
|
36
|
-
this.allMetadata = allMetadata;
|
|
37
|
-
this.setupEventListeners();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private setupEventListeners() {
|
|
41
|
-
// Page visibility tracking
|
|
42
|
-
document.addEventListener('visibilitychange', () => {
|
|
43
|
-
this.isPageVisible = !document.hidden;
|
|
44
|
-
if (this.config.autoTrackSessions) {
|
|
45
|
-
this.trackSessionState();
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Page view tracking
|
|
50
|
-
if (this.config.autoTrackPageViews) {
|
|
51
|
-
this.trackPageView();
|
|
52
|
-
|
|
53
|
-
// Track route changes for SPAs
|
|
54
|
-
let currentUrl = window.location.href;
|
|
55
|
-
const observer = new MutationObserver(() => {
|
|
56
|
-
if (window.location.href !== currentUrl) {
|
|
57
|
-
currentUrl = window.location.href;
|
|
58
|
-
this.trackPageView();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
observer.observe(document.body, { childList: true, subtree: true });
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// User interaction tracking
|
|
65
|
-
if (this.config.autoTrackEvents) {
|
|
66
|
-
this.setupInteractionTracking();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private trackPageView() {
|
|
71
|
-
this.pageViewCount++;
|
|
72
|
-
const pageViewEvent = {
|
|
73
|
-
message: 'Page View',
|
|
74
|
-
metadata: {
|
|
75
|
-
url: window.location.href,
|
|
76
|
-
title: document.title,
|
|
77
|
-
referrer: document.referrer,
|
|
78
|
-
pageViewCount: this.pageViewCount,
|
|
79
|
-
...this.getAutoTrackMetadata('pageView'),
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
record({
|
|
84
|
-
name: pageViewEvent.message,
|
|
85
|
-
attributes: asAttributeMap(
|
|
86
|
-
{
|
|
87
|
-
...this.allMetadata,
|
|
88
|
-
...pageViewEvent.metadata,
|
|
89
|
-
},
|
|
90
|
-
false,
|
|
91
|
-
) as Record<string, string>,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private trackSessionState() {
|
|
96
|
-
const sessionDuration = Date.now() - this.sessionStartTime;
|
|
97
|
-
const sessionEvent = {
|
|
98
|
-
message: this.isPageVisible ? 'Session Active' : 'Session Inactive',
|
|
99
|
-
metadata: {
|
|
100
|
-
sessionDuration,
|
|
101
|
-
isVisible: this.isPageVisible,
|
|
102
|
-
...this.getAutoTrackMetadata('session'),
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
record({
|
|
107
|
-
name: sessionEvent.message,
|
|
108
|
-
attributes: asAttributeMap(
|
|
109
|
-
{
|
|
110
|
-
...this.allMetadata,
|
|
111
|
-
...sessionEvent.metadata,
|
|
112
|
-
},
|
|
113
|
-
false,
|
|
114
|
-
) as Record<string, string>,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private setupInteractionTracking() {
|
|
119
|
-
const selectorPrefix = this.config.selectorPrefix ?? 'data-analytics-';
|
|
120
|
-
|
|
121
|
-
document.addEventListener('click', (event) => {
|
|
122
|
-
const target = event.target as HTMLElement;
|
|
123
|
-
if (!target) return;
|
|
124
|
-
|
|
125
|
-
// Find the closest element with analytics attributes
|
|
126
|
-
const analyticsElement = target.closest(`[${selectorPrefix}name]`);
|
|
127
|
-
if (!analyticsElement) return;
|
|
128
|
-
|
|
129
|
-
const analyticsName = analyticsElement.getAttribute(`${selectorPrefix}name`);
|
|
130
|
-
if (!analyticsName) return;
|
|
131
|
-
|
|
132
|
-
const interactionEvent = {
|
|
133
|
-
message: 'User Interaction',
|
|
134
|
-
metadata: {
|
|
135
|
-
elementName: analyticsName,
|
|
136
|
-
elementType: target.tagName.toLowerCase(),
|
|
137
|
-
elementText: target.textContent?.slice(0, 100),
|
|
138
|
-
interactionType: 'click',
|
|
139
|
-
...this.getAutoTrackMetadata('event'),
|
|
140
|
-
},
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
record({
|
|
144
|
-
name: interactionEvent.message,
|
|
145
|
-
attributes: asAttributeMap(
|
|
146
|
-
{
|
|
147
|
-
...this.allMetadata,
|
|
148
|
-
...interactionEvent.metadata,
|
|
149
|
-
},
|
|
150
|
-
false,
|
|
151
|
-
) as Record<string, string>,
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private getAutoTrackMetadata(source: AmplifyAutoTrackSource): Metadata {
|
|
157
|
-
const { beforeAutoTrack } = this.config;
|
|
158
|
-
return typeof beforeAutoTrack === 'function' ? (beforeAutoTrack(source) ?? {}) : {};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public setUser(user: ReportUser | null) {
|
|
162
|
-
this.currentUser = user;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public updateMetadata(metadata: AttributeMap) {
|
|
166
|
-
Object.assign(this.allMetadata, metadata);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export interface AmplifyReporterConfig extends ReporterConfigBase {
|
|
171
|
-
/**
|
|
172
|
-
* AWS Region for Amplify.
|
|
173
|
-
*/
|
|
174
|
-
region: string;
|
|
175
|
-
/**
|
|
176
|
-
* The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
|
|
177
|
-
* This must be called manually for the reporter to work.
|
|
178
|
-
*/
|
|
179
|
-
identityPoolId: string;
|
|
180
|
-
/**
|
|
181
|
-
* The Pinpoint App Id to report to.
|
|
182
|
-
*/
|
|
183
|
-
analyticsAppId: string;
|
|
184
|
-
/** Optional proxy URL */
|
|
185
|
-
proxyUrl?: string;
|
|
186
|
-
/**
|
|
187
|
-
* The Cognito User Pool to configure in Auth.configure.
|
|
188
|
-
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
189
|
-
*/
|
|
190
|
-
userPoolId?: string;
|
|
191
|
-
/**
|
|
192
|
-
* The Cognito Web Client Id to configure in Auth.configure.
|
|
193
|
-
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
194
|
-
*/
|
|
195
|
-
userPoolWebClientId?: string;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
|
|
199
|
-
* It will automatically send events containing url information when the page is visited.
|
|
200
|
-
*/
|
|
201
|
-
autoTrackPageViews?: boolean;
|
|
202
|
-
/**
|
|
203
|
-
* If you want to track user interactions with elements on the page, you can use this feature.
|
|
204
|
-
* All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
|
|
205
|
-
*/
|
|
206
|
-
autoTrackEvents?: boolean;
|
|
207
|
-
/**
|
|
208
|
-
* A web session can be defined in different ways.
|
|
209
|
-
* 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.
|
|
210
|
-
*/
|
|
211
|
-
autoTrackSessions?: boolean;
|
|
212
|
-
/**
|
|
213
|
-
* Optional function to run before autotracked analytics events are sent out.
|
|
214
|
-
* The returned metadata is attached to the event.
|
|
215
|
-
*/
|
|
216
|
-
beforeAutoTrack?(source: AmplifyAutoTrackSource): Metadata | undefined;
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
|
|
220
|
-
*/
|
|
221
|
-
selectorPrefix?: string;
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Modify how the reporter sends events to Amplify.
|
|
225
|
-
*/
|
|
226
|
-
buffering?: AmplifyReporterBufferingConfig;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Configuration options for the buffering behavior of Pinpoint's event tracker.
|
|
231
|
-
*
|
|
232
|
-
* @see https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js/#set-up-existing-analytics-backend
|
|
233
|
-
*/
|
|
234
|
-
type AmplifyReporterBufferingConfig = {
|
|
235
|
-
/** Number of items to buffer for sending. */
|
|
236
|
-
bufferSize?: number;
|
|
237
|
-
/** Number of events sent each time Pinpoint flushes. */
|
|
238
|
-
flushSize?: number;
|
|
239
|
-
/** Interval Pinpoint flushes analytics events. Measured in milliseconds. */
|
|
240
|
-
flushInterval?: number;
|
|
241
|
-
/** The maximum number of times Pinpoint will retry to send an event. */
|
|
242
|
-
resendLimit?: number;
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
export function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig): IReporter {
|
|
246
|
-
Amplify.configure({
|
|
247
|
-
Auth: {
|
|
248
|
-
Cognito: {
|
|
249
|
-
identityPoolId: config.identityPoolId,
|
|
250
|
-
allowGuestAccess: true,
|
|
251
|
-
},
|
|
252
|
-
},
|
|
253
|
-
Analytics: {
|
|
254
|
-
Pinpoint: {
|
|
255
|
-
appId: config.analyticsAppId,
|
|
256
|
-
region: config.region,
|
|
257
|
-
...config.buffering,
|
|
258
|
-
},
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
const allMetadata = asAttributeMap({
|
|
263
|
-
appName: info.service,
|
|
264
|
-
service: info.service,
|
|
265
|
-
domain: window.location.host,
|
|
266
|
-
environment: info.environment,
|
|
267
|
-
version: info.version,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// Initialize auto-tracker for Gen2
|
|
271
|
-
const autoTracker = new AmplifyAutoTracker(config, allMetadata);
|
|
272
|
-
|
|
273
|
-
if (config.proxyUrl) {
|
|
274
|
-
// installPinpointProxy(new URL(config.proxyUrl));
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const reporter: IReporter = {
|
|
278
|
-
trackEvent: function (event: ReporterEvent): void {
|
|
279
|
-
record({
|
|
280
|
-
name: event.message,
|
|
281
|
-
attributes: asAttributeMap(
|
|
282
|
-
{
|
|
283
|
-
...allMetadata,
|
|
284
|
-
...event.metadata,
|
|
285
|
-
...event.tags,
|
|
286
|
-
},
|
|
287
|
-
false,
|
|
288
|
-
) as Record<string, string>,
|
|
289
|
-
metrics: event.metrics,
|
|
290
|
-
});
|
|
291
|
-
},
|
|
292
|
-
addBreadcrumb: function (breadcrumb: ReporterBreadcrumb): void {
|
|
293
|
-
reporter.trackEvent({
|
|
294
|
-
message: breadcrumb.message,
|
|
295
|
-
metadata: {
|
|
296
|
-
category: breadcrumb.category,
|
|
297
|
-
...breadcrumb.metadata,
|
|
298
|
-
},
|
|
299
|
-
});
|
|
300
|
-
},
|
|
301
|
-
addMetadata: function (metadata: Metadata): void {
|
|
302
|
-
Object.assign(allMetadata, asAttributeMap(metadata, true));
|
|
303
|
-
autoTracker.updateMetadata(asAttributeMap(metadata, true));
|
|
304
|
-
// Note: updateEndpoint is not available in Amplify v7
|
|
305
|
-
// Metadata updates would need to be handled differently
|
|
306
|
-
},
|
|
307
|
-
setUser: function (user: ReportUser | null): void {
|
|
308
|
-
const userMetadata = user
|
|
309
|
-
? asAttributeMap({
|
|
310
|
-
userId: user.id,
|
|
311
|
-
})
|
|
312
|
-
: {};
|
|
313
|
-
Object.assign(allMetadata, asAttributeMap(userMetadata, true));
|
|
314
|
-
autoTracker.setUser(user);
|
|
315
|
-
// Only call identifyUser when we have a valid user ID
|
|
316
|
-
// Calling with empty string can cause issues with Pinpoint USER_ID
|
|
317
|
-
if (user?.id) {
|
|
318
|
-
identifyUser({
|
|
319
|
-
userId: user.id,
|
|
320
|
-
userProfile: {
|
|
321
|
-
email: user.email,
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
setRouteName: function (routeName: string): void {
|
|
327
|
-
reporter.addMetadata({ routeName });
|
|
328
|
-
},
|
|
329
|
-
setPageName: function (pageName: string): void {
|
|
330
|
-
reporter.addMetadata({ pageName });
|
|
331
|
-
},
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
return reporter;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Pinpoint has strict attribute name and value length limits
|
|
339
|
-
*/
|
|
340
|
-
export function asAttributeMap(values: Record<string, unknown>, groupValues = true): AttributeMap {
|
|
341
|
-
const attributeMap = buildAttributeMap(values, undefined, groupValues);
|
|
342
|
-
|
|
343
|
-
const checkedEntries = Object.entries(attributeMap).map(([key, value]) => {
|
|
344
|
-
const truncatedKey = key.length > 50 ? `___${key.slice(-47)}` : key;
|
|
345
|
-
const truncatedValue = Array.isArray(value)
|
|
346
|
-
? (value?.map((val) => val.slice(0, 100)) ?? null)
|
|
347
|
-
: (value?.slice(0, 100) ?? null);
|
|
348
|
-
|
|
349
|
-
return [truncatedKey, truncatedValue];
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
// Pinpoint only accepts 40 attributes
|
|
353
|
-
if (checkedEntries.length > 40) {
|
|
354
|
-
logger.error(`Amplify only allows 40 attributes per event, truncating to 40 attributes`, {
|
|
355
|
-
attributes: checkedEntries,
|
|
356
|
-
});
|
|
357
|
-
checkedEntries.length = 40;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return Object.fromEntries(checkedEntries);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
|
|
365
|
-
* values which are string arrays. This function takes in an object and ensures
|
|
366
|
-
* all of its values are of type `string[]` to appease Pinpoint.
|
|
367
|
-
*/
|
|
368
|
-
export function buildAttributeMap(
|
|
369
|
-
values: Record<string, any>,
|
|
370
|
-
parentKey: string | undefined = undefined,
|
|
371
|
-
groupValues = true,
|
|
372
|
-
): AttributeMap {
|
|
373
|
-
const valuesWithStringArrays: AttributeMap = {};
|
|
374
|
-
|
|
375
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
376
|
-
const combinedKey = parentKey ? [parentKey, key].join('.') : key;
|
|
377
|
-
|
|
378
|
-
// Only treat undefined or null as empty; avoid converting falsy values like ''/0/false to null
|
|
379
|
-
if (value === undefined || value === null) {
|
|
380
|
-
// For event attributes (groupValues === false), Pinpoint rejects null values.
|
|
381
|
-
// In that case we omit the key entirely to avoid "Event attribute value can not be null".
|
|
382
|
-
if (groupValues) {
|
|
383
|
-
valuesWithStringArrays[combinedKey] = null;
|
|
384
|
-
}
|
|
385
|
-
} else if (groupValues && Array.isArray(value)) {
|
|
386
|
-
valuesWithStringArrays[combinedKey] = value.map((element) =>
|
|
387
|
-
typeof element === 'string' ? element : JSON.stringify(element),
|
|
388
|
-
);
|
|
389
|
-
} else if (typeof value === 'object') {
|
|
390
|
-
const flattenedAttribute = buildAttributeMap(value, combinedKey, groupValues);
|
|
391
|
-
Object.assign(valuesWithStringArrays, flattenedAttribute);
|
|
392
|
-
} else {
|
|
393
|
-
const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
|
|
394
|
-
valuesWithStringArrays[combinedKey] = groupValues ? [stringValue] : stringValue;
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
return valuesWithStringArrays;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// function installPinpointProxy(proxyUrl: URL) {
|
|
402
|
-
// // No public API for overriding where the Pinpoint client sends events to... 🤮
|
|
403
|
-
// // In theory you can pass in an `endpoint` to the Pinpoint client's constructor like any other AWS
|
|
404
|
-
// // client, but Amplify's analytics doesn't expose anything we can use to get an endpoint threaded
|
|
405
|
-
// // down to the Pinpoint client's constructor.
|
|
406
|
-
// //
|
|
407
|
-
// // The Pinpoint client _also_ isn't available synchronously because it is instantiated when events
|
|
408
|
-
// // get sent out, and then reconfigured whenever the API credentials change. We need to hook `_initClients`
|
|
409
|
-
// // to ensure that the Pinpoint client being used is always patched with our custom endpoint.
|
|
410
|
-
// const provider = (globalThis as any).Analytics?.getPluggable?.('AWSPinpoint') as any;
|
|
411
|
-
// if (!provider || typeof provider._initClients !== 'function') {
|
|
412
|
-
// logger.error(
|
|
413
|
-
// 'Installation of the Pinpoint proxy failed. This likely means the internals of the @aws-amplify/analytics package have changed.',
|
|
414
|
-
// );
|
|
415
|
-
// return;
|
|
416
|
-
// }
|
|
417
|
-
|
|
418
|
-
// const originalInitClients = provider._initClients;
|
|
419
|
-
// const requestMiddleware: FinalizeRequestMiddleware<ServiceInputTypes, ServiceOutputTypes> =
|
|
420
|
-
// (next) => async (args) => {
|
|
421
|
-
// const pinpointClient = provider.pinpointClient as PinpointClient | undefined;
|
|
422
|
-
|
|
423
|
-
// if (pinpointClient && proxyUrl.pathname !== '/' && HttpRequest.isInstance(args.request)) {
|
|
424
|
-
// // Add proxyUrl.pathname to final request url if it was provided
|
|
425
|
-
// const shouldStripSlash = proxyUrl.pathname.endsWith('/');
|
|
426
|
-
// args.request.path = `${proxyUrl.pathname}${args.request.path.slice(shouldStripSlash ? 1 : 0)}`;
|
|
427
|
-
|
|
428
|
-
// // Wrap request body so the proxy has signing info
|
|
429
|
-
// if (typeof args.request.body === 'string') {
|
|
430
|
-
// const credentials = await pinpointClient.config.credentials();
|
|
431
|
-
// args.request.body = JSON.stringify({
|
|
432
|
-
// credentials,
|
|
433
|
-
// data: JSON.parse(args.request.body),
|
|
434
|
-
// });
|
|
435
|
-
// }
|
|
436
|
-
// }
|
|
437
|
-
|
|
438
|
-
// return next(args);
|
|
439
|
-
// };
|
|
440
|
-
|
|
441
|
-
// provider._initClients = async (credentials: unknown) => {
|
|
442
|
-
// const result = await originalInitClients.call(provider, credentials);
|
|
443
|
-
// const pinpointClient = provider.pinpointClient as PinpointClient | undefined;
|
|
444
|
-
|
|
445
|
-
// if (pinpointClient) {
|
|
446
|
-
// pinpointClient.config.endpoint = (): Promise<Endpoint> =>
|
|
447
|
-
// Promise.resolve({
|
|
448
|
-
// hostname: proxyUrl.hostname,
|
|
449
|
-
// // Passing proxyUrl.pathname here doesn't work; it gets overridden
|
|
450
|
-
// path: '/',
|
|
451
|
-
// port: undefined,
|
|
452
|
-
// protocol: proxyUrl.protocol,
|
|
453
|
-
// });
|
|
454
|
-
|
|
455
|
-
// pinpointClient.middlewareStack.remove(requestMiddleware);
|
|
456
|
-
// pinpointClient.middlewareStack.add(requestMiddleware, { step: 'finalizeRequest' });
|
|
457
|
-
// }
|
|
458
|
-
// return result;
|
|
459
|
-
// };
|
|
460
|
-
// }
|