Skip to content

Segment.io Integration

If you already use Segment inside your application and want to integrate Contextual data with your segment data, then please follow the below instructions to ensure the Segment ID is linked to the Contextual InstallID.

Info

Contextual will store Segment's "Identify" Traits only, please filter out other events such as "Track". Contextual supports Traits of the following types: String, Numeric, Datetime (a string in ISO-8601 format), These types map directly to Contextual's Tags. Contextual will ignore structures.

IOS

Swift: 

After Segment initialisation, during registration simply pass segment anonymousId into Contextual register function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 let segmentId = Analytics.shared().getAnonymousId()
    Contextual.sharedInstance().registerInstall(forApp: <appkey>, segmentId: segmentId, withDebugMode: false) {
            Contextual.sharedInstance().tagUserId(segmentId)
            Analytics.shared().identify("", traits: [
                "sh_cuid": segmentId,
                "email": "myuser@acme.com",
                "gender": "male",
                "phone": "+14088123123"
            ])
    }

If you didn't link segment during Contextual registration and would like to do it at any other place in your application

1
2
3
4
5
6
7
8
  let segmentId = Analytics.shared().getAnonymousId()
  Contextual.sharedInstance().tagUserId(segmentId)
  Analytics.shared().identify("", traits: [
                "sh_cuid": segmentId,
                "email": "myuser@acme.com",
                "gender": "male",
                "phone": "+14088123123"
    ])

Objc: 

After Segment initialisation, during registration simply pass segment anonymousId into Contextual register function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
NSString *segmentId = [[SEGAnalytics sharedAnalytics] getAnonymousId];
 [CONTEXTUAL registerInstallForApp:<appkey> withDebugMode:YES   segmentId:segmentId completion:^{
        [CONTEXTUAL tagUserId:segmentId];
        [[SEGAnalytics sharedAnalytics] identify:@""
                                        traits:@{
                                            @"sh_cuid": segmentId,
                                            @"email": "myuser@acme.com",
                                            @"gender": "male",
                                            @"phone": "+14088123123"
                                        }];

    }];

If you didn't link segment during Contextual registration and would like to do it at any other place in your application

1
2
3
4
5
6
7
8
9
    NSString *segmentId = [[SEGAnalytics sharedAnalytics] getAnonymousId];
    [CONTEXTUAL tagUserId:segmentId];
    [[SEGAnalytics sharedAnalytics] identify:@""
                                              traits:@{
                                                  @"sh_cuid": segmentId,
                                                  @"email": @"afterregisteration@test.com",
                                                  @"firstName": @"Ganesh",
                                                  @"lastName": @"Faterpekar"
                                              }];

Android

1.After Segment initialisation, simply pass segment anonymousId into Contextual register function

1
Contextual.INSTANCE.init(getApplication, <appKey>,  "anonymousID");
Note to get your anonymousID you can do:
1
2
3
4
 com.segment.analytics.Analytics.with(MyActivity.this)
                .getAnalyticsContext()
                .traits()
                .anonymousId()

2.Contextual provides a ISHEventObserver to inform notification center about initialisation of SDK is done. Therefore, please register a notification as

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class MyActivity implements ISHEventObserver {
Contextual.INSTANCE.init(getApplication, "MyAppKey",  "anonymousID", this);

@Override
public void onInstallRegistered(String installId){
}

@Override
public void onInstallRegisterError(String errorMsg) {
}

3.Please call segment api such as sending traits in the callback function onInstallRegistered

1
2
3
4
5
@Override
public void onInstallRegistered(String installId){
    com.segment.analytics.Analytics.with(MainActivity.this).identify(
            new com.segment.analytics.Traits().putName("Michael Bolton"));
}

Segment Destination Webhook

1.Go to your Segment Dashboard

2.Please add the below webhook URL in your segment destinations

1
    https://api.contextu.al/v3/hooks/segment/?app_key="Your Contextual App Key"

3.Then add "Contextual Auth Token" to Header as below

Contextual Dashboard Settings

Info

To get the "Contextual Auth Token" and "Contextual App Key" go to the Contextual Dashboard Settings and choose from the API Tab

Contextual Dashboard Settings

You can find more auth token information at the following link https://docs.contextu.al/authentication/


Last update: 2024-01-12