Page updated Jan 16, 2024

Automatically track sessions

Analytics Auto Tracking helps you to automatically track user behaviors like sessions start/stop, page view change and web events like clicking or mouseover.

Session Tracking

You can track the session both in a web app or a React Native app by using Analytics. A web session can be defined in different ways. To keep it simple we define a web session as being active when the page is not hidden and inactive when the page is hidden. A session in a React Native app is active when the app is in the foreground and inactive when the app is in the background.

For example:

1Analytics.autoTrack('session', {
2 // REQUIRED, turn on/off the auto tracking
3 enable: true,
4 // OPTIONAL, the attributes of the event, you can either pass an object or a function
5 // which allows you to define dynamic attributes
6 attributes: {
7 attr: 'attr'
8 },
9 // when using function
10 // attributes: () => {
11 // const attr = somewhere();
12 // return {
13 // myAttr: attr
14 // }
15 // },
16 // OPTIONAL, the service provider, by default is the Amazon Pinpoint
17 provider: 'AWSPinpoint'
18});

When the page is loaded, the Analytics module will send an event to the Amazon Pinpoint Service.

1{
2 eventType: '_session_start',
3 attributes: {
4 attr: 'attr'
5 }
6}

To keep backward compatibility, the auto tracking of the session is enabled by default. You can turn it off by:

1Analytics.configure({
2 // OPTIONAL - Allow recording session events. Default is true.
3 autoSessionRecord: false
4});

or

1Analytics.autoTrack('session', {
2 enable: false
3});
4
5// Note: this must be called before Amplify.configure() or Analytics.configure() to cancel the session_start event

Note: Amplify doesn't capture the location automatically. Instead, you can add the location information in the default config when you configure Analytics or while updating the end point.