Automatically track sessions
These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.
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:
import { configureAutoTrack } from 'aws-amplify/analytics';
configureAutoTrack({ // REQUIRED, turn on/off the auto tracking enable: true, // REQUIRED, the event type, it's one of 'event', 'pageView' or 'session' type: 'session', // OPTIONAL, additional options for the tracked event. options: { // OPTIONAL, the attributes of the event attributes: { customizableField: 'attr' } }});By default, when the page/app transitions to the foreground, the Analytics module will send an event to the Amazon Pinpoint Service.
{ "eventType": "_session_start", "attributes": { "customizableField": "attr" }}This behavior can be disabled by calling configureAutoTrack:
import { configureAutoTrack } from 'aws-amplify/analytics';
configureAutoTrack({ enable: false, type: 'session'});