|
| 1 | +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + title: 'Automatically track sessions', |
| 5 | + description: 'The Amplify analytics plugin records when an application opens and closes. This session information can be viewed either from your local computer’s terminal or the AWS Console for Pinpoint.', |
| 6 | + platforms: [ |
| 7 | + 'javascript', |
| 8 | + 'react-native', |
| 9 | + 'swift', |
| 10 | + 'android', |
| 11 | + 'flutter', |
| 12 | + 'angular', |
| 13 | + 'nextjs', |
| 14 | + 'react', |
| 15 | + 'vue' |
| 16 | + ], |
| 17 | +}; |
| 18 | + |
| 19 | +export const getStaticPaths = async () => { |
| 20 | + return getCustomStaticPath(meta.platforms); |
| 21 | +}; |
| 22 | + |
| 23 | +export function getStaticProps(context) { |
| 24 | + return { |
| 25 | + props: { |
| 26 | + platform: context.params.platform, |
| 27 | + meta |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +<InlineFilter filters={['javascript', "angular", "react", "vue", "react-native", "nextjs"]}> |
| 34 | +Analytics Auto Tracking helps you to automatically track user behaviors like sessions start/stop, page view change and web events like clicking or mouseover. |
| 35 | + |
| 36 | +## Session Tracking |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +For example: |
| 41 | + |
| 42 | +```javascript |
| 43 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 44 | + |
| 45 | +configureAutoTrack({ |
| 46 | + // REQUIRED, turn on/off the auto tracking |
| 47 | + enable: true, |
| 48 | + // REQUIRED, the event type, it's one of 'event', 'pageView' or 'session' |
| 49 | + type: 'session', |
| 50 | + // OPTIONAL, additional options for the tracked event. |
| 51 | + options: { |
| 52 | + // OPTIONAL, the attributes of the event |
| 53 | + attributes: { |
| 54 | + customizableField: 'attr' |
| 55 | + } |
| 56 | + } |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +By default, when the page/app transitions to the foreground, the Analytics module will send an event to the Amazon Pinpoint Service. |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "eventType": "_session_start", |
| 65 | + "attributes": { |
| 66 | + "customizableField": "attr" |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +This behavior can be disabled by calling `configureAutoTrack`: |
| 72 | + |
| 73 | +```javascript |
| 74 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 75 | + |
| 76 | +configureAutoTrack({ |
| 77 | + enable: false, |
| 78 | + type: 'session' |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +## Page View Tracking |
| 83 | + |
| 84 | +Use this feature to track the most frequently viewed page/url in your webapp. It automatically sends events containing url information when a page is visited. |
| 85 | + |
| 86 | +This behavior can be enabled by calling `configureAutoTrack`: |
| 87 | +```javascript |
| 88 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 89 | + |
| 90 | +configureAutoTrack({ |
| 91 | + // REQUIRED, turn on/off the auto tracking |
| 92 | + enable: true, |
| 93 | + // REQUIRED, the event type, it's one of 'event', 'pageView' or 'session' |
| 94 | + type: 'pageView', |
| 95 | + // OPTIONAL, additional options for the tracked event. |
| 96 | + options: { |
| 97 | + // OPTIONAL, the attributes of the event |
| 98 | + attributes: { |
| 99 | + customizableField: 'attr' |
| 100 | + }, |
| 101 | + |
| 102 | + // OPTIONAL, the event name. By default, this is 'pageView' |
| 103 | + eventName: 'pageView', |
| 104 | + |
| 105 | + // OPTIONAL, the type of app under tracking. By default, this is 'multiPageApp'. |
| 106 | + // You will need to change it to 'singlePage' if your app is a single-page app like React |
| 107 | + appType: 'multiPageApp', |
| 108 | + |
| 109 | + // OPTIONAL, provide the URL for the event. |
| 110 | + urlProvider: () => { |
| 111 | + // the default function |
| 112 | + return window.location.origin + window.location.pathname; |
| 113 | + } |
| 114 | + } |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +This behavior can be disabled by calling `configureAutoTrack`: |
| 119 | +```javascript |
| 120 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 121 | + |
| 122 | +configureAutoTrack({ |
| 123 | + enable: false, |
| 124 | + type: 'pageView' |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +## Page Event Tracking |
| 129 | + |
| 130 | +Use this type of tracking to track user interactions with specific elements on a page. Just attach the specified selectors to your DOM element and turn on the auto tracking. |
| 131 | + |
| 132 | +This behavior can be enabled by calling `configureAutoTrack`: |
| 133 | +```javascript |
| 134 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 135 | + |
| 136 | +configureAutoTrack({ |
| 137 | + // REQUIRED, turn on/off the auto tracking |
| 138 | + enable: true, |
| 139 | + // REQUIRED, the event type, it's one of 'event', 'pageView' or 'session' |
| 140 | + type: 'event', |
| 141 | + // OPTIONAL, additional options for the tracked event. |
| 142 | + options: { |
| 143 | + // OPTIONAL, the attributes of the event |
| 144 | + attributes: { |
| 145 | + customizableField: 'attr' |
| 146 | + }, |
| 147 | + // OPTIONAL, events you want to track. By default, this is 'click' |
| 148 | + events: ['click'], |
| 149 | + |
| 150 | + // OPTIONAL, the prefix of the selectors. By default, this is 'data-amplify-analytics-' |
| 151 | + // Per https://www.w3schools.com/tags/att_global_data.asp, always start |
| 152 | + // the prefix with 'data' to avoid collisions with the user agent |
| 153 | + selectorPrefix: 'data-amplify-analytics-' |
| 154 | + } |
| 155 | +}); |
| 156 | +``` |
| 157 | + |
| 158 | +For example: |
| 159 | + |
| 160 | +```html |
| 161 | +<!-- you want to track this button and send an event when it is clicked --> |
| 162 | +<button |
| 163 | + data-amplify-analytics-on="click" |
| 164 | + data-amplify-analytics-name="click" |
| 165 | + data-amplify-analytics-attrs="attr1:attr1_value,attr2:attr2_value" |
| 166 | +/> |
| 167 | +``` |
| 168 | + |
| 169 | +When the button above is clicked, an event will be sent automatically. This is equivalent to doing: |
| 170 | + |
| 171 | +```html |
| 172 | +<script> |
| 173 | + import { record } from 'aws-amplify/analytics'; |
| 174 | + var sendEvent = function() { |
| 175 | + record({ |
| 176 | + name: 'click', |
| 177 | + attributes: { |
| 178 | + attr: 'attr', // the default ones |
| 179 | + attr1: attr1_value, // defined in the button component |
| 180 | + attr2: attr2_value // defined in the button component |
| 181 | + } |
| 182 | + }); |
| 183 | + }; |
| 184 | +</script> |
| 185 | +<button onclick="sendEvent()" /> |
| 186 | +``` |
| 187 | + |
| 188 | +This behavior can be disabled by calling `configureAutoTrack`: |
| 189 | +```javascript |
| 190 | +import { configureAutoTrack } from 'aws-amplify/analytics'; |
| 191 | + |
| 192 | +configureAutoTrack({ |
| 193 | + enable: false, |
| 194 | + type: 'event' |
| 195 | +}); |
| 196 | +``` |
| 197 | + |
| 198 | +<Callout> |
| 199 | + |
| 200 | +**Note:** Amplify doesn't capture the location automatically. Instead, you can add the location information in the default config when you [configure Analytics](/[platform]/build-a-backend/add-aws-services/analytics/set-up-analytics/#set-up-existing-analytics-backend) or while [updating the end point](/[platform]/build-a-backend/add-aws-services/analytics/existing-resources). |
| 201 | + |
| 202 | +</Callout> |
| 203 | +</InlineFilter> |
| 204 | + |
| 205 | +<InlineFilter filters={["swift", "android", "flutter"]}> |
| 206 | +The Amplify analytics plugin records when an application opens and closes. This session information can be viewed either from the AWS Console for Pinpoint. |
| 207 | + |
| 208 | +1. On the Pinpoint Console under **Analytics**, choose **Events**. |
| 209 | +2. Enable filters, you can select `Session Start` and `Session Stop` events to filter on session events. |
| 210 | +</InlineFilter> |
0 commit comments