iOS Swift Device Mode

Installing SDK

MoEngage can be integrated via Segment with Swift Package Manager only, as segment only supports installation via SPM.

Install using Swift Package Manager

To integrate use the following GitHub URL link and set the branch as master or version as 1.0.0 and above https://github.com/moengage/MoEngage-Segment-Swift

Setup Segment SDK

Now head to the AppDelegate, and setup the Segment SDK :

Swift
import Segment_MoEngage
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
   let sdkConfig = MoEngageSDKConfig(withAppID: "YOUR APP ID")
   MoEngageInitializer.initializeDefaultInstance(sdkConfig: sdkConfig)
...
}

Just under your Analytics-Swift library setup, call analytics.add(plugin: ...) to add an instance of the plugin to the Analytics timeline.

Swift
import Segment_MoEngage
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
   let analytics = Analytics(configuration: Configuration(writeKey: "")
                    .flushAt(3)
                    .trackApplicationLifecycleEvents(true))
analytics.add(plugin: AppsFlyerDestination())
...
}
warning

Data Redirection

Please make sure to set the Data Center/ Region for MoEngage SDK separately in your AppDelegate's application:didFinishLaunchingWithOptions method. Refer doc for more info.

Setup MoEngage

To setup MoEngage, do the following :

  1. Navigate to MoEngage Dashboard > Settings > General.
  2. Copy the AppID.
  3. Go to Segment dashboard, go to Destinations and select MoEngage Integration.
  4. Enable MoEngage Integration.
  5. Update the APP ID obtained in step 1.
  6. Save the changes.

Setup.png

Tracking User Attribute

User attributes are specific traits of a user, like email, username, mobile, gender, etc. identify let you tie a user to their actions and record traits about them. It includes a unique User ID and any optional traits you know about them.

Swift
Analytics.main.identify("a user's id", traits: @["email":"a user's email address"])

For more info refer to this link.

Tracking Events

Event tracking is used to track user behavior in an app. track lets you record the actions your users perform. Every action triggers i.e, “event”, which can also have associated attributes.

Swift
Analytics.main.track("Item Purchased", properties: @["item":"Sword of Heracles"])

That's all you need for tracking data. For more info refer to this link.

Reset User

The reset method clears the SDK’s internal stores for the current user. This is useful for apps where users can log in and out with different identities over time.

Swift
Analytics.main.reset()

For more info refer to this link.

Push Notifications

Push Notifications are a great way to keep your users engaged and informed about your app. You have the following options while implementing push notifications in your app:

MoEngage Push Implementation

This will be the preferred way of implementation since Segment SDK currently doesn't support the UserNotifications framework which was introduced by Apple in iOS10. Follow this link to implement Push Notification in your mobile app using MoEngage SDK: Push Notifications

warning

Important

Please note that AppDelegate Swizzling does not work reliably with Segment Integration because of the delay in initializing the SDK by Segment, therefore make sure to call the MoEngage SDK methods for all Push-related callbacks.


Segment Push Implementation

warning

Issue

With this approach, we have found that MoEngage SDK will be unable to process Notifications when the Application is launched with a notification click. This is because Segment SDK not providing the payload to MoEngage SDK in this particular scenario. Hence we would highly recommend you use the first approach, where you will have to directly call the MoEngage SDK methods in Push related callbacks.

1. Follow the directions to register for push using Segment SDK in this link.

2. In your application’s application:didReceiveRemoteNotification: method, add the following:

Swift
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

3. If you integrated the application:didReceiveRemoteNotification:fetchCompletionHandler: in your app, add the following to that method:

Swift
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

4. If you implemented handleActionWithIdentifier:forRemoteNotification: , add the following to that method:

Swift
Analytics.main.handleAction(identifier: identifier, userInfo: userInfo)

Install / Update Differentiation :

Since you might integrate us when your app is already on the App Store, we would need to know whether your app update would be an actual UPDATE or an INSTALL.
To differentiate between those, use one of the methods below:

Swift
//For new Install call following
MoEngageSDKAnalytics.sharedInstance.appStatus(.install)

//For an app update call following
MoEngageSDKAnalytics.sharedInstance.appStatus(.update)

For more info on this refer to the following link.

InApp Native

In-App Messaging is custom views that you can send to a segment of users to show custom messages or give new offers or take to some specific pages. Follow the link to know more about inApp Messaging and how to implement it in your application: InApp NATIV

info

Segment Docs

For more info on using Segment for iOS refer to Developer Docs provided by Segment.

Previous

Next

Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?