Page updated Jan 22, 2024

Migrate from v5 to v6

This guide will help you migrate Amplify JavaScript v5's Analytics APIs to the new v6's Analytics APIs.

As of v6 of Amplify, you will now import APIs directly from the aws-amplify/analytics path when using the Pinpoint provider. If you are using the Personalize, Kinesis, or Kinesis (Firehose) provider, you will need to import separate APIs from the appropriate subpath (ie import { record } from 'aws-amplify/analytics/personalize' when using the record API with the AmazonPersonalize provider) instead of passing in the provider or options.provider parameter.

Note: we have removed the startSession API in v6. In v6, we recommend you to use the record API with the event { name: '_session.start' } instead.

Enable and Disable

In both v5 and v6, Analytics is enabled by default. You can disable and enable all Analytics providers using the enable and disable APIs exported from the aws-amplify/analytics path.

1import { enable, disable } from 'aws-amplify/analytics';
2
3// disable all Analytics providers (enabled by default)
4disable();
5
6// enable all Analytics providers
7enable();
1import { Analytics } from 'aws-amplify';
2
3Analytics.disable();
4
5Analytics.enable();

Pinpoint

In v6, provider is determined by import path. To use Pinpoint APIs you will import them directly from aws-amplify/analytics. These APIs no longer accept parameters specific to Kinesis or Personalize.

Analytics.record

The record API in v6 has not changed in behavior, however, there are a couple of changes to be aware of in v6:

  • The record API is now synchronous and no longer returns a Promise as events are always buffered.
  • The immediate option was removed from the record API in v6. Instead, you can now use the flushEvents API to clear all events from the buffer before calling record.
  • The provider parameter has been removed and provider will be determined by import path. The Pinpoint record API does not accept Personalize or Kinesis events in v6.
Input

V5

1event: AnalyticsEvent | PersonalizeAnalyticsEvent | KinesisAnalyticsEvent
2provider?: string // removed in v6
3
4// Pinpoint
5AnalyticsEvent {
6 name: string;
7 attributes?: EventAttributes;
8 metrics?: EventMetrics;
9 immediate?: boolean; // removed in v6
10}

V6

1input: {
2 name: string;
3 attributes?: Record<string, string>;
4 metrics?: Record<string, number>;
5}
1import { record } from 'aws-amplify/analytics';
2
3record({
4 name: 'albumVisit',
5 attributes: { genre: '', artist: '' },
6 metrics: { minutesListened: 30 }
7});
1import { Analytics } from 'aws-amplify';
2
3Analytics.record({
4 name: 'albumVisit',
5 attributes: { genre: '', artist: '' },
6 metrics: { minutesListened: 30 }
7});

Analytics.autoTrack

The autoTrack API has been renamed to configureAutoTrack in v6 to make the purpose of this API more clear. In v6, provider is determined by import path. To use the Pinpoint configureAutoTrack API you will import directly from aws-amplify/analytics.

The functionality of this API has not changed, however the input structure has been altered in the following ways:

  • We have transitioned from positional parameters to named parameters.
  • The opts property has been renamed to options.
  • The required enable property has been moved outside of the options field.
  • trackerType has been renamed to type.
  • Optional tracker options have been moved into the options field for all tracker types.
  • The type parameter in the pageView input type has been renamed ot appType to differentiate from the tracker type.
  • The appType values SPA and multiPageApp have been renamed to singlePage and multiPage respectively.
Input

V5

1trackerType: 'pageView' | 'event' | 'session'
2 opts: {
3 provider: string; // removed in v6
4 enable: boolean;
5 attributes?: // simplified in v6: no longer accepts a function
6 | (() => [key: string]: string | Promise<[key: string]: string>)
7 | [key: string]: string;
8 // trackerType: pageView
9 eventName?: string;
10 type?: 'SPA' | 'multiPageApp';
11 getUrl?: () => string; // renamed urlProvider in v6
12 // trackerType: event
13 events?: string[];
14 selectorPrefix?: string;
15}

V6

1input {
2 enable: boolean;
3 type: 'session' | 'pageView' | 'event'
4 options?: {
5 attributes?: Record<string, string>;
6 // type: pageView
7 eventName?: string;
8 urlProvider?: (() => string);
9 appType?: 'multiPage' | 'singlePage';
10 // type: event
11 events?: (keyof GlobalEventHandlersEventMap)[]; // 'abort' | 'animationcancel' | 'animationend' | ...
12 selectorPrefix?: string;
13 };
14}
1import { configureAutoTrack } from 'aws-amplify/analytics';
2
3configureAutoTrack({
4 enable: true,
5 type: 'session',
6 options: {
7 attributes: {
8 customizableField: 'attr'
9 }
10 }
11});
1import { Analytics } from 'aws-amplify';
2
3Analytics.autoTrack('session', {
4 enable: true,
5 attributes: {
6 customizableField: 'attr'
7 }
8});

Analytics.updateEndpoint

This API has been renamed to identifyUser in v6 to align with other platforms. The functionality is the same, although the type definition is more specific.

  • The address, optOut, and channelType options are no longer available as they do not affect Analytics endpoints.
  • attributes has been renamed to customProperties.
  • The provider parameter has been removed and provider will be determined by import path.
Input

V5

1attrs: { [key: string]: any; }
2provider?: string
3
4// The attrs parameter type is generic in v5
5// Below are the properties expected by the API
6attrs: {
7 address?: string; // removed in v6
8 attributes?: { [key: string]: any; }; // renamed customProperties in v6
9 channelType?: string; // removed in v6
10 demographic?: {
11 appVersion?: string;
12 locale?: string;
13 make?: string;
14 model?: string;
15 modelVersion?: string;
16 platform?: string;
17 platformVersion?: string;
18 timezone?: string;
19 },
20 location?: {
21 city?: string;
22 country?: string;
23 latitude?: number;
24 longitude?: number;
25 postalCode?: string;
26 region?: string;
27 },
28 metrics?: { [key: string]: any; };
29 optOut?: string; // removed in v6
30 userId: string;
31 userAttributes?: { [key: string]: any; }; // moved under options in v6
32}

V6

1input: {
2 userId: string;
3 userProfile: {
4 customProperties?: Record<string, string[]>;
5 demographic?: {
6 appVersion?: string;
7 locale?: string;
8 make?: string;
9 model?: string;
10 modelVersion?: string;
11 platform?: string;
12 platformVersion?: string;
13 timezone?: string;
14 };
15 email?: string;
16 location?: {
17 city?: string;
18 country?: string;
19 latitude?: number;
20 longitude?: number;
21 postalCode?: string;
22 region?: string;
23 };
24 metrics?: Record<string, number>;
25 name?: string;
26 plan?: string;
27 };
28 options?: { userAttributes?: Record<string, string[]>; };
29}
1import { identifyUser } from 'aws-amplify/analytics';
2
3identifyUser({
4 userId: 'xxxxxxx',
5 userProfile: {
6 customProperties: { hobbies: ['piano', 'hiking'] },
7 },
8 options: {
9 userAttributes: {
10 interests: ['football', 'basketball', 'AWS']
11 }
12 }
13});
1import { Analytics } from 'aws-amplify';
2
3Analytics.updateEndpoint({
4 userId: 'xxxxxxx',
5 attributes: { hobbies: ['piano', 'hiking'] },
6 userAttributes: {
7 interests: ['football', 'basketball', 'AWS']
8 }
9});

Kinesis and Kinesis (Firehose)

In v6, provider is determined by import path. To use Kinesis APIs you will import them from aws-amplify/analytics/kinesis and to use Kinesis (Firehose) APIs you will import them from aws-amplify/analytics/kinesis-firehose. These APIs no longer accept parameters specific to Pinpoint or Personalize.

Analytics.record

The Kinesis and Kinesis (Firehose) record APIs in v6 have not changed in behavior, however, there are a couple of changes to be aware of in v6:

  • The record API is now synchronous and no longer returns a Promise as events are always buffered.
  • The immediate option was removed from the record API in v6. Alternatively in v6 you can use the flushEvents API to clear all events from the buffer before calling record.
  • The partitionKey option has been removed from the Kinesis (Firehose) record API in v6.
  • The provider parameter has been removed and provider will be determined by import path.
Input

V5

1event: AnalyticsEvent | PersonalizeAnalyticsEvent | KinesisAnalyticsEvent
2provider?: string // removed in v6
3
4// Kinesis or Kinesis (Firehose)
5KinesisAnalyticsEvent {
6 data: object | string;
7 partitionKey: string;
8 streamName: string;
9 immediate?: boolean;
10}

V6

1input: {
2 streamName: string;
3 data: Record<string, unknown> | Uint8Array;
4 // Kinesis only
5 partitionKey: string;
6}
1import { record } from 'aws-amplify/analytics/kinesis'; // or 'aws-amplify/analytics/kinesis-firehose'
2
3// Kinesis
4kinesisRecord({
5 data: {
6 // The data blob to put into the record
7 },
8 partitionKey: 'myPartitionKey', // removed from Kinesis (Firehose) in v6
9 streamName: 'myKinesisStream'
10});
1import { Analytics, AWSKinesisProvider, AWSKinesisFirehoseProvider } from 'aws-amplify';
2Analytics.addPluggable(new AWSKinesisProvider());
3Analytics.addPluggable(new AWSKinesisFirehoseProvider());
4
5// Kinesis
6Analytics.record({
7 data: {
8 // The data blob to put into the record
9 },
10 partitionKey: 'myPartitionKey', // accepted for firehose provider but not used
11 streamName: 'myKinesisStream'
12}, 'AWSKinesisProvider'); // or 'AWSKinesisFirehoseProvider'

Personalize

To use the personalize provider in v5, you had to pass in an additional parameter to Analytics API's with the string value AmazonPersonalize. In v6, however, you will import the APIs from the aws-amplify/analytics/personalize subpath instead.

Analytics.record

The Personalize record API in v6 has not changed in behavior, however, there are a couple of changes to be aware of in v6:

  • The record API is now synchronous and no longer returns a Promise as events are always buffered.
  • The provider parameter has been removed and provider will be determined by import path.
Input

V5

1event: AnalyticsEvent | PersonalizeAnalyticsEvent | KinesisAnalyticsEvent
2provider?: string // removed in v6
3
4// Personalize
5PersonalizeAnalyticsEvent {
6 userId?: string;
7 eventType?: string;
8 properties?: {
9 [key: string]: string;
10 };
11}

V6

1input: {
2 userId?: string;
3 eventId?: string;
4 eventType: string;
5 properties: Record<string, unknown>;
6}
1import { record } from 'aws-amplify/analytics/personalize';
2
3// Personalize
4record({
5 eventType: 'Identify',
6 properties: {
7 userId: 'userId'
8 }
9});
1import { Analytics, AmazonPersonalizeProvider, AWSKinesisProvider, AWSKinesisFirehoseProvider } from 'aws-amplify';
2Analytics.addPluggable(new AmazonPersonalizeProvider());
3Analytics.addPluggable(new AWSKinesisProvider());
4Analytics.addPluggable(new AWSKinesisFirehoseProvider());
5
6Analytics.record({
7 eventType: 'Identify',
8 properties: {
9 userId: 'userId'
10 }
11}, 'AmazonPersonalize');