Skip to content

Commit b95c58b

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: record error event when event name is invalid
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 813caa9 commit b95c58b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { ClickstreamAnalytics } from '@aws/clickstream-web';
4141

4242
// record event with attributes
4343
ClickstreamAnalytics.record({
44-
name: 'add_to_cart',
44+
name: 'button_click',
4545
attributes: {
4646
event_category: 'shoes',
4747
currency: 'CNY',
@@ -74,7 +74,7 @@ ClickstreamAnalytics.setUserAttributes({
7474
});
7575
```
7676

77-
Current login user's attributes will be cached in localStorage, so the next time browser open you don't need to set all user's attribute again, of course you can use the same api `ClickstreamAnalytics.setUserAttributes()` to update the current user's attribute when it changes.
77+
When opening for the first time after integrating the SDK, you need to manually set the user attributes once, and current login user's attributes will be cached in localStorage, so the next time browser open you don't need to set all user's attribute again, of course you can use the same api `ClickstreamAnalytics.setUserAttributes()` to update the current user's attribute when it changes.
7878

7979
#### Record event with items
8080

src/provider/ClickstreamProvider.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ export class ClickstreamProvider implements AnalyticsProvider {
118118
record(event: ClickstreamEvent) {
119119
const result = EventChecker.checkEventName(event.name);
120120
if (result.error_code > 0) {
121+
const errorEvent = this.createEvent({
122+
name: Event.PresetEvent.CLICKSTREAM_ERROR,
123+
attributes: {
124+
[Event.ReservedAttribute.ERROR_CODE]: result.error_code,
125+
[Event.ReservedAttribute.ERROR_MESSAGE]: result.error_message,
126+
},
127+
});
128+
this.recordEvent(errorEvent);
121129
logger.error(result.error_message);
122130
return;
123131
}

test/provider/ClickstreamProvider.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,18 @@ describe('ClickstreamProvider test', () => {
102102
});
103103

104104
test('test record event with invalid event name', () => {
105-
mockCreateEvent = jest.spyOn(AnalyticsEventBuilder, 'createEvent');
105+
const mockProviderCreateEvent = jest.spyOn(provider, 'createEvent');
106106
provider.record({ name: '01testEvent' });
107-
expect(mockCreateEvent).not.toHaveBeenCalled();
107+
const { ERROR_CODE, ERROR_MESSAGE } = Event.ReservedAttribute;
108+
expect(mockProviderCreateEvent).toHaveBeenCalledWith(
109+
expect.objectContaining({
110+
name: Event.PresetEvent.CLICKSTREAM_ERROR,
111+
attributes: {
112+
[ERROR_CODE]: Event.ErrorCode.EVENT_NAME_INVALID,
113+
[ERROR_MESSAGE]: expect.anything(),
114+
},
115+
})
116+
);
108117
});
109118

110119
test('test setUserAttributes reached max number limit', () => {

0 commit comments

Comments
 (0)