When it comes to retrieving Sitecore content for native mobile apps, the first thing that comes to mind is to use Sitecore Layout Service REST endpoint and Dictionary Service. And considering that the Layout Service rendering process goes through the Rendering Engine, personalization rules will be respected. However, there is a caveat, in the context of native mobile apps there is no cookie-based tracking available, therefore, the Sitecore Layout Service will not recognize a known contact or a returning anonymous contact and many personalization rules will not work in this case.
In this blog post I will describe an approach on how to enable contact-related personalization (e.g. based on contact behavior) when retrieving Sitecore content for native mobile apps. Also, I will provide details on what additional measures can be taken to achieve more powerful personalization on the native apps together with examples for Android, using Kotlin.
Let’s start with what services can be used to retrieve content from Sitecore for native mobile apps. There are two main options:
- Sitecore Layout Service REST endpoint / Dictionary Service / GraphQL
- Sitecore Item Service (Sitecore Service Client)
The Sitecore Service Client provides a REST API endpoint to access Sitecore items, but, unlike the Layout Service, it does not provide support for personalization and tracking. Taking this into account, in our case, the Sitecore Layout Service is the way to go.
How to enable personalization for a known contact when retrieving Sitecore content for native mobile apps?
Considering that cookie-based tracking is not available in the context of native mobile apps, the Layout Service and the Rendering Engine will not know what contact is it to be able to apply contact-related personalization.
In order to be able to use contact-related personalization for a known contact, first, a contact has to be identified and then the Sitecore analytics cookie has to be passed to the Layout Service endpoint with each request.
One of the approaches for how to accomplish this in the context of native mobile applications:
- Create an API endpoint that is aware of HttpContext and Sitecore, accepts a contact identifier (e.g. username/email address) and identifies the contact:
- A native mobile application will send a contact identifier to the endpoint by using an HTTP client and with the response
SC_ANALYTICS_GLOBAL_COOKIEcookie is returned.
- A mobile app will pass the analytics cookie with every request to the Layout Service endpoint. For Android, cookie management can be handled with these HTTP clients:
And for details on how to implement cookie management in iOS with Swift, you can see here.
Figure 1. Enabling contact-related personalization when retrieving Sitecore content for native mobile apps by creating and API for contact identification and applying cookie management mechanism.
Below you can see an example of how to handle this approach for a native mobile app for Android by using Kotlin and khttp HTTP client, though an HTTP client of your choice can be used together with a more sophisticated cookie management mechanism, as the current example is simplified.
First, we need to call our endpoint, pass a contact identifier (username) and get the Sitecore analytics cookie:
val queryStr = mapOf("identifier" to "username") val result = get("https://yourdomain/identify", params=queryStr) //accessing the response cookies result.cookies["SC_ANALYTICS_GLOBAL_COOKIE"] // "b0760a00d4cc4503a6169a1aa9cfb71a|True" - // a device GUID/ a tracker identifier
The API will identify the contact and
SC_ANALYTICS_GLOBAL_COOKIE cookie is returned as a response. It has to be passed to the Layout Service with each request in order for the Sitecore Rendering Engine to apply contact-related personalization successfully.
val queryStr = mapOf("item" to "[path]", "sc_lang" to "[language]", "sc_apikey" to "[key]", "tracking" to "[true]") val url = "http://yourdomain/sitecore/api/layout/render/[config]" val cookies = mapOf("SC_ANALYTICS_GLOBAL_COOKIE" to cookieValue) //sending the analytics cookie together with request to the Layout Service val result = get(url, cookies=cookies, params=queryStr)
If contact-related personalization (e.g. based on contact behavior) rules were applied, the Layout Service will return personalized content in
On of the ways of how to improve personalization is to enable tracking contact’s activity on native mobile apps, which was described in the previous blog posts. A recommended approach would be to leverage the Sitecore Universal Tracker and write interaction data from native mobile apps to Sitecore and later use it for penalization purposes.
Figure 2. Enabling contact-related personalization when retrieving Sitecore content for native mobile apps and improving it by writing interaction data to Sitecore with the Sitecore Universal Tracker.
Contact-related personalization (e.g. based on contact behavior) can be enabled on native mobile apps when retrieving Sitecore content by creating an API for contact identification and applying cookie management mechanism (Android: khttp / OkHttp by using CookieJar or an interceptor/ HttpURLConnection by using CookieManager). In addition to this, it is of great value to be able to write interaction data to Sitecore, which can positively impact personalization.
Related blog posts:
- Approaches for writing interaction data from native mobile apps to Sitecore with examples for Android (Kotlin);
- An alternative approach for sending interaction data from native mobile apps to Sitecore