@aws-amplify/analytics 7.0.16 → 7.0.17-unstable.0d2aa5d.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 (120) hide show
  1. package/dist/cjs/providers/kinesis/apis/flushEvents.js +8 -6
  2. package/dist/cjs/providers/kinesis/apis/flushEvents.js.map +1 -1
  3. package/dist/cjs/providers/kinesis/apis/record.js +7 -7
  4. package/dist/cjs/providers/kinesis/apis/record.js.map +1 -1
  5. package/dist/cjs/providers/kinesis/utils/getEventBuffer.js +2 -2
  6. package/dist/cjs/providers/kinesis/utils/getEventBuffer.js.map +1 -1
  7. package/dist/cjs/providers/kinesis-firehose/apis/flushEvents.js +10 -8
  8. package/dist/cjs/providers/kinesis-firehose/apis/flushEvents.js.map +1 -1
  9. package/dist/cjs/providers/kinesis-firehose/apis/record.js +8 -8
  10. package/dist/cjs/providers/kinesis-firehose/apis/record.js.map +1 -1
  11. package/dist/cjs/providers/kinesis-firehose/utils/getEventBuffer.js +2 -2
  12. package/dist/cjs/providers/kinesis-firehose/utils/getEventBuffer.js.map +1 -1
  13. package/dist/cjs/providers/personalize/apis/flushEvents.js +10 -8
  14. package/dist/cjs/providers/personalize/apis/flushEvents.js.map +1 -1
  15. package/dist/cjs/providers/personalize/apis/record.js +14 -14
  16. package/dist/cjs/providers/personalize/apis/record.js.map +1 -1
  17. package/dist/cjs/providers/personalize/utils/autoTrackMedia.js +8 -8
  18. package/dist/cjs/providers/personalize/utils/autoTrackMedia.js.map +1 -1
  19. package/dist/cjs/providers/personalize/utils/getEventBuffer.js +2 -2
  20. package/dist/cjs/providers/personalize/utils/getEventBuffer.js.map +1 -1
  21. package/dist/cjs/providers/pinpoint/apis/flushEvents.js +20 -16
  22. package/dist/cjs/providers/pinpoint/apis/flushEvents.js.map +1 -1
  23. package/dist/cjs/providers/pinpoint/apis/identifyUser.js +1 -1
  24. package/dist/cjs/providers/pinpoint/apis/identifyUser.js.map +1 -1
  25. package/dist/cjs/trackers/PageViewTracker.js +2 -4
  26. package/dist/cjs/trackers/PageViewTracker.js.map +1 -1
  27. package/dist/cjs/utils/resolveCredentials.js +1 -1
  28. package/dist/cjs/utils/resolveCredentials.js.map +1 -1
  29. package/dist/esm/apis/disable.mjs +1 -1
  30. package/dist/esm/apis/enable.mjs +1 -1
  31. package/dist/esm/providers/kinesis/apis/flushEvents.mjs +5 -3
  32. package/dist/esm/providers/kinesis/apis/flushEvents.mjs.map +1 -1
  33. package/dist/esm/providers/kinesis/apis/record.mjs +3 -3
  34. package/dist/esm/providers/kinesis/apis/record.mjs.map +1 -1
  35. package/dist/esm/providers/kinesis/types/inputs.d.ts +2 -2
  36. package/dist/esm/providers/kinesis/utils/getEventBuffer.mjs +3 -3
  37. package/dist/esm/providers/kinesis/utils/getEventBuffer.mjs.map +1 -1
  38. package/dist/esm/providers/kinesis-firehose/apis/flushEvents.mjs +5 -3
  39. package/dist/esm/providers/kinesis-firehose/apis/flushEvents.mjs.map +1 -1
  40. package/dist/esm/providers/kinesis-firehose/apis/record.mjs +3 -3
  41. package/dist/esm/providers/kinesis-firehose/apis/record.mjs.map +1 -1
  42. package/dist/esm/providers/kinesis-firehose/types/buffer.d.ts +1 -1
  43. package/dist/esm/providers/kinesis-firehose/types/inputs.d.ts +2 -2
  44. package/dist/esm/providers/kinesis-firehose/utils/getEventBuffer.mjs +3 -3
  45. package/dist/esm/providers/kinesis-firehose/utils/getEventBuffer.mjs.map +1 -1
  46. package/dist/esm/providers/personalize/apis/flushEvents.mjs +5 -3
  47. package/dist/esm/providers/personalize/apis/flushEvents.mjs.map +1 -1
  48. package/dist/esm/providers/personalize/apis/record.mjs +3 -3
  49. package/dist/esm/providers/personalize/apis/record.mjs.map +1 -1
  50. package/dist/esm/providers/personalize/types/buffer.d.ts +4 -4
  51. package/dist/esm/providers/personalize/types/inputs.d.ts +2 -2
  52. package/dist/esm/providers/personalize/utils/autoTrackMedia.d.ts +2 -2
  53. package/dist/esm/providers/personalize/utils/autoTrackMedia.mjs +8 -8
  54. package/dist/esm/providers/personalize/utils/autoTrackMedia.mjs.map +1 -1
  55. package/dist/esm/providers/personalize/utils/cachedSession.mjs.map +1 -1
  56. package/dist/esm/providers/personalize/utils/getEventBuffer.mjs +3 -3
  57. package/dist/esm/providers/personalize/utils/getEventBuffer.mjs.map +1 -1
  58. package/dist/esm/providers/pinpoint/apis/configureAutoTrack.mjs +1 -1
  59. package/dist/esm/providers/pinpoint/apis/flushEvents.mjs +18 -14
  60. package/dist/esm/providers/pinpoint/apis/flushEvents.mjs.map +1 -1
  61. package/dist/esm/providers/pinpoint/apis/identifyUser.d.ts +1 -1
  62. package/dist/esm/providers/pinpoint/apis/identifyUser.mjs +2 -2
  63. package/dist/esm/providers/pinpoint/apis/identifyUser.mjs.map +1 -1
  64. package/dist/esm/providers/pinpoint/apis/record.mjs.map +1 -1
  65. package/dist/esm/providers/pinpoint/types/inputs.d.ts +1 -1
  66. package/dist/esm/trackers/PageViewTracker.mjs +2 -4
  67. package/dist/esm/trackers/PageViewTracker.mjs.map +1 -1
  68. package/dist/esm/trackers/SessionTracker.mjs.map +1 -1
  69. package/dist/esm/trackers/SessionTracker.native.mjs.map +1 -1
  70. package/dist/esm/types/inputs.d.ts +3 -3
  71. package/dist/esm/types/kinesis.d.ts +2 -2
  72. package/dist/esm/types/trackers.d.ts +7 -7
  73. package/dist/esm/utils/eventBuffer/types.d.ts +3 -5
  74. package/dist/esm/utils/resolveCredentials.mjs +1 -1
  75. package/dist/esm/utils/resolveCredentials.mjs.map +1 -1
  76. package/dist/esm/utils/userAgent.mjs.map +1 -1
  77. package/package.json +110 -110
  78. package/src/errors/assertValidationError.ts +1 -1
  79. package/src/providers/kinesis/apis/flushEvents.ts +7 -4
  80. package/src/providers/kinesis/apis/record.ts +6 -3
  81. package/src/providers/kinesis/types/buffer.ts +1 -0
  82. package/src/providers/kinesis/types/inputs.ts +2 -2
  83. package/src/providers/kinesis/utils/getEventBuffer.ts +22 -21
  84. package/src/providers/kinesis/utils/resolveConfig.ts +4 -1
  85. package/src/providers/kinesis-firehose/apis/flushEvents.ts +7 -4
  86. package/src/providers/kinesis-firehose/apis/record.ts +6 -3
  87. package/src/providers/kinesis-firehose/types/buffer.ts +2 -1
  88. package/src/providers/kinesis-firehose/types/inputs.ts +2 -2
  89. package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +23 -21
  90. package/src/providers/kinesis-firehose/utils/resolveConfig.ts +4 -1
  91. package/src/providers/personalize/apis/flushEvents.ts +7 -4
  92. package/src/providers/personalize/apis/record.ts +8 -5
  93. package/src/providers/personalize/types/buffer.ts +6 -4
  94. package/src/providers/personalize/types/inputs.ts +2 -2
  95. package/src/providers/personalize/utils/autoTrackMedia.ts +34 -26
  96. package/src/providers/personalize/utils/cachedSession.ts +5 -5
  97. package/src/providers/personalize/utils/getEventBuffer.ts +24 -20
  98. package/src/providers/personalize/utils/resolveConfig.ts +4 -2
  99. package/src/providers/pinpoint/apis/configureAutoTrack.ts +5 -2
  100. package/src/providers/pinpoint/apis/flushEvents.ts +8 -5
  101. package/src/providers/pinpoint/apis/identifyUser.ts +3 -2
  102. package/src/providers/pinpoint/apis/record.ts +4 -2
  103. package/src/providers/pinpoint/types/inputs.ts +3 -1
  104. package/src/providers/pinpoint/utils/resolveConfig.ts +1 -0
  105. package/src/providers/pinpoint/utils/resolveCredentials.ts +3 -1
  106. package/src/trackers/EventTracker.ts +7 -6
  107. package/src/trackers/PageViewTracker.ts +8 -13
  108. package/src/trackers/SessionTracker.native.ts +6 -5
  109. package/src/trackers/SessionTracker.ts +6 -5
  110. package/src/types/inputs.ts +6 -5
  111. package/src/types/kinesis.ts +2 -2
  112. package/src/types/trackers.ts +8 -8
  113. package/src/utils/eventBuffer/EventBuffer.ts +3 -2
  114. package/src/utils/eventBuffer/types.ts +3 -5
  115. package/src/utils/groupBy.ts +3 -2
  116. package/src/utils/resolveCredentials.ts +4 -2
  117. package/src/utils/trackerConfigHelpers.native.ts +2 -2
  118. package/src/utils/trackerConfigHelpers.ts +2 -2
  119. package/src/utils/trackerHelpers.ts +1 -2
  120. package/src/utils/userAgent.ts +1 -1
@@ -2,10 +2,12 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { Amplify } from '@aws-amplify/core';
5
+
5
6
  import {
6
7
  AnalyticsValidationErrorCode,
7
8
  assertValidationError,
8
9
  } from '../../../errors';
10
+
9
11
  import { DEFAULT_KINESIS_CONFIG } from './constants';
10
12
 
11
13
  export const resolveConfig = () => {
@@ -24,8 +26,9 @@ export const resolveConfig = () => {
24
26
  assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
25
27
  assertValidationError(
26
28
  flushSize < bufferSize,
27
- AnalyticsValidationErrorCode.InvalidFlushSize
29
+ AnalyticsValidationErrorCode.InvalidFlushSize,
28
30
  );
31
+
29
32
  return {
30
33
  region,
31
34
  bufferSize,
@@ -1,13 +1,14 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
5
+ import { ConsoleLogger } from '@aws-amplify/core';
6
+
4
7
  import { getEventBuffer, resolveConfig } from '../utils';
5
8
  import {
6
9
  getAnalyticsUserAgentString,
7
10
  resolveCredentials,
8
11
  } from '../../../utils';
9
- import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
10
- import { ConsoleLogger } from '@aws-amplify/core';
11
12
 
12
13
  const logger = new ConsoleLogger('KinesisFirehose');
13
14
 
@@ -32,8 +33,10 @@ export const flushEvents = () => {
32
33
  identityId,
33
34
  resendLimit,
34
35
  userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
35
- })
36
+ }),
36
37
  )
37
38
  .then(eventBuffer => eventBuffer.flushAll())
38
- .catch(e => logger.warn('Failed to flush events.', e));
39
+ .catch(e => {
40
+ logger.warn('Failed to flush events.', e);
41
+ });
39
42
  };
@@ -1,6 +1,10 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { fromUtf8 } from '@smithy/util-utf8';
5
+ import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6
+ import { ConsoleLogger } from '@aws-amplify/core';
7
+
4
8
  import { RecordInput } from '../types';
5
9
  import { getEventBuffer, resolveConfig } from '../utils';
6
10
  import {
@@ -8,9 +12,7 @@ import {
8
12
  isAnalyticsEnabled,
9
13
  resolveCredentials,
10
14
  } from '../../../utils';
11
- import { fromUtf8 } from '@smithy/util-utf8';
12
- import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
13
- import { ConsoleLogger } from '@aws-amplify/core';
15
+ import { AnalyticsValidationErrorCode } from '../../../errors';
14
16
 
15
17
  const logger = new ConsoleLogger('KinesisFirehose');
16
18
 
@@ -34,6 +36,7 @@ const logger = new ConsoleLogger('KinesisFirehose');
34
36
  export const record = ({ streamName, data }: RecordInput): void => {
35
37
  if (!isAnalyticsEnabled()) {
36
38
  logger.debug('Analytics is disabled, event will not be recorded.');
39
+
37
40
  return;
38
41
  }
39
42
 
@@ -1,9 +1,10 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { AWSCredentials } from '@aws-amplify/core/internals/utils';
5
+
4
6
  import { EventBufferConfig } from '../../../utils';
5
7
  import { KinesisStream } from '../../../types';
6
- import { AWSCredentials } from '@aws-amplify/core/internals/utils';
7
8
 
8
9
  export type KinesisFirehoseBufferEvent = KinesisStream & {
9
10
  event: Uint8Array;
@@ -3,7 +3,7 @@
3
3
 
4
4
  import { KinesisEventData } from '../../../types';
5
5
 
6
- export type RecordInput = {
6
+ export interface RecordInput {
7
7
  streamName: string;
8
8
  data: KinesisEventData;
9
- };
9
+ }
@@ -1,19 +1,16 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import {
5
- AWSCredentials,
6
- haveCredentialsChanged
4
+ import {
5
+ AWSCredentials,
6
+ haveCredentialsChanged,
7
7
  } from '@aws-amplify/core/internals/utils';
8
8
  import {
9
9
  FirehoseClient,
10
10
  PutRecordBatchCommand,
11
11
  } from '@aws-sdk/client-firehose';
12
- import {
13
- EventBuffer,
14
- groupBy,
15
- IAnalyticsClient
16
- } from '../../../utils';
12
+
13
+ import { EventBuffer, IAnalyticsClient, groupBy } from '../../../utils';
17
14
  import {
18
15
  KinesisFirehoseBufferEvent,
19
16
  KinesisFirehoseEventBufferConfig,
@@ -35,7 +32,7 @@ const cachedClients: Record<string, [FirehoseClient, AWSCredentials]> = {};
35
32
 
36
33
  const createPutRecordsBatchCommand = (
37
34
  streamName: string,
38
- events: KinesisFirehoseBufferEvent[]
35
+ events: KinesisFirehoseBufferEvent[],
39
36
  ): PutRecordBatchCommand =>
40
37
  new PutRecordBatchCommand({
41
38
  DeliveryStreamName: streamName,
@@ -47,24 +44,25 @@ const createPutRecordsBatchCommand = (
47
44
  const submitEvents = async (
48
45
  events: KinesisFirehoseBufferEvent[],
49
46
  client: FirehoseClient,
50
- resendLimit?: number
47
+ resendLimit?: number,
51
48
  ): Promise<KinesisFirehoseBufferEvent[]> => {
52
49
  const groupedByStreamName = Object.entries(
53
- groupBy(event => event.streamName, events)
50
+ groupBy(event => event.streamName, events),
54
51
  );
55
52
 
56
53
  const requests = groupedByStreamName
57
- .map(([streamName, events]) =>
58
- createPutRecordsBatchCommand(streamName, events)
54
+ .map(([streamName, groupedEvents]) =>
55
+ createPutRecordsBatchCommand(streamName, groupedEvents),
59
56
  )
60
57
  .map(command => client.send(command));
61
58
 
62
59
  const responses = await Promise.allSettled(requests);
63
60
  const failedEvents = responses
64
61
  .map((response, i) =>
65
- response.status === 'rejected' ? groupedByStreamName[i][1] : []
62
+ response.status === 'rejected' ? groupedByStreamName[i][1] : [],
66
63
  )
67
64
  .flat();
65
+
68
66
  return resendLimit
69
67
  ? failedEvents
70
68
  .filter(event => event.retryCount < resendLimit)
@@ -84,12 +82,16 @@ export const getEventBuffer = ({
84
82
  userAgentValue,
85
83
  }: KinesisFirehoseEventBufferConfig): EventBuffer<KinesisFirehoseBufferEvent> => {
86
84
  const sessionIdentityKey = [region, identityId].filter(id => !!id).join('-');
87
- const [ cachedClient, cachedCredentials ] = cachedClients[sessionIdentityKey] ?? [];
85
+ const [cachedClient, cachedCredentials] =
86
+ cachedClients[sessionIdentityKey] ?? [];
88
87
  let credentialsHaveChanged = false;
89
88
 
90
89
  // Check if credentials have changed for the cached client
91
90
  if (cachedClient) {
92
- credentialsHaveChanged = haveCredentialsChanged(cachedCredentials, credentials);
91
+ credentialsHaveChanged = haveCredentialsChanged(
92
+ cachedCredentials,
93
+ credentials,
94
+ );
93
95
  }
94
96
 
95
97
  if (!eventBufferMap[sessionIdentityKey] || credentialsHaveChanged) {
@@ -101,12 +103,12 @@ export const getEventBuffer = ({
101
103
  credentials,
102
104
  customUserAgent: userAgentValue,
103
105
  }),
104
- credentials
106
+ credentials,
105
107
  ];
106
108
  }
107
109
 
108
- const [ firehoseClient ] = cachedClients[sessionIdentityKey];
109
-
110
+ const [firehoseClient] = cachedClients[sessionIdentityKey];
111
+
110
112
  return events => submitEvents(events, firehoseClient, resendLimit);
111
113
  };
112
114
 
@@ -117,11 +119,11 @@ export const getEventBuffer = ({
117
119
  flushSize,
118
120
  flushInterval,
119
121
  },
120
- getClient
122
+ getClient,
121
123
  );
122
124
 
123
125
  const releaseSessionKeys = Object.keys(eventBufferMap).filter(
124
- key => key !== sessionIdentityKey
126
+ key => key !== sessionIdentityKey,
125
127
  );
126
128
  for (const releaseSessionKey of releaseSessionKeys) {
127
129
  eventBufferMap[releaseSessionKey].flushAll().finally(() => {
@@ -2,10 +2,12 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { Amplify } from '@aws-amplify/core';
5
+
5
6
  import {
6
7
  AnalyticsValidationErrorCode,
7
8
  assertValidationError,
8
9
  } from '../../../errors';
10
+
9
11
  import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from './constants';
10
12
 
11
13
  export const resolveConfig = () => {
@@ -24,8 +26,9 @@ export const resolveConfig = () => {
24
26
  assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
25
27
  assertValidationError(
26
28
  flushSize < bufferSize,
27
- AnalyticsValidationErrorCode.InvalidFlushSize
29
+ AnalyticsValidationErrorCode.InvalidFlushSize,
28
30
  );
31
+
29
32
  return {
30
33
  region,
31
34
  bufferSize,
@@ -1,13 +1,14 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
5
+ import { ConsoleLogger } from '@aws-amplify/core';
6
+
4
7
  import { getEventBuffer, resolveConfig } from '../utils';
5
8
  import {
6
9
  getAnalyticsUserAgentString,
7
10
  resolveCredentials,
8
11
  } from '../../../utils';
9
- import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
10
- import { ConsoleLogger } from '@aws-amplify/core';
11
12
 
12
13
  const logger = new ConsoleLogger('Personalize');
13
14
 
@@ -30,8 +31,10 @@ export const flushEvents = () => {
30
31
  credentials,
31
32
  identityId,
32
33
  userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
33
- })
34
+ }),
34
35
  )
35
36
  .then(eventBuffer => eventBuffer.flushAll())
36
- .catch(e => logger.warn('Failed to flush events', e));
37
+ .catch(e => {
38
+ logger.warn('Failed to flush events', e);
39
+ });
37
40
  };
@@ -1,6 +1,9 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
5
+ import { ConsoleLogger } from '@aws-amplify/core';
6
+
4
7
  import { RecordInput } from '../types';
5
8
  import {
6
9
  autoTrackMedia,
@@ -14,12 +17,11 @@ import {
14
17
  isAnalyticsEnabled,
15
18
  resolveCredentials,
16
19
  } from '../../../utils';
17
- import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
18
- import { ConsoleLogger } from '@aws-amplify/core';
19
20
  import {
20
21
  IDENTIFY_EVENT_TYPE,
21
22
  MEDIA_AUTO_TRACK_EVENT_TYPE,
22
23
  } from '../utils/constants';
24
+ import { AnalyticsValidationErrorCode } from '../../../errors';
23
25
 
24
26
  const logger = new ConsoleLogger('Personalize');
25
27
 
@@ -56,6 +58,7 @@ export const record = ({
56
58
  }: RecordInput): void => {
57
59
  if (!isAnalyticsEnabled()) {
58
60
  logger.debug('Analytics is disabled, event will not be recorded.');
61
+
59
62
  return;
60
63
  }
61
64
 
@@ -70,9 +73,9 @@ export const record = ({
70
73
  updateCachedSession(
71
74
  typeof properties.userId === 'string' ? properties.userId : '',
72
75
  cachedSessionId,
73
- cachedUserId
76
+ cachedUserId,
74
77
  );
75
- } else if (!!userId) {
78
+ } else if (userId) {
76
79
  updateCachedSession(userId, cachedSessionId, cachedUserId);
77
80
  }
78
81
 
@@ -101,7 +104,7 @@ export const record = ({
101
104
  properties,
102
105
  },
103
106
  },
104
- eventBuffer
107
+ eventBuffer,
105
108
  );
106
109
  } else {
107
110
  eventBuffer.append({
@@ -1,17 +1,19 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { PersonalizeEvent } from './';
5
- import { EventBufferConfig } from '../../../utils';
6
4
  import { AWSCredentials } from '@aws-amplify/core/internals/utils';
7
5
 
8
- export type PersonalizeBufferEvent = {
6
+ import { EventBufferConfig } from '../../../utils';
7
+
8
+ import { PersonalizeEvent } from './';
9
+
10
+ export interface PersonalizeBufferEvent {
9
11
  trackingId: string;
10
12
  sessionId?: string;
11
13
  userId?: string;
12
14
  event: PersonalizeEvent;
13
15
  timestamp: number;
14
- };
16
+ }
15
17
 
16
18
  export type PersonalizeBufferConfig = EventBufferConfig & {
17
19
  region: string;
@@ -1,11 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- export type PersonalizeEvent = {
4
+ export interface PersonalizeEvent {
5
5
  userId?: string;
6
6
  eventId?: string;
7
7
  eventType: string;
8
8
  properties: Record<string, unknown>;
9
- };
9
+ }
10
10
 
11
11
  export type RecordInput = PersonalizeEvent;
@@ -1,11 +1,12 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { EventBuffer } from '../../../utils';
5
- import { PersonalizeBufferEvent, PersonalizeEvent } from '../types';
6
4
  import { isBrowser } from '@aws-amplify/core/internals/utils';
7
5
  import { ConsoleLogger } from '@aws-amplify/core';
8
6
 
7
+ import { EventBuffer } from '../../../utils';
8
+ import { PersonalizeBufferEvent, PersonalizeEvent } from '../types';
9
+
9
10
  enum HTML5_MEDIA_EVENT {
10
11
  'PLAY' = 'play',
11
12
  'PAUSE' = 'pause',
@@ -25,28 +26,30 @@ enum EVENT_TYPE {
25
26
  'TIME_WATCHED' = 'TimeWatched',
26
27
  }
27
28
 
28
- interface IRecordEvent {
29
- (eventType: string, properties: Record<string, unknown>): void;
30
- }
29
+ type IRecordEvent = (
30
+ eventType: string,
31
+ properties: Record<string, unknown>,
32
+ ) => void;
31
33
 
32
- type MediaAutoTrackConfig = {
34
+ interface MediaAutoTrackConfig {
33
35
  trackingId: string;
34
36
  sessionId: string;
35
37
  userId?: string;
36
38
  event: PersonalizeEvent;
37
- };
39
+ }
38
40
 
39
41
  const logger = new ConsoleLogger('MediaAutoTrack');
40
42
 
41
43
  const startIframeAutoTracking = (
42
44
  element: HTMLElement,
43
- recordEvent: IRecordEvent
45
+ recordEvent: IRecordEvent,
44
46
  ) => {
45
47
  let isPlaying = false;
46
48
  let player: any;
47
49
  const mediaProperties = (): Record<string, unknown> => {
48
50
  const duration = Number(parseFloat(player.getDuration()).toFixed(4));
49
51
  const currentTime = Number(parseFloat(player.getCurrentTime()).toFixed(4));
52
+
50
53
  return {
51
54
  duration,
52
55
  eventValue: Number((currentTime / duration).toFixed(4)),
@@ -64,23 +67,22 @@ const startIframeAutoTracking = (
64
67
  }
65
68
  }, 3_000);
66
69
 
67
- element.addEventListener('unload', () => clearInterval(timer));
70
+ element.addEventListener('unload', () => {
71
+ clearInterval(timer);
72
+ });
68
73
 
69
- // @ts-ignore
70
- window.onYouTubeIframeAPIReady = () => {
71
- // @ts-ignore
72
- delete window.onYouTubeIframeAPIReady;
74
+ (window as any).onYouTubeIframeAPIReady = () => {
75
+ delete (window as any).onYouTubeIframeAPIReady;
73
76
 
74
- // @ts-ignore
75
- player = new window.YT.Player(element.id, {
77
+ player = new (window as any).YT.Player(element.id, {
76
78
  events: {
77
79
  onStateChange: (event: any) => {
78
- const iframeEventMapping = {
80
+ const iframeEventMapping: Record<number, EVENT_TYPE> = {
79
81
  0: EVENT_TYPE.ENDED,
80
82
  1: EVENT_TYPE.PLAY,
81
83
  2: EVENT_TYPE.PAUSE,
82
84
  };
83
- // @ts-ignore
85
+
84
86
  const eventType = iframeEventMapping[event.data];
85
87
  switch (eventType) {
86
88
  case EVENT_TYPE.ENDED:
@@ -103,7 +105,7 @@ const startIframeAutoTracking = (
103
105
 
104
106
  const startHTMLMediaAutoTracking = (
105
107
  element: HTMLMediaElement,
106
- recordEvent: IRecordEvent
108
+ recordEvent: IRecordEvent,
107
109
  ) => {
108
110
  let isPlaying = false;
109
111
  const mediaProperties = (): Record<string, unknown> => ({
@@ -117,7 +119,9 @@ const startHTMLMediaAutoTracking = (
117
119
  }
118
120
  }, 3_000);
119
121
 
120
- element.addEventListener('unload', () => clearInterval(timer));
122
+ element.addEventListener('unload', () => {
123
+ clearInterval(timer);
124
+ });
121
125
 
122
126
  element.addEventListener(HTML5_MEDIA_EVENT.PLAY, () => {
123
127
  isPlaying = true;
@@ -137,7 +141,7 @@ const startHTMLMediaAutoTracking = (
137
141
 
138
142
  const checkElementLoaded = (interval: number, maxTries: number) => {
139
143
  let retryCount = 0;
140
- const wait = () => new Promise(r => setTimeout(r, interval));
144
+ const wait = () => new Promise(resolve => setTimeout(resolve, interval));
141
145
  const check = async (elementId: string): Promise<boolean> => {
142
146
  if (retryCount >= maxTries) {
143
147
  return false;
@@ -149,16 +153,18 @@ const checkElementLoaded = (interval: number, maxTries: number) => {
149
153
  } else {
150
154
  retryCount += 1;
151
155
  await wait();
152
- return await check(elementId);
156
+
157
+ return check(elementId);
153
158
  }
154
159
  };
160
+
155
161
  return check;
156
162
  };
157
163
 
158
164
  const recordEvent =
159
165
  (
160
166
  config: MediaAutoTrackConfig,
161
- eventBuffer: EventBuffer<PersonalizeBufferEvent>
167
+ eventBuffer: EventBuffer<PersonalizeBufferEvent>,
162
168
  ): IRecordEvent =>
163
169
  (eventType: string, properties: Record<string, unknown>) => {
164
170
  // override eventType and merge properties
@@ -178,19 +184,21 @@ const recordEvent =
178
184
 
179
185
  export const autoTrackMedia = async (
180
186
  config: MediaAutoTrackConfig,
181
- eventBuffer: EventBuffer<PersonalizeBufferEvent>
187
+ eventBuffer: EventBuffer<PersonalizeBufferEvent>,
182
188
  ) => {
183
189
  const { eventType, properties } = config.event;
184
190
  const { domElementId, ...otherProperties } = properties;
185
191
  if (!isBrowser()) {
186
192
  logger.debug(`${eventType} only for browser`);
193
+
187
194
  return;
188
195
  }
189
196
 
190
197
  if (typeof domElementId === 'string' && !domElementId) {
191
198
  logger.debug(
192
- "Missing domElementId field in 'properties' for MediaAutoTrack event type."
199
+ "Missing domElementId field in 'properties' for MediaAutoTrack event type.",
193
200
  );
201
+
194
202
  return;
195
203
  }
196
204
 
@@ -210,7 +218,7 @@ export const autoTrackMedia = async (
210
218
  case MEDIA_TYPE.IFRAME:
211
219
  startIframeAutoTracking(
212
220
  element,
213
- recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer)
221
+ recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer),
214
222
  );
215
223
  break;
216
224
  case MEDIA_TYPE.VIDEO:
@@ -218,7 +226,7 @@ export const autoTrackMedia = async (
218
226
  if (element instanceof HTMLMediaElement) {
219
227
  startHTMLMediaAutoTracking(
220
228
  element,
221
- recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer)
229
+ recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer),
222
230
  );
223
231
  }
224
232
  break;
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { Cache } from '@aws-amplify/core';
5
- import { isBrowser, amplifyUuid } from '@aws-amplify/core/internals/utils';
5
+ import { amplifyUuid, isBrowser } from '@aws-amplify/core/internals/utils';
6
6
 
7
7
  const PERSONALIZE_CACHE_USERID = '_awsct_uid';
8
8
  const PERSONALIZE_CACHE_SESSIONID = '_awsct_sid';
@@ -12,14 +12,14 @@ const CACHE_EXPIRY_IN_DAYS = 7;
12
12
 
13
13
  const normalize = (key: string): string =>
14
14
  [key, isBrowser() ? window.location.host : DEFAULT_CACHE_PREFIX].join(
15
- DELIMITER
15
+ DELIMITER,
16
16
  );
17
17
 
18
18
  const getCache = (key: string) => Cache.getItem(normalize(key));
19
19
 
20
20
  const setCache = (key: string, value: unknown) => {
21
21
  const expiredAt = new Date(
22
- Date.now() + 3_600_000 * 24 * CACHE_EXPIRY_IN_DAYS
22
+ Date.now() + 3_600_000 * 24 * CACHE_EXPIRY_IN_DAYS,
23
23
  );
24
24
  Cache.setItem(normalize(key), value, {
25
25
  expires: expiredAt.getTime(),
@@ -28,7 +28,7 @@ const setCache = (key: string, value: unknown) => {
28
28
 
29
29
  export const resolveCachedSession = async () => {
30
30
  let sessionId: string | undefined = await getCache(
31
- PERSONALIZE_CACHE_SESSIONID
31
+ PERSONALIZE_CACHE_SESSIONID,
32
32
  );
33
33
  if (!sessionId) {
34
34
  sessionId = amplifyUuid();
@@ -46,7 +46,7 @@ export const resolveCachedSession = async () => {
46
46
  export const updateCachedSession = (
47
47
  newUserId?: string,
48
48
  currentSessionId?: string,
49
- currentUserId?: string
49
+ currentUserId?: string,
50
50
  ) => {
51
51
  const isNoCachedSession = !currentSessionId;
52
52
  const isSignOutCase = !newUserId && !currentUserId;