December 2014

Volume 29 Number 12

Mobile Apps : Advanced Push Notifications and Mobile Analytics

Kevin Ashley

With the increasing complexity of new mobile apps, developers are interested in the next step in push notification services—push analytics, market segmentation, reporting and reach via push available across all major platforms. Microsoft recently acquired the Capptain platform, which provides all of these services and then some.

While Capptain is not yet a part of Microsoft Azure, it’s already available for developers at capptain.com. Azure provides a reliable set of services for push notifications for developers on Windows 8, Android and iOS platforms.This article covers some advanced topics in push notifications for mobile developers.  

Capptain provides rich analytics that can tell you most of what you need to know about your app, including on which devices it’s been used and usage trends. It can define segments, create marketing campaigns and monetize your apps with push. It’s powerful technology, and it can start working for you with just a few initial steps.

Being a mobile developer myself, I quickly realized the benefits of using Capptain in conjunction with Azure Mobile Services. My Active Fitness app (activefitness.co) has a sizeable user base. This study provided me with an opportunity to experiment with Capptain and use it in the production version of my app.

Capptain Concepts

In this article, I’ll be using the following main Capptain concepts: activities, jobs, application information (appinfo) and extra data (extras). Figure 1 lists most of the Capptain concepts and terms and should be a good starting point to help you understand how Capptain works.

Figure 1 Capptain Terms and Concepts

Device Each device gets a unique identifier. If you have several apps on the same device, the device identifier (deviceid) will be the same. (On Windows Phone, device ID is unique per device and publisher).
User Capptain implicitly assigns one user to one device, so devices and users are equivalent concepts.
Session A session is one use of the application performed by a user. Sessions are automatically computed from a sequence of activities performed by a user. There’s no need to start/stop a session. Instead, you can start/stop activities. If no activity is reported, no session is reported.
Activity An activity is one use of a given portion of the application performed by one user (usually a screen, but it can be anything suitable to the application). A user can only perform one activity at a time. An activity has duration—from the moment it’s started to the moment it’s stopped.
Event An event is an instant action; unlike an activity, it doesn’t have duration.
Job A job is like an activity. You can start/stop it and it has duration. A job is intended for a background task, it might not have a UI.
Error An error is an issue correctly reported by the application.
Crash The Capptain SDK automatically reports a crash and an application failure.
Application Information (AppInfo) This is used to tag users (similar to cookies). For one given key, Capptain only keeps track of the latest value set (no history). Setting or changing the value of an appinfo forces Capptain to reevaluate audience criteria set on this appinfo (if any), meaning that appinfo can be used to trigger real-time pushes.
Extra Data (Extras) Extra data (or extras) is some arbitrary data you can attach to an event, error, activity and job. You can use this data to create ways to identify segments of your activities, jobs and so on.

Activity is a fundamental concept to Capptain. For example, an activity could be a page in your app the user is visiting. It could also be any logical activity that has duration. Job is another concept that also has duration, however, jobs are associated with background tasks, not necessarily connected with the UI.

Capptain provides an extensive set of APIs and native SDKs for all major platforms: iOS, Android and Windows 8 (see Figure 2).

Figure 2 Capptain Provides an Extensive Set of APIs

Analytics API The Analytics API is an HTTP API, which lets you retrieve analytics data (the one displayed on the Analytics tab of the Capptain Web site).
Monitor API The Monitor API is an XMPP API, which lets you retrieve real-time monitor data (the one displayed on the Monitor tab of the Capptain Web site).
Segments API The Segments API is an HTTP API, which lets you manage Capptain segments (everything under the Segments tab of the Capptain Web site).
Reach API The Reach API is an HTTP API, which lets you manage Reach campaigns without having to use the Capptain Web interface manually. The Reach API is a high-level API so you can leverage the Web interface of the Capptain Reach campaign manager.
Device API The Device API is an HTTP/REST API, which lets you retrieve and enrich the information gathered by the Capptain platform about all devices (and users) using your application.
Push API The Push API is an HTTP API, which lets you push custom data to devices running an application embedding the Capptain SDK.
SDK API The SDK API is an HTTP API, which lets you report logs as a native SDK would do, but using a simple HTTP API.
Account API The Account API is a set of HTTP APIs aimed at retrieving or updating account-related information.

Besides those APIs, Capptain provides several SDKs for all major platforms, as listed in Figure 3.

Figure 3 Capptain Provides SDKs to Support All Major Platforms

Android SDK Native Android SDK
iOS SDK Native iOS SDK
Web Web SDK
Windows Phone Windows Phone SDK
Windows 8 Windows Store SDK

Get Started with Capptain

First, go to capptain.com and create an account. After creating an account, set up an app of your choice—whether on Android, iOS, Windows Phone, Windows Store (Windows 8.x) or Web. Each app has an associated SDK, which I’ll show how to use after explaining some of the Capptain concepts. Capptain also provides a set of demo applications you’ll find at the bottom of your account page. You can also check what kind of analytics it provides, with data already in place.

Implement Activity Tracking

To start using Capptain, start with the right SDK for your platform. I placed an example on GitHub that shows how to use the SDK for Windows 8.1. SDKs on other platforms work similarly, and use the same basic concepts.

In the Package.appxmanifest, you need to ensure Internet capability is enabled. Go to the Declarations panel of your Package.appxmanifest file. In Available Declarations, select and add File Type Associations. In the right screen, in the Name field, type capptain_log_file and in the File type field type .set. Then, in Available Declarations select and add Cached File Updater.

Next, use NuGet to fetch the latest version of Capptain for Windows 8.1. In the application OnLaunched event, add the initialization code for Capptain. Copy your app ID and SDK key from the Capptain portal:

/* Capptain configuration. */
CapptainConfiguration capptainConfiguration = new CapptainConfiguration();
capptainConfiguration.Agent.ApplicationId = "YOUR_APPID";
capptainConfiguration.Agent.SDKKey = "YOUR_SDK_KEY";
/* Initialize Capptain angent with above configuration. */
CapptainAgent.Instance.Init(e, capptainConfiguration);

For my app, I wanted Capptain to track pages and how much time the user spends on each page. The following snippet, when placed in the page OnNavigatedTo method, will start tracking HubPage as a new activity in Capptain:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);
  CapptainAgent.Instance.StartActivity("HubPage");
}

Remember, in Capptain you can only have one activity at a time per user per app. You don’t need to call EndActivity. The new page calls StartActivity, and the old activity automatically ends. If you have many pages, you can simplify this approach by deriving from a special class called CapptainPage. This will automatically associate your pages with new activities.

Another way to track your activities with Capptain is by deriving your page from CapptainPage. The advantage to this method is you don’t need to StartActivity manually in the OnNavigatedTo handler. The CapptainPage will handle it for you. To do it this way, simply insert the following code into your XAML:

<capptain:CapptainPage
  xmlns:capptain="using:Capptain.Agent">
  <!-- layout -->
  ...
</capptain:CapptainPage >

With this code, I called the StartActivity method with HubPage. When you use the CapptainPage, you don’t need to call Start­Activity. It will track the name of your page by default. You can always override GetCapptainPageName to report a different value.

// In the .xaml.cs file
protected override string GetCapptainPageName()
{
  /* your code */
  return "new name";
}

Use Reach with Push Notifications

Capptain is especially interesting for mobile apps because it lets you push notifications to user devices using Capptain Reach. You can set up campaigns and then monitor how they’re delivered. For the announcements you push, you can define campaign name, content (including images), define your audience, timing and so on.

Capptain Reach is a powerful feature, as it lets you drive advertising campaigns, promotions and other types of marketing campaigns directly to your users. The key to successful marketing is relevance. Thanks to the ability of Capptain to know exactly what page your user is on, or what activity your user is doing, each campaign can be more relevant and more effective.

For Windows Store apps, Capptain uses the Windows Notifications Service (WNS). You need to update your Package manifest and update the Capptain portal with the WNS key. There are two scenarios in which you can integrate these notifications into your app—via Overlay or WebView integration. After you’ve started a Capptain account, you can refer to the Capptain documentation for in-depth integration guidance on “How to Integrate Capptain Reach on Windows” at bit.ly/12b3bub and “Initialize the Capptain Reach SDK” at bit.ly/1w90J3M.

Overlay Integration

With overlay integration, you derive from the CapptainPageOverlay page. The notification will be delivered automatically, using resources included in the NuGet package in the Resources/Overlay directory. When a notification comes from Reach API, your app page will inject the announcement embedded in CapptainOverlayAnnouncement or CapptainOverlayNotification views. Each of these views effectively contains a WebView that displays the announcement. You can further customize these views if you want to implement your own presentation for the Reach notification.

To get started, include the following declaration in any XAML page in your project (I implemented this method in the ItemPage.xaml of the sample project included with this article):

xmlns:capptain="using:Capptain.Overlay"

In XAML, instead of <Page>, make your page derive from CapptainPageOverlay, as follows:

public sealed partial class ItemPage :
      CapptainPageOverlay

Capptain Reach will inject its notification views in the first grid it finds on your page. If you want a specific grid to receive the view, you can use the grid named CapptainGrid:

<Grid x:Name="CapptainGrid"></Grid>

WebView Integration

For WebView integration, you need to include WebViews named capptain_notification_content or capptain_announcement_content, depending on the type of content you need to receive.

Use the following code to insert the WebViews:

<capptain:CapptainPage
  xmlns:capptain="using:Capptain.Page">
  <Grid>
    <WebView x:Name="capptain_notification_content" 
      Visibility="Collapsed"
      ScriptNotify="scriptEvent" Height="64" 
      HorizontalAlignment="Right"
      VerticalAlignment="Top"/>
    <WebView x:Name="capptain_announcement_content" 
      Visibility="Collapsed"
      ScriptNotify="scriptEvent" HorizontalAlignment="Right"
      VerticalAlignment="Top"/>
    <!-- layout -->
  </Grid>
</capptain:CapptainPage>

Data Push Notifications

Besides receiving visual content in your app with the Overlay and WebView Integration methods, you can also receive data push notifications directly in your app. To do so, you need to implement two handlers. The best place to put them is the constructor of your App object, in App.cs, as shown in Figure 4.

Figure 4 Implementing Handlers in the Constructor of the App Object

CapptainReach.Instance.DataPushStringReceived += (body) =>
{
  Debug.WriteLine("String data push message received: " + body);
  return true;
};
CapptainReach.Instance.DataPushBase64Received 
  += (decodedBody, encodedBody) =>
{
  Debug.WriteLine("Base64 data push message received: " 
    + encodedBody);
  // Do something useful with decodedBody like updating an image view
  return true;
};
CapptainReach.Instance.PushMessageReceived 
  += (id, replyTo, payload) =>
{
  // Your code
};

Analyze Results

Capptain provides a rich view of your data. You can view technical information associated with your devices, such as manufacturer, OS version, firmware, screen resolution and SDK version. Now that your app sends activity information to Capptain, you can also track any technical details you like.

Wrapping Up

Push notifications are powerful mechanisms. Most platforms currently provide notification service mechanisms, such as WNS, ANS and Google Cloud Messaging. Azure also provides a scalable infrastructure called Notification Hubs. As mobile apps evolve, there’s a need in analytics, reach, advertising and coordinating marketing campaigns with push. Capptain provides this next-level push notifications analytics tier many mobile developers will need.


Kevin Ashley is an architect evangelist for Microsoft. He’s coauthor of “Professional Windows 8 Programming” (Wrox, 2012) and a developer of top apps and games. He often presents on technology at various events, industry shows and Webcasts. In his role, he works with startups and partners, advising on software design, business and technology strategy, architecture, and development. Follow his blog at kevinashley.com and on Twitter at twitter.com/kashleytwit.

Thanks to the following Microsoft technical experts for reviewing this article: Greg Oliver and Bruno Terkaly