Xamarin - ios

Xamarin ios SDK

Before starting your application instrumentation, ensure that you have created an app and have an appKey

Install

Download Pyze-Xamarin-iOS.dll from here

Add Pyze iOS Binding in your Xamarin project

  • Add the Pyze_Xamarin_iOS.dll under .iOS -> References
  • Right click on References -> Edit References -> .Net Assembly -> Browse the dll -> Ok

Initialize

After importing Pyze Xamarin Binding, you must initialize the Pyze library.

Inside the AppDelegate of the application override the WillFinishLaunching lifecycle callback of the application and call Pyze.Initialize.

public override bool WillFinishLaunching(UIApplication application, NSDictionary launchOptions)
{
            Pyze.Initialize("Your Pyze App Key");
            return true;
}

Alternatively you can pass the log throttling setting as the second parameter.

Ex:
Pyze.Initialize("Your Pyze App Key", PyzeLogLevel.All);

Custom Events

Custom events allow you to easily track unique user actions within your app.

Events

PyzeCustomEvents.PostWithEventName("Blog Read");

This allows you to track when a user executed an action, for a simple count of how often that event is occurring.

Events with Attribtues

Attributes are any key-value pair that you can attach to an event. You can have up to 99 attributes with any event. It is highly recommended to follow our Best Practices and heavily rely on attributes to make your instrumentation as minimal and useful as possible. In a typical application, you should have many more attributes than events.

var keys = new object [] { "screen", "seconds spent" };
var values = new object [] { "Home", "50" }:

PyzeCustomEvents.PostWithEventName("Blog Read", attributes);

Profile

Profiles are a powerful way to add data about your users to Pyze. Use profiles to add data that is not specific to a point in time (events) and are attached to a specific user.

When a user logs in to your app, call setUserProfile to identify them to Pyze. Any events that happen after they are identified, will be attributed to this user. You should only call this method once upon the initial login of a session.

You can optionally include profile attributes.

Pyze built-in profile attributes get special handling in the pyze UI, and we will attempt to discern the data type for custom attributes based on the data recieved. There is no difference between the built-in attributes and custom ones outside of formatting applied.

var pyzeProfileKeys = new object [] { "email_id", "date_of_birth" };
var pyzeProfileValues = new object [] { "xyz@abc.com", "1984-06-01" };
var pyzeProfileAttributes = NSDictionary.FromObjectsAndKeys (pyzeProfileValues, pyzeProfileKeys);

var customProfileKeys = new object [] { "age", "pincode" };
var customProfileValues = new object [] { "29", "23200" };
var customProfileAttributes = NSDictionary.FromObjectsAndKeys (customProfileValues, customProfileKeys);

PyzeIdentity.setUserProfile(userId,pyzeProfileAttributes,customProfileAttributes);

Pyze profile attributes

Pyze User Profile Field Name Data Type/Description
background (string) User background, biography or historical data
country (string) Country codes must be sent in the ISO-3166-1 alpha-2 standard.
current_location (object) Format: {“longitude”: -33.991894, “latitude”: 25.243732}
date_of_first_use (date at which the user first used the app) String in ISO 8601 format or in yyyy-MM-dd’T’HH:mm:ss.SSSZ format.
date_of_last_use (date at which the user last used the app) String in ISO 8601 format or in yyyy-MM-dd’T’HH:mm:ss.SSSZ format.
date_of_birth (date of birth) String in format “YYYY-MM-DD”, example: 1984-06-01.
email_id (string) Email Id
email_subscribe (string) Acceptable values are “opt_in” (explicit approval to receive email messages), “opt_out” (explicit denial to email messages), and “subscribed” (neither opted in nor out).
email_hard_bounced Automatically updated when a hard bounce occurs (true or false)
email_spam_reported Automatically updated when a user marks your email as spam, via the ISP (true or false)
facebook_id facebook ID
first_name (string) User’s First name
gender (string) “M”, “F”, “O” (other), “N” (not applicable), “P” (prefer not to say) or “U” (unknown).
home_city (string) User’s Home City
image_url (string) URL of image to be associated with the user
language (string) Require language to be sent in the ISO-639-1 standard.
last_name (string) User’s Last Name
marked_email_as_spam_at (string) Date at which the user’s email was marked as spam. Must be in ISO 8601 format or in yyyy-MM-dd’T’HH:mm:ss.SSSZ format.
phone (string) Phone number
push_subscribe (string) Available values are “opted_in” (explicitly registered to receive push messages), “unsubscribed” (explicitly opted out of push messages), and “subscribed” (neither opted in nor out).
push_tokens Array of objects with app_id and token string. You may optionally provide a device_id for the device this token is associated with, e.g., [{“app_id”: App Identifier, “token”: “abcd”, “device_id”: “optional_field_value”}]. If a device_id is not provided, one will be randomly generated.
time_zone (string) Time Zone’s must be sent as per IANA Time Zone Database (e.g., “America/New_York” or “Eastern Time (US & Canada)”). Only valid values will be respected.
twitter_id Twitter ID

Updating an existing user Profile

To update user profile attributes which are already set, use the following api.

Note : Do not call this api before calling setUserProfile.

PyzeIdentity.updateUserProfile(userId,pyzeProfileAttributes,customProfileAttributes);

Logging out a user

Call this api when a user logs out. Event sent after this call will have no identity attached, and will be attributed to an anonymous user.

PyzeIdentity.resetUserProfile(); 

User Privacy

Pyze provides APIs to allow end-users to Opt out of Data Collection and also instruct the Pyze system to forget a user’s data.

setUserOptOut

Allows end-users to opt out from data collection. Opt-out can be toggled true or false.

Pyze.setUserOptOut(true) 

To resume user data collection set value to false

deleteUser

Allows end-users to opt out from data collection and delete the user in the Pyze system. We recommend you confirm this action as once a user is deleted, this cannot be undone.

Pyze.deleteUser(true) 

Push Notifications

IOS Push Notifications

Apple provides the Apple Push Notification Service (APNS) to allow app publishers to reach out to their users via push notifications.

1. Generate P12 Certificate

  • Go to Certificates, Identifiers & Profiles page of your apple developer account.
  • Select Identifiers and go to YOUR_APP_IDENTIFIER.
  • Under Capabilities go to Push Notifications and select Configure
  • Now under Development/Production SSL Certificate select Create Certificate
  • Upload the Certificate Signing Request as mentioned here
  • Download the certificate generated and add to the Keychain Access
  • Now select the certificate just imported and choose Export.
  • Enter the password and save the .p12 to the desired location.

2. Configure Pyze as your provider

In order to allow Pyze to send push notifications on your behalf to users of your app, please provide the iOS push certificate to Pyze.

  • Login to growth.pyze.com
  • Click Settings from the Navigation Pane
  • Select the app you want to provide keys for from the drop down list

  • Select Push Notifications on the left menu
  • Upload a Push notifications certificate in .p12 format, provide p12 password (the password you generated while creating the certificate), and specify the provisioning mode: Development or Production depending on the type of certificate you are using.

  • Select daily and weekly quota limits
  • Click Save

3. Enabling Push Notifications

You can either turn Remote Notifications on either from the Xamarin IDE or by editing the app’s property list (usually Info.plist) file depending on you preference.

Option 1: Using Xamarin IDE
  • In Xamarin IDE, open your plist file.
  • Open Background Modes, Enable Background Modes and Remote Notifications by clicking on the corresponding check marks.
Option 2: Editing app’s property list (usually Info.plist) file directly

Open your Info.plist file and add following lines

    <key>UIBackgroundModes</key>
    <array>
      <string>remote-notification</string>
    </array>

Enable your app for Push Notifications

Generate a build for iOS before proceeding.

Enable Remote Notifications

Open info.plist and enable background mode and check Remote notification option

Add the following snippet inside FinishedLaunching lifecycle callback of the App Delegate

// Override point for customization after application launch.
// If not required for your application you can safely delete this method
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
   var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
          UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());

     UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
     UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else {
       UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}

Override RegisteredForRemoteNotifications lifecycle callback of the App Delegate and add the following snippet

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
  // Get current device token
  var DeviceToken = deviceToken.Description;
  if (!string.IsNullOrWhiteSpace(DeviceToken))
  {
    DeviceToken = DeviceToken.Trim('<').Trim('>');
  }

  // Get previous device token
  //var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

  Pyze.SetRemoteNotificationDeviceToken(deviceToken);

  // Save new device token
  NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}

Override DidReceiveRemoteNotification lifecycle callback of the App Delegate and add the following snippet

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler)
{
  Pyze.ProcessReceivedRemoteNotification(userInfo);
}

In-App Notifications

In-app notifications allow app businesses to reach out to app users when they use your app.

In-App notifications are built-in to the pyze sdk and have no additional dependecies.businesses to reach out to users from manually from Dynamic Funnels and Intelligence Explorer, and automatically based on workflows and campaigns from Growth Automation.

In-App notifications can be sent from Campaigns

Call the following methed, wherever you want to check for new messages and pop-up a modal with the notification:

Pyze.ShowUnreadInAppNotificationUIWithCompletionHandler((obj) =>
{
});

Debugging and Logging

Please refer to the ios documentation for platform-specific information

API Reference

Full API reference available here

Last modified 2020-12-23