Front-End Web & Mobile

How to: Record User Sign-in on Android with Amazon Pinpoint

In a previous post, we introduced the AWS Mobile SDK v2.6.0 for Android and implemented user sign-in and sign-up with IdentityManager.  IdentityManager is a new capability in the AWS Mobile SDK that provides a native graphical interface for authentication.  We also showed how to add basic session analytics using Amazon Pinpoint.

We want to show how to track user sign-in information within Amazon Pinpoint.  Specifically, Amazon Pinpoint has a “Users” tab on the Analytics page.  You can fill in the user sign-in and daily active users graphs to better track your users.  This enables you to get more granular about your user usage.  We show how to find out when users actually use the app – not merely when they start the app.

The code for this is all in the mobile app.  In the LoginActivity class, initialize IdentityManager, and then use callbacks for when the user is signed in.  There are two things to do:

  1. Update the endpoint profile so that the user ID is specified.
  2. Send a sign-in custom event to record the sign-in.

These are done within the DefaultSignInResultHandler that is established within the onCreate() method:

        // Set up the callbacks to handle the authentication response
        identityManager.setUpToAuthenticate(this, new DefaultSignInResultHandler() {
            @Override
            public void onSuccess(Activity activity, IdentityProvider identityProvider) {
                // Update the Endpoint Profile to include user ID
                final EndpointProfile profile = pinpointManager.getTargetingClient().currentEndpoint();
                profile.getUser().setUserId(identityManager.getCachedUserID());
                pinpointManager.getTargetingClient().updateEndpointProfile(profile);

                // Record the user login
                final AnalyticsEvent event = mgr.createEvent("_userauth.sign_in");
                mgr.recordEvent(event);
                mgr.submitEvents();

                Toast.makeText(LoginActivity.this,
                        String.format("Logged in as %s", identityManager.getCachedUserID()),
                        Toast.LENGTH_LONG).show();
                // Go to the main activity
                final Intent intent = new Intent(activity, NoteListActivity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                activity.startActivity(intent);
                activity.finish();
            }

            @Override
            public boolean onCancel(Activity activity) {
                return false;
            }
        });

The first block updates the Amazon Pinpoint endpoint profile so that each event is tagged with the user ID:

                // Update the Endpoint Profile to include user ID
                final EndpointProfile profile = pinpointManager.getTargetingClient().currentEndpoint();
                profile.getUser().setUserId(identityManager.getCachedUserID());
                pinpointManager.getTargetingClient().updateEndpointProfile(profile);

The second block sens a specific _userauth.sign_in event to Amazon Pinpoint to record the sign-in:

                // Record the user login
                final AnalyticsEvent event = mgr.createEvent("_userauth.sign_in");
                mgr.recordEvent(event);
                mgr.submitEvents();

After these are implemented, go to the Amazon Pinpoint console.  Select your project, choose Analytics, and finally choose the Users tab.

The Sign-ins chart is driven by the _userauth.sign_in event.  If you have not included the update to EndpointProfile, then you will see a change in the Sign-ins chart, but no change in the Daily active users chart.  The daily active users value only increases if you send an event after you change EndpointProfile to include the user ID.

For more information

Amazon Pinpoint is an incredibly powerful mobile analytics and engagement solution.  For more information about the Amazon Pinpoint solution, see the Amazon Pinpoint Developer Guide.  You can also review the Android topic in the Amazon Pinpoint Developer Guide and the AWS Mobile SDK for Android Developer Guide.