Segment SDK integration

Here's how to integrate Segment with Leanplum for your Android and iOS mobile apps.

To setup Push notifications, In-App Messages, Variables, please follow the Leanplum Developer docs for those features.

Android

To install the Leanplum SDK through Segment, make sure to follow the setup instructions in Segment's documentation.

In addition to the standard Segment integration dependencies, the following also needs to be added in order to get the Leanplum SDK:

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    implementation ‘com.segment.analytics.android:analytics:4.9.0’
    implementation ‘com.leanplum.segment:LeanplumIntegration:1.1.2’
}

Usage

This is an example of how to use Leanplum once is integrated through Segment.

Make sure to place the initialization part in your project Application class:

private static final String SEGMENT_WRITE_KEY = " ... ";

Analytics analytics = new Analytics
  .Builder(getApplicationContext(), SEGMENT_WRITE_KEY)
  .use(LeanplumIntegration.FACTORY)
  .build();

Now you can use Segment as you are used to, e.g.:

analytics.track(" ... ", ... );

🚧

Do not explicitly call Leanplum.start, as it is called within the LeanplumIntegration.

In addition to that you can also use the advanced features of Leanplum. Once the Leanplum SDK is successfully registered, Segment executes a callback:

analytics.onIntegrationReady(LeanplumIntegration.LEANPLUM_SEGMENT_KEY,
    new Analytics.Callback() {
      @Override
      public void onReady(Object instance) {
        Leanplum.addVariablesChangedHandler( ... );
      }
    });

If you would like more information about the integration, please refer to Leanplum-Segment-Android Github.

iOS

To set up the segment-integrated Leanplum SDK, see the setup instructions in Segment's documentation, then follow the steps below.

Using CocoaPods as the dependency manager:

🚧

Note:

If you already have CocoaPods installed and have a podfile, please skip to step 3.

  1. Install CocoaPods by running the following command:
$ sudo gem install cocoapods 

For issues with installing CocoaPods, please refer here.

  1. Add a Podfile. In terminal, navigate to your app's directory. Add a podfile to your app by running the following command:
$ pod init 
  1. Open your podfile by running the following command:
$ open -a Xcode Podfile 
  1. Insert the following line of code into your Podfile:
pod 'LeanplumSegment', '~> 1.1.4' 
  1. Now, install the SDK by running the following command:
$ pod install 

Usage

Import the LeanplumSegment integration:

#import <LeanplumSegment/SEGLeanplumIntegrationFactory.h> 

Add the following lines into your AppDelegate:

NSString *const SEGMENT_WRITE_KEY = @" ... ";
SEGAnalyticsConfiguration *config =
    [SEGAnalyticsConfiguration configurationWithWriteKey:SEGMENT_WRITE_KEY];
[config use:[SEGLeanplumIntegrationFactory instance]];
[SEGAnalytics setupWithConfiguration:config];
let configuration = AnalyticsConfiguration(writeKey: " [YOUR_SEGMENT_WRITE_KEY] ";)
configuration.use(SegmentLeanplumIntegrationFactory())
Analytics.setup(with: configuration)

Make sure to place your Segment Write key within the code. This block of code also calls for Leanplum start.

Make some Track calls:

To use the integration, use the following line of code to properly track events within the App:

[[SEGAnalytics sharedAnalytics] track:@" ... "];

In addition to that you can also use the advanced features of Leanplum. Once the Leanplum SDK is successfully registered, Segment posts a NSNotification, hence register to it:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[...]
  [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(segmentIntegrationDidStart)
             name:SEGAnalyticsIntegrationDidStart
           object:LPLeanplumSegmentKey];
}

- (void)segmentIntegrationDidStart {
  [Leanplum onVariablesChanged:^{
      [...]
  }];
}

If you would like more information about the integration, as well as an example project, please refer Leanplum-Segment-iOS Github. As always, feel free to reach out to [email protected] with any other questions.