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: 
```javascript
Analytics.autoTrack('session', {
    // REQUIRED, turn on/off the auto tracking
    enable: true,
    // OPTIONAL, the attributes of the event, you can either pass an object or a function 
    // which allows you to define dynamic attributes
    attributes: {
        attr: 'attr'
    },
    // when using function
    // attributes: () => {
    //    const attr = somewhere();
    //    return {
    //        myAttr: attr
    //    }
    // },
    // OPTIONAL, the service provider, by default is the Amazon Pinpoint
    provider: 'AWSPinpoint'
});
```

When the page is loaded, the Analytics module will send an event to the Amazon Pinpoint Service.
```javascript
{ 
    eventType: '_session_start', 
    attributes: { 
        attr: 'attr' 
    }
}
```

To keep backward compatibility, the auto tracking of the session is enabled by default. You can turn it off by:
```javascript
Analytics.configure({
    // OPTIONAL - Allow recording session events. Default is true.
    autoSessionRecord: false,
});
```
or 
```javascript
Analytics.autoTrack('session', {
    enable: false
});

// Note: this must be called before Amplify.configure() or Analytics.configure() to cancel the session_start event
```

import js0 from '/src/fragments/lib/analytics/js/autotrack/page-tracking.mdx';

<Fragments fragments={{ js: js0 }} />

<Callout> 

Note: Amplify doesn't capture the location automatically. Instead, you can add the location information in the default config when you [configure Analytics](https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js#set-up-existing-analytics-backend) or while [updating the end point](https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js#update-endpoint).

</Callout>