In my last article, I showed the current best way of integrating analytics into your Android app using Kotlin. The events are only half the story for engagement. You need to be able to segment your users so that you can send them appropriate signals to draw them back to your app. That means understanding your users — where they are, whether they have authenticated (and how), and what they are interested in.
Amazon Pinpoint and the AWS Mobile SDK for Android provides some demographic information about the device the user is using out of the box. You can target all iOS users separately from all Android users, target a specific phone manufacturer or Android version, or a particular app version. Everything else needs to be provided by changing the endpoint profile. The endpoint profile is a blob of information about the user. It is session specific and can be easily adjusted.
If you followed the last article, your project is already set up for analytics — you have the service set up and you have integrated the Amazon Pinpoint SDK into your app.
Adding authenticated user information
The most basic change we can do is to add user information into the endpoint profile. When a user authenticates, you want to start recording this fact. Firstly, there is a graph within the Analytics section of Amazon Pinpoint that directly deals with active users. This graph is driven by the user information you record in the endpoint profile. Secondly, we can segment users based on whether they are authenticated or not.
To update the profile, we get a copy of the current endpoint profile, copy relevant information into it, then update the endpoint profile with the new information. Finally, we record an event to send the updated endpoint profile:
To effect this change, I added two additional methods to my definition of an analytics service with the following implementation:
When I do authentication, I can use
recordSuccessfulLogin() to record this. The endpoint will get updated and the appropriate signal will be sent to Amazon Pinpoint. The
_userauth.sign_in is a special event in Amazon Pinpoint for recording a successful authentication. Adding an update to the endpoint profile will enable you to count active users:
The graphs themselves may be uninteresting. From a developer perspective, though, the daily active users and monthly active users graphs have upticks. That is caused by setting the user ID within the endpoint profile.
Adding category information
A common requirement is to record “interests”. If you have a news app, you may want to opt in to news, sports, breaking news, politics, or any other number of categories. You can use a custom attribute called “categories” to store these.
The way I like to do this is to store the categories in a shared preferences store. You can use AWS AppSync to store shared preferences in the cloud so that the preferences are shared among all devices, or just use the shared preferences built into Android, which is what I will do here.
First, add a new method definition into the
Now, let’s add to the implementation in
Whether we update the categories through a UI action or because we loaded the categories via shared preferences, we always record an event so that the endpoint profile is transmitted to the other side.
You can have up to 20 custom attributes, each of which can be a set of strings, single string, integer or double type.
Adding location information
The final thing you may want to do is to tag each event with some location information. Step 1 is to create a location service. I separate out the location service into it’s own class that is injected via dependency injection. First, add the appropriate permissions into the app:
You can use either coarse or fine grained location. For analytics, it’s generally good enough to be “close”, but you can use either level of granularity. The location service looks like this:
Reality check: This is the wrong way to do this, but is illustrative of the technique. In a real application, you would be checking permissions, asking for permission to access location, recording the preference, and doing appropriate changes to the analytics.
Now, let’s take a look at the
AWSAnalyticsService class updates:
endpoint.location object allows you to also set the city, country, postalCode and region — all can be specified as strings. You can use the
Geocoder methods within the Google Maps API for this if needed. You can also use an alternate location provider, such as the currently recommended
Why would we want to do all this work? As I mentioned at the beginning of the article, we want to be able to engage our users by sending them messages that may be relevant to them. Part of this is user segmentation.
Let’s go back to the Amazon Pinpoint console:
Now, go to Segments > New Segment to create a segment. Start building your segment by selecting Custom Attributes, then categories, then one of your categories:
Your standard attributes are the phone type, manufacturer, app version, and so on. The custom attributes are the ones that you programmatically enable via the endpoint profile.
Now that you have a segment, use Settings to enable a channel, then create a campaign with the segment you have defined. The SMS and Email channels are relatively easily configured. Push notifications requires more set up (and I’ll be covering that in a future article).