Microsoft App Center Analytics

Integrating OneSignal into your Microsoft App Center Analytics

OneSignal and Microsoft have partnered up to provide an easy way to integrate push notifications and analytics into your mobile apps.

Leveraging OneSignal's NotificationReceived, NotificationOpened, and PermissionObserver methods, you can add Microsoft Analytics Custom Events to track and update Notification and Subscription events as they occur.

Before Custom Events can be tracked in Microsoft, please Enable App Center Analytics at runtime

Sending Custom events

Microsoft Analytics allows you to track up to twenty properties with your Custom Events to understand the interaction between your user and the notifications they click. This is valuable for tracking additional data sent within the notification (like a campaign_id), the OneSignal notification_id, any action buttons clicked, and/or any data within the OSNotification Object.

Tracking Mobile Notification Received and Clicked Events

Notification Received Event Examples

Received: Send an event to your analytics system from the SDKs NotificationReceived event handler when a notification is received. Keep in mind, this event only gets called if the app is open and in the foreground or background. It will not get called if the app has been swiped away.

class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
  @Override
  public void notificationReceived(OSNotification notification) {
    
    Map<String, String> properties = new HashMap<>();
    
    String notification_id = notification.payload.notificationID
    properties.put("notification_id", notification_id);
    
    JSONObject data = notification.payload.additionalData;
    String campaign_id;
		//Send extra data like a campaign_id
    if (data != null) {
      campaign_id = data.optString("campaign_id", null);
      if (campaign_id != null)
        properties.put("campaign_id", campaign_id);
      }
    }
    Analytics.trackEvent("Notification Received", properties);
  }
}
let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
  print("Received Notification: \(notification!.payload.notificationID)")
  MSAnalytics.trackEvent("Notification Received", withProperties: ["Notification_Id" : notification!.payload.notificationID])
}
^(OSNotification *notification) {
	NSLog(@"Received Notification - %@ - %@", notification.payload.notificationID, notification.payload.title);
  MSAnalytics.trackEvent("Notification Received", withProperties: ["Notification_Id" : notification.payload.notificationID])
}

Notification Clicked Event Examples

Clicked/Opened: Send another event to your analytics system from the NotificationOpened or Action event handler when a notification is clicked.

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
  @Override
  public void notificationOpened(OSNotificationOpenResult result) {

    Map<String, String> properties = new HashMap<>();
    
    String notification_id = result.notification.payload.notificationID
    properties.put("notification_id", notification_id);

    JSONObject data = result.notification.payload.additionalData;
    String campaign_id;
    
    //Send extra data like a campaign_id
    if (data != null) {
      campaign_id = data.optString("campaign_id", null);
      if (campaign_id != null)
        properties.put("campaign_id", campaign_id);
      }
    }
    Analytics.trackEvent("Notification Clicked", properties);
  }
}
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
  let payload: OSNotificationPayload = result!.notification.payload

  if payload.additionalData != nil {
     let additionalData = payload.additionalData
     if additionalData?["campaign_id"] != nil {
        MSAnalytics.trackEvent("Notification Clicked", withProperties: ["Notification_Id" : payload.notificationID, "campaign_id" : additionalData!["campaign_id"]])
     } else {
       MSAnalytics.trackEvent("Notification Clicked", withProperties: ["Notification_Id" : payload.notificationID])
     }
  }
}
^(OSNotificationOpenedResult *result) {
   OSNotificationPayload* payload = result.notification.payload;
   MSAnalytics.trackEvent("Notification Clicked", withProperties: ["Notification_Id" : payload.notificationID])
}

More Examples of Event Handlers for other SDKs in our Mobile SDK Reference.


Tracking Mobile Subscription Events

Send a subscription event to your analytics system from the SDKs PermissionObserver event handler when a user subscribes.

public class MainActivity extends Activity implements OSPermissionObserver {
  protected void onCreate(Bundle savedInstanceState) {
    OneSignal.addPermissionObserver(this);
  }
  
  public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {
    if (stateChanges.getFrom().getEnabled() &&
        !stateChanges.getTo().getEnabled()) {
          Analytics.trackEvent("Unsubscribed from push");
      } else {
          Analytics.trackEvent("Subscribed from push");
    }   
  }
}

//Make sure to hold a reference to your observable at the class level, otherwise it my not fire.
func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
  if stateChanges.from.status == OSNotificationPermission.notDetermined {
     if stateChanges.to.status == OSNotificationPermission.authorized {
        MSAnalytics.trackEvent("User Subscribed")
     } else if stateChanges.to.status == OSNotificationPermission.denied {
        MSAnalytics.trackEvent("User Unsubscribed")
     }
  }
  // prints out all properties
  print("PermissionStateChanges: \n\(stateChanges)")
}

// Output:
/*
Thanks for accepting notifications!
PermissionStateChanges:
Optional(<OSSubscriptionStateChanges:
from: <OSPermissionState: hasPrompted: 0, status: NotDetermined>,
to:   <OSPermissionState: hasPrompted: 1, status: Authorized>
>
*/
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSPermissionObserver>
@end

@implementation AppDelegate
  
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Add your AppDelegate as an obsserver
  [OneSignal addPermissionObserver:self];
}

- (void)onOSPermissionChanged:(OSPermissionStateChanges*)stateChanges {
  
    // Example of detecting anwsering the permission prompt
    if (stateChanges.from.status == OSNotificationPermissionNotDetermined) {
      if (stateChanges.to.status == OSNotificationPermissionAuthorized)
         MSAnalytics.trackEvent("User Subscribed")
      else if (stateChanges.to.status == OSNotificationPermissionDenied)
         MSAnalytics.trackEvent("User Unsubscribed")
    }
}

More Examples of Event Handlers for other SDKs in our Mobile SDK Reference.


Azure pipelines NotificationServiceExtension setup

Seems like this is a common issue with this: https://developercommunity.visualstudio.com/t/build-ios-app-with-app-extensions/383129

The comment by Shakaib Arif seems to be the most promising: https://developercommunity.visualstudio.com/t/build-ios-app-with-app-extensions/383129#T-N512054-N1325335