Skip to content

Commit 82e0d20

Browse files
MoEngage SDK documentation update for analytics-kotlin and analytics-swift (#4328)
* MoEngage SDK documentation update for supporting analytics-kotlin and analytics-swift * Update src/connections/destinations/catalog/moengage/index.md Review comments addressed. Co-authored-by: stayseesong <[email protected]> --------- Co-authored-by: stayseesong <[email protected]>
1 parent a0d83e9 commit 82e0d20

File tree

1 file changed

+96
-93
lines changed
  • src/connections/destinations/catalog/moengage

1 file changed

+96
-93
lines changed

src/connections/destinations/catalog/moengage/index.md

Lines changed: 96 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The MoEngage Destination source code is open-sourced and freely available on Git
1717

1818
Connection Mode | Maintained by | GitHub Link
1919
---------|----------|---------
20-
iOS | MoEngage | [MoEngage-Segment-iOS](https://github.com/moengage/MoEngage-Segment-iOS){:target="_blank"}
20+
iOS | MoEngage | [MoEngage-Segment-Swift](https://github.com/moengage/MoEngage-Segment-Swift){:target="_blank"}
2121
Android | MoEngage | [moengage-segment-integration](https://github.com/moengage/moengage-segment-integration){:target="_blank"}
2222
Web | Segment | [analytics.js-integrations](https://github.com/segmentio/analytics.js-integrations/tree/master/integrations/moengage){:target="_blank"}
2323

@@ -79,56 +79,49 @@ analytics.reset();
7979
## iOS
8080

8181

82-
To get started with MoEngage on iOS, first integrate your app with the [MoEngage-Segment-iOS](https://github.com/moengage/MoEngage-Segment-iOS){:target="_blank"} library. You can integrate MoEngage and Segment with [CocoaPods](http://cocoapods.org){:target="_blank"} or with Swift Package Manager.
83-
84-
* Initialize pod with pod init command, this will create a podfile for your project.
85-
* Update your podfile by adding pod '**Segment-MoEngage**' as shown below:
86-
87-
```ruby
88-
use_frameworks!
89-
pod 'Segment-MoEngage'
90-
```
91-
92-
* Update the pod.
93-
94-
pod update
82+
To get started with MoEngage on iOS, first integrate your app with the [MoEngage-Segment-Swift](https://github.com/moengage/MoEngage-Segment-Swift){:target="_blank"} library. You can integrate MoEngage and Segment with Swift Package Manager.
9583

84+
> info ""
85+
> **Note:** This document covers the integration with [analytics-swift](https://github.com/segmentio/analytics-swift){:target="_blank"}, if you have integrated with [analytics-ios](https://github.com/segmentio/analytics-ios){:target="_blank"} refer [this documentation](https://partners.moengage.com/hc/en-us/articles/4409143473172-iOS-device-mode-){:target="_blank"} for details on how to integrate.
9686
97-
To install with SPM use the [MoEngage-Segment-iOS](https://github.com/moengage/MoEngage-Segment-iOS.git){:target="_blank"} library and set the branch as master or version as 7.0.0 and above.
87+
To install with SPM use the [MoEngage-Segment-Swift](https://github.com/moengage/MoEngage-Segment-Swift.git){:target="_blank"} library and set the branch as master or version as 1.0.0 and above.
9888

9989
### Configure the Segment SDK:
10090

101-
Navigate to the App Delegate file, and setup the Segment SDK:
102-
1. Import `SEGMoEngageIntegrationFactory.h` and `SEGMoEngageInitializer.h`.
103-
2. Initialize `MOSDKConfig` object and call `initializeDefaultInstance:` method of `SEGMoEngageInitializer`.
104-
3. Initialize `SEGMoEngageIntegrationFactory` instance to the `SEGAnalyticsConfiguration` as shown below:
105-
106-
```objc
107-
#import <SEGMoEngageIntegrationFactory.h>
108-
#import <SEGMoEngageInitializer.h>
109-
#import <SEGAnalytics.h>
110-
111-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
112-
113-
//Initialize SDKConfig object and call initializeDefaultInstance: method of SEGMoEngageInitializer
114-
MOSDKConfig* sdkConfig = [[MOSDKConfig alloc] initWithAppID:@"YOUR APP ID"];
115-
[SEGMoEngageInitializer initializeDefaultInstance:sdkConfig];
91+
Now head to the App Delegate file, and setup the Segment SDK by
92+
1. Importing `Segment`, `Segment_MoEngage` and `MoEngageSDK`.
93+
2. Initialize `MoEngageSDKConfig` object and call `initializeDefaultInstance` method of `MoEngageInitializer`.
94+
3. Initialize `MoEngageDestination` as shown below:
11695

117-
// Add your configuration key from Segment
118-
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"configuration key"];
96+
Under your Analytics-Swift library setup, add the MoEngage plugin using the `analytics.add(plugin: ...)` method. The MoEngage dashboard now tracks all of your events.
11997

120-
// Add MoEngageIntegrationFactory. Without this data will not come to MoEngage.
121-
[config use:[SEGMoEngageIntegrationFactory instance]];
122-
[SEGAnalytics setupWithConfiguration:config];
123-
}
124-
```
98+
```
99+
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
100+
.flushAt(3)
101+
.trackApplicationLifecycleEvents(true))
102+
analytics.add(plugin: MoEngageDestination())
103+
```
104+
### Configure the MoEngage SDK:
105+
106+
```swift
107+
import Segment_MoEngage
108+
import MoEngageSDK
109+
...
110+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
111+
...
112+
113+
let sdkConfig = MoEngageSDKConfig(withAppID: "YOUR APP ID")
114+
MoEngageInitializer.shared.initializeDefaultInstance(sdkConfig: sdkConfig)
115+
...
116+
}
117+
```
125118

126119
### Tracking User Attribute
127120

128121
[User attributes](https://developers.moengage.com/hc/en-us/articles/4403905883796-Tracking-user-attributes){:target="_blank"} are specific traits of a user, like email, username, mobile, gender etc. **identify** lets 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.
129122

130-
```objc
131-
[[SEGAnalytics sharedAnalytics] identify:@"a user's id" traits:@{ @"email": @"a user's email address" }];
123+
```swift
124+
Analytics.main.identify("a user's id", traits: @["email":"a user's email address"])
132125
```
133126

134127
Read more about [identify calls](/docs/connections/sources/catalog/libraries/mobile/ios/#identify).
@@ -138,8 +131,8 @@ Read more about [identify calls](/docs/connections/sources/catalog/libraries/mob
138131

139132
Segment uses event tracking to track user behavior in an app. `track` calls let you record the actions your users perform. Every action triggers an "event", which can also have associated attributes.
140133

141-
```objc
142-
[[SEGAnalytics sharedAnalytics] track:@"Item Purchased" properties:@{ @"item": @"Sword of Heracles"}];
134+
```swift
135+
Analytics.main.track("Item Purchased", properties: @["item":"Sword of Heracles"])
143136
```
144137

145138
Read more about [track calls](/docs/connections/sources/catalog/libraries/mobile/ios/#track).
@@ -149,8 +142,8 @@ Read more about [track calls](/docs/connections/sources/catalog/libraries/mobile
149142

150143
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.
151144

152-
```objc
153-
[[SEGAnalytics sharedAnalytics] reset];
145+
```swift
146+
Analytics.main.reset()
154147
```
155148

156149
Read more about the [reset method](/docs/connections/sources/catalog/libraries/mobile/ios/#reset).
@@ -161,12 +154,12 @@ Read more about the [reset method](/docs/connections/sources/catalog/libraries/m
161154
Since your app might already be on the App Store, you must specify whether your app update would be an `UPDATE` or an `INSTALL`.
162155
To differentiate between those, use one of the method below:
163156

164-
```objc
157+
```swift
165158
//For new Install call following
166-
[[MoEngage sharedInstance] appStatus:AppStatusInstall];
159+
MoEngageSDKAnalytics.sharedInstance.appStatus(.install)
167160

168161
//For an app update call following
169-
[[MoEngage sharedInstance] appStatus:AppStatusUpdate];
162+
MoEngageSDKAnalytics.sharedInstance.appStatus(.update)
170163
```
171164

172165
Read more on [install/update differentiation](https://developers.moengage.com/hc/en-us/articles/4403910297620){:target="_blank"}.
@@ -184,21 +177,24 @@ Push Notifications are a great way to keep your users engaged and informed about
184177

185178
**Segment Push Implementation:**
186179

187-
1. Follow the directions to register for push notifications [with the Segment SDK](/docs/connections/sources/catalog/libraries/mobile/ios/#how-do-i-use-push-notifications).
180+
1. In your application's application:didRegisterForRemoteNotificationsWithDeviceToken: method, add the following:
181+
```swift
182+
Analytics.main.registeredForRemoteNotifications(deviceToken: deviceToken)
183+
```
188184

189185
2. In your application's application:didReceiveRemoteNotification: method, add the following:
190-
```objc
191-
[[SEGAnalytics sharedAnalytics] receivedRemoteNotification:userInfo];
186+
```swift
187+
Analytics.main.receivedRemoteNotification(userInfo: userInfo)
192188
```
193189

194190
3. If you integrated the application:didReceiveRemoteNotification:fetchCompletionHandler: in your app, add the following to that method:
195-
```objc
196-
[[SEGAnalytics sharedAnalytics] receivedRemoteNotification:userInfo];
191+
```swift
192+
Analytics.main.receivedRemoteNotification(userInfo: userInfo)
197193
```
198194

199195
4. If you implemented handleActionWithIdentifier:forRemoteNotification:, add the following to that method:
200-
```objc
201-
[[SEGAnalytics sharedAnalytics] handleActionWithIdentifier:identifier forRemoteNotification:userInfo];
196+
```swift
197+
Analytics.main.handleAction(identifier: identifier, userInfo: userInfo)
202198
```
203199

204200
**MoEngage Push Implementation:**
@@ -224,16 +220,19 @@ For more info on using **Segment for iOS** refer to [**Developer Docs**](/docs/c
224220

225221
To use MoEngage in an Android app, you must perform the following steps to set up your environment.
226222

227-
![MavenBadge](https://maven-badges.herokuapp.com/maven-central/com.moengage/moengage-segment-integration/badge.svg)
223+
![MavenBadge](https://maven-badges.herokuapp.com/maven-central/com.moengage/moengage-segment-kotlin-destination/badge.svg)
228224

229225
To enable the full functionality of MoEngage (like Push Notifications, InApp Messaging), complete the following steps in your Android app.
230226

227+
> info ""
228+
> **Note:** This document covers the integration with [analytics-kotlin](https://github.com/segmentio/analytics-kotlin){:target="_blank"}, if you have integrated with [analytics-android](https://github.com/segmentio/analytics-android){:target="_blank"} refer [this documentation](https://partners.moengage.com/hc/en-us/articles/4409143473172-iOS-device-mode-){:target="_blank"} for details on how to integrate.
229+
231230
### Adding the MoEngage Dependency
232231

233232
Along with the Segment dependency, add the below dependency in your `build.gradle` file.
234233

235234
```groovy
236-
implementation("com.moengage:moengage-segment-integration:$sdkVersion") {
235+
implementation("com.moengage:moengage-segment-kotlin-destination:$sdkVersion") {
237236
transitive = true
238237
}
239238
```
@@ -254,16 +253,15 @@ Refer to the [SDK Configuration](https://developers.moengage.com/hc/en-us/articl
254253
After adding the dependency, you must register the integration with Segment SDK. To do this, import the MoEngage
255254
integration:
256255

257-
```java
258-
import com.segment.analytics.android.integrations.moengage.MoEngageIntegration;
256+
```kotlin
257+
import com.segment.analytics.kotlin.destinations.moengage.MoEngageDestination
259258
```
260259

261260
Add the following line:
262261

263-
```java
264-
Analytics analytics = new Analytics.Builder(this, "write_key")
265-
.use(MoEngageIntegration.FACTORY)
266-
.build();
262+
```kotlin
263+
Analytics("<YOUR WRITE KEY>", context)
264+
.add(MoEngageDestination(application))
267265
```
268266

269267

@@ -274,12 +272,12 @@ Copy the APP ID from the Settings Page `Dashboard --> Settings --> App --> Gener
274272
> info ""
275273
> **Note:** MoEngage recommend that you initialize the SDK on the main thread inside `onCreate()` and not create a worker thread and initialize the SDK on that thread.
276274
277-
```java
278-
// this is the instance of the application class and "XXXXXXXXXXX" is the APP ID from the dashboard.
279-
MoEngage moEngage = new MoEngage.Builder(this, "XXXXXXXXXXX")
280-
.enableSegmentIntegration()
281-
.build();
282-
MoEngage.initialiseDefaultInstance(moEngage);
275+
```kotlin
276+
// this is the instance of the application class and "YOUR_APP_ID" is the APP ID from the dashboard.
277+
val moEngage = MoEngage.Builder(this, "YOUR_APP_ID")
278+
.enablePartnerIntegration(IntegrationPartner.SEGMENT)
279+
.build()
280+
MoEngage.initialiseDefaultInstance(moEngage)
283281
```
284282
### Exclude MoEngage Storage File from Auto-Backup
285283
Auto backup service of Android periodically backs up the Shared Preference file, Database files, and so on.
@@ -298,14 +296,14 @@ This is required for migrations to the MoEngage Platform so the SDK can determin
298296

299297
If the user was already using your application and has just updated to a new version which has the MoEngage SDK, below is an example call:
300298

301-
```java
302-
MoEAnalyticsHelper.INSTANCE.setAppStatus(context, AppStatus.UPDATE);
299+
```kotlin
300+
MoEAnalyticsHelper.setAppStatus(context, AppStatus.UPDATE)
303301
```
304302

305303
If this is a fresh install:
306304

307-
```java
308-
MoEAnalyticsHelper.INSTANCE.setAppStatus(context, AppStatus.INSTALL);
305+
```kotlin
306+
MoEAnalyticsHelper.setAppStatus(context, AppStatus.INSTALL)
309307
```
310308

311309
## MoEngage Android SDK Features
@@ -325,13 +323,15 @@ Refer to the [MoEngage - NotificationConfig](https://moengage.github.io/android-
325323
Use the `configureNotificationMetaData()` to pass on the configuration to the SDK.
326324

327325

328-
```java
329-
MoEngage moEngage =
330-
new MoEngage.Builder(this, "XXXXXXXXXX")
331-
.configureNotificationMetaData(new NotificationConfig(R.drawable.small_icon, R.drawable.large_icon))
332-
.enablePartnerIntegration(IntegrationPartner.SEGMENT)
333-
.build();
334-
MoEngage.initialiseDefaultInstance(moEngage);
326+
```kotlin
327+
val moEngage = MoEngage.Builder(this, "YOUR_APP_ID")
328+
.enablePartnerIntegration(IntegrationPartner.SEGMENT)
329+
.configureNotificationMetaData(NotificationConfig(
330+
smallIcon = R.drawable.small_icon,
331+
largeIcon = R.drawable.large_icon
332+
))
333+
.build()
334+
MoEngage.initialiseDefaultInstance(moEngage)
335335
```
336336

337337
#### Configuring Firebase Cloud Messaging
@@ -347,22 +347,25 @@ For showing Push notifications there are 2 important steps:
347347

348348
To opt-out of MoEngage token registration mechanism disable token registration while configuring FCM in the `MoEngage.Builder` as shown below
349349

350-
```java
351-
MoEngage moEngage = new MoEngage.Builder(this, "XXXXXXXXXX")
352-
.configureNotificationMetaData(new NotificationConfig(R.drawable.small_icon, R.drawable.large_icon))
353-
.configureFcm(FcmConfig(false))
354-
.enablePartnerIntegration(IntegrationPartner.SEGMENT)
355-
.build();
356-
MoEngage.initialiseDefaultInstance(moEngage);
350+
```kotlin
351+
val moEngage = MoEngage.Builder(this, "YOUR_APP_ID")
352+
.enablePartnerIntegration(IntegrationPartner.SEGMENT)
353+
.configureNotificationMetaData(NotificationConfig(
354+
smallIcon = R.drawable.small_icon,
355+
largeIcon = R.drawable.large_icon
356+
))
357+
.configureFcm(FcmConfig(false))
358+
.build()
359+
MoEngage.initialiseDefaultInstance(moEngage)
357360
```
358361

359362
###### Pass the push token to the MoEngage SDK
360363

361364
The Application must pass the Push Token received from FCM to the MoEngage SDK for the MoEngage platform to send out push notifications to the device.
362365
Use the below API to pass the push token to the MoEngage SDK.
363366

364-
```java
365-
MoEFireBaseHelper.getInstance().passPushToken(getApplicationContext(), token);
367+
```kotlin
368+
MoEFireBaseHelper.getInstance().passPushToken(applicationContext,token)
366369
```
367370

368371
Please make sure token is passed to MoEngage SDK whenever push token is refreshed and on application update. Passing token on application update is important for migration to the MoEngage Platform.
@@ -372,12 +375,12 @@ Please make sure token is passed to MoEngage SDK whenever push token is refreshe
372375
To pass the push payload to the MoEngage SDK call the MoEngage API from the `onMessageReceived()` from the Firebase receiver.
373376
Before passing the payload to the MoEngage SDK you should check if the payload is from the MoEngage platform using the helper API provided by the SDK.
374377

375-
```java
376-
if (MoEPushHelper.getInstance().isFromMoEngagePlatform(remoteMessage.getData())) {
377-
MoEFireBaseHelper.getInstance().passPushPayload(getApplicationContext(), remoteMessage.getData());
378-
} else {
379-
// your app's business logic to show notification
380-
}
378+
```kotlin
379+
if (MoEPushHelper.getInstance().isFromMoEngagePlatform(remoteMessage.data)) {
380+
MoEFireBaseHelper.getInstance().passPushPayload(applicationContext, remoteMessage.data)
381+
} else {
382+
// your app's business logic to show notification
383+
}
381384
```
382385

383386
##### Push Registration and Receiving handled by SDK

0 commit comments

Comments
 (0)