Skip to content

Building Social Graph Relationships

Prerequisite

Overview

GetSocial SDK exposes all the methods needed to import existing connections between users or gradually add/remove connections in the graph. All methods can operate on GetSocial user IDs or user IDs from external identities like Facebook, or your identity provider.

Importing Friends

If your app already has a concept of friends or any kind of the relationship between users you can import this data into GetSocial Social Graph to leverage friend suggestions or advanced segmentation in Smart Audiences.

Important

If you import friends with setFriends(...) methods, it will overwrite existing connections. You have to ensure you call it only once. Use addFriendsByAuthIdentities(...) to add new user connections.

By GetSocial User IDs

```java tab=”Android(Java)” hl_lines=”3”
List getSocialUserIds = Arrays.asList(“user_id_1”, “user_id_2”);

GetSocial.User.setFriends(getSocialUserIds, new CompletionCallback() {
@Override
public void onSuccess() {
Log.i(“GetSocial”,”Friends data successfuly imported.”);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while importing friends: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”3”
val getSocialUserIds = listOf(“user_id_1”, “user_id_2”)

GetSocial.User.setFriends(getSocialUserIds, object: CompletionCallback {
override fun onSuccess() {
println(“Friends data successfuly imported.”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while importing friends, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”2”
NSArray* getSocialUserIds = @[@”1234”, @”4567”];
[GetSocialUser setFriendsWithIds:getSocialUserIds success:^() {
NSLog(@”Friends successfully replaced.”);
} failure:^(NSError *error) {
NSLog(@”Exception while replacing a friends: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="2"
let getSocialUserIds = ["1234", "4567"]
GetSocialUser.setFriendsWithIds(getSocialUserIds, success: {
    print("Friends successfully replaced.")
}, failure: { error in
    print("Exception while replacing a friends: \(error)")
})

```csharp tab=”Unity” hl_lines=”5”
List getSocialUserIds = new List();
getSocialUserIds.Add(“123”);
getSocialUserIds.Add(“456”);

GetSocial.User.SetFriends(getSocialUserIds,
() => {
Debug.Log (“Successfully replaced friends. “);
},
error => {
Debug.Log (“Exception while replacing friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="3"
const getSocialUserIds = ['123', '456'];

GetSocialUser.setFriends(getSocialUserIds).then(
    () => {
        console.log('Successfully replaced friends.');
    }, (error) => {
        console.log('Exception while replacing friend: ' + error);
});

By Auth Identities

An identity is some unique user data to identify them among other users. You can use any auth provider to create an identity (your custom login, Facebook, Twitter, Google, GitHub, etc). Each user may have any number of different identities, but only one of the same provider. It means that you may attach one Facebook, one Twitter and one custom identity, but you can not attach two Facebook identities. Find out more about identities.

```java tab=”Android(Java)” hl_lines=”4”
List providerUserIds = Arrays.asList(“external_user_id_1”, “external_user_id_2”);
String identityProviderId = “my_custom_identity_provider_id”;

GetSocial.User.setFriendsByAuthIdentities(identityProviderId, providerUserIds, new CompletionCallback() {
@Override
public void onSuccess() {
Log.i(“GetSocial”,”Friends data successfuly imported.”);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while importing friends: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”4”
val providerUserIds = listOf(“external_user_id_1”, “external_user_id_2”)
val identityProviderId = “my_custom_identity_provider_id”

GetSocial.User.setFriendsByAuthIdentities(identityProviderId, providerUserIds, object : CompletionCallback{
override fun onSuccess() {
println(“Friends data successfuly imported.”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while importing friends, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”3”
NSArray* providerUserIds = @[@”1234”, “456”];
NSString* identityProviderId = @”custom_auth_method”;
[GetSocialUser setFriendsWithIds:providerUserIds forProvider:identityProviderId success:^(void) {
NSLog(@”Friends successfully replaced.”);
} failure:^(NSError *error) {
NSLog(@”Exception while replacing friends: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="3"
let providerUserIds = ["1234", "456"]
let identityProviderId = "custom_auth_method"
GetSocialUser.setFriendsWithIds(providerUserIds, forProvider: identityProviderId, success: {
    print("Friends successfully replaced.")
}, failure: { error in
    print("Exception while replacing friends: \(error)")
})

```csharp tab=”Unity” hl_lines=”6”
List providerUserIds = new List();
providerUserIds.Add(“123”);
providerUserIds.Add(“456”);
string identityProviderId = “custom_auth_method”;

GetSocial.User.SetFriends(identityProviderId, providerUserIds,
() => {
Debug.Log (“Successfully replaced friends. “);
},
error => {
Debug.Log (“Exception while replacing friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="4"
const providerUserIds = ['123, '456'];
const identityProviderId = 'custom_auth_method';

GetSocialUser.setFriends(identityProviderId, providerUserIds).then(
    () => {
        console.log('Successfully replaced friends.');
    }, (error) => {
        console.log('Exception while replacing friend: ' + error);
});

Adding a Friend

You can make any user become a friend of the current user. If the operation succeeds, both of them will be friends. If you are trying to add a user, that is your friend already, the operation will succeed, nothing will be changed, and you will just receive the current friends count.

By GetSocial User IDs

```java tab=”Android(Java)” hl_lines=”3”
final String getSocialUserId = anotherUser.getId();

GetSocial.User.addFriend(getSocialUserId, new Callback() {
@Override
public void onSuccess(Integer friendsCount) {
Log.i(“GetSocial”, getSocialUserId + ” successfully added as a friend. Total friends count: ” + friendsCount);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while adding a friend: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”3”
val getSocialUserId = anotherUser.id

GetSocial.User.addFriend(getSocialUserId, object : Callback {
override fun onSuccess(friendsCount : Int) {
println(“${getSocialUserId} successfully added as a friend. Total friends count: ${friendsCount}”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while adding a friend, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”2”
NSString *getSocialUserId = anotherUser.userId;
[GetSocialUser addFriend:getSocialUserId success:^(int friendsCount) {
NSLog(@”%@ successfully added as a friend. Total friend count: %d”, getSocialUserId, friendsCount);
} failure:^(NSError *error) {
NSLog(@”Exception while adding a friend: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="2"
let getSocialUserId = anotherUser.userId
GetSocialUser.addFriend(getSocialUserId, success: { friendsCount in
    print("\(getSocialUserId) successfully added as a friend. Total friend count: \(friendsCount)")
}, failure: { error in
    print("Exception while adding a friend: \(error)")
})

```csharp tab=”Unity” hl_lines=”3”
var getSocialUserId = anotherUser.Id;

GetSocial.User.AddFriend (getSocialUserId,
friendsCount => {
Debug.Log (getSocialUserId + ” successfully added as a friend. Total friends count: ” + friendsCount);
},
error => {
Debug.Log (“Exception while adding a friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="3"
const getSocialUserId = anotherUser.userId;

GetSocialUser.addFriend(getSocialUserId).then(
    (friendsCount) => {
        console.log(getSocialUserId + ' successfully added as a friend. Total friends count: ' + friendsCount);
    }, (error) => {
        console.log('Exception while adding a friend: ' + error);
    });

By Auth Identities

```java tab=”Android(Java)” hl_lines=”4”
List providerUserIds = Arrays.asList(“external_user_id_1”, “external_user_id_2”);
String identityProviderId = “my_custom_identity_provider_id”;

GetSocial.User.addFriendsByAuthIdentities(identityProviderId, providerUserIds, new Callback() {
@Override
public void onSuccess(Integer friendsCount) {
Log.i(“GetSocial”, “Friends successfully added. Total friends count: ” + friendsCount);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while adding friends: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”4”
val providerUserIds = listOf(“external_user_id_1”, “external_user_id_2”)
val identityProviderId = “my_custom_identity_provider_id”

GetSocial.User.addFriendsByAuthIdentities(identityProviderId, providerUserIds, object : Callback {
override fun onSuccess(friendsCount : Int) {
println(“Friends successfully added. Total friends count: ${friendsCount}”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while adding friends, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”3”
NSArray* providerUserIds = @[@”1234”, @”456”];
NSString* identityProviderId = @”custom_auth_method”;
[GetSocialUser addFriendsWithIds:providerUserIds forProvider:identityProviderId success:^(int friendsCount) {
NSLog(@”Friends successfully added. Total friend count: %d” , friendsCount);
} failure:^(NSError *error) {
NSLog(@”Exception while adding a friends: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="3"
let providerUserIds = ["1234", "456"]
let identityProviderId = "custom_auth_method"
GetSocialUser.addFriends(withIds: providerUserIds, forProvider: identityProviderId, success: { friendsCount in
    print("Friends successfully added. Total friend count: \(friendsCount)")
}, failure: { error in
    print("Exception while adding a friends: \(error)")
})

```csharp tab=”Unity” hl_lines=”6”
List providerUserIds = new List();
providerUserIds.Add(“123”);
providerUserIds.Add(“456”);
string identityProviderId = “custom_auth_method”;

GetSocial.User.AddFriendsByAuthIdentities(identityProviderId, providerUserIds,
friendsCount => {
Debug.Log (“Successfully added friends. Total friends count: ” + friendsCount);
},
error => {
Debug.Log (“Exception while adding friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="4"
const providerUserIds = ['123','456'];
const identityProviderId = 'custom_auth_method';

GetSocialUser.addFriendsByAuthIdentities(identityProviderId, providerUserIds).then(
    (friendsCount) => {
        console.log('Successfully added friends. Total friends count: ' + friendsCount);
    }, 
    (error) => {
        console.log('Exception while adding friend: ' + error);
    });

Removing a Friend

You can remove any user from the current user’s friend list by user’s unique ID. If you are trying to remove a user, who’s not on the friend list, the operation will succeed, nothing will be changed, and you will just receive the current friends count.

By GetSocial User IDs

```java tab=”Android(Java)” hl_lines=”3”
final String getSocialUserId = anotherUser.getId();

GetSocial.User.removeFriend(getSocialUserId, new Callback() {
@Override
public void onSuccess(Integer friendsCount) {
Log.i(“GetSocial”, getSocialUserId + ” successfully removed from a friends list. Total friend count: ” + friendsCount);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while removing a friend: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”3”
val getSocialUserId = anotherUser.id

GetSocial.User.removeFriend(getSocialUserId, object : Callback {
override fun onSuccess(friendsCount : Int) {
println(“${getSocialUserId} successfully removed from a friends list. Total friend count: ${friendsCount}”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while removing a friend, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”2”
NSString *getSocialUserId = anotherUser.userId;
[GetSocialUser removeFriend:getSocialUserId success:^(int friendsCount) {
NSLog(@”%@ successfully removed from friends. Total friends count: %d”, getSocialUserId, friendsCount);
} failure:^(NSError *error) {
NSLog(@”Exception while removing a friend: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="2"
let getSocialUserId = anotherUser.userId
GetSocialUser.removeFriend(getSocialUserId, success: { friendsCount in
    print("\(getSocialUserId) successfully removed from friends. Total friends count: \(friendsCount)")
}, failure: { error in
    print("Exception while removing a friend: \(error)")
})

```csharp tab=”Unity” hl_lines=”3”
var getSocialUserId = anotherUser.Id;

GetSocial.User.RemoveFriend (getSocialUserId,
friendsCount => {
Debug.Log (getSocialUserId + ” successfully removed from friends. Total friends count: ” + friendsCount);
},
error => {
Debug.Log (“Exception while removing a friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="3"
const getSocialUserId = anotherUser.userId;

GetSocialUser.removeFriend(getSocialUserId).then(
    (friendsCount) => {
        console.log(getSocialUserId + ' successfully removed from friends. Total friends count: ' + friendsCount);
    }, 
    (error) => {
        console.log('Exception while removing a friend: ' + error);
    });

By Auth Identities

```java tab=”Android(Java)” hl_lines=”4”
List providerUserIds = Arrays.asList(“external_user_id_1”, “external_user_id_2”);
String identityProviderId = “my_custom_identity_provider_id”;

GetSocial.User.removeFriendsByAuthIdentities(identityProviderId, providerUserIds, new Callback() {
@Override
public void onSuccess(Integer friendsCount) {
Log.i(“GetSocial”, “Users successfully removed from a friends list. Total friend count: ” + friendsCount);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("GetSocial", "Exception while removing friends: " + exception.getMessage());
}

});
kotlin tab=”Android(Kotlin)” hl_lines=”4”
val providerUserIds = listOf(“external_user_id_1”, “external_user_id_2”)
val identityProviderId = “my_custom_identity_provider_id”

GetSocial.User.removeFriendsByAuthIdentities(identityProviderId, providerUserIds, object : Callback {
override fun onSuccess(friendsCount : Int) {
println(“Users successfully removed from a friends list. Total friend count: ${friendsCount}”)
}

override fun onFailure(exception: GetSocialException) {
println(“Exception while removing friends, error: ${exception.message}”)
}
})
```

```objc tab=”iOS(Objective-C)” hl_lines=”3”
NSArray* providerUserIds = @[@”1234”, “456”];
NSString* identityProviderId = @”custom_auth_method”;
[GetSocialUser removeFriendsWithIds:providerUserIds forProvider:identityProviderId success:^(int friendsCount) {
NSLog(@”Friends successfully removed. Total friends count: %d”, friendsCount);
} failure:^(NSError *error) {
NSLog(@”Exception while removing friends: %@”, error);
}];

```swift tab="iOS(Swift)" hl_lines="3"
let providerUserIds = ["1234", "456"]
let identityProviderId = "custom_auth_method"
GetSocialUser.removeFriends(withIds: providerUserIds, forProvider: identityProviderId, success: { friendsCount in
    print("Friends successfully removed. Total friends count: \(friendsCount)")
}, failure: { error in
    print("Exception while removing friends: \(error)")
})

```csharp tab=”Unity” hl_lines=”6”
List providerUserIds = new ArrayList();
userIdList.Add(“123”);
userIdList.Add(“456”);
string identityProviderId = “custom_auth_method”;

GetSocial.User.RemoveFriendsByAuthIdentities(identityProviderId, providerUserIds
friendsCount => {
Debug.Log (“Successfully removed friends. Total friends count: ” + friendsCount);
},
error => {
Debug.Log (“Exception while removing a friend: ” + error.Message);
});

```javascript tab="React Native" hl_lines="4"
const providerUserIds = ['123', '456'];
const identityProviderId = 'custom_auth_method';

GetSocialUser.removeFriendsByAuthIdentities(identityProviderId, providerUserIds).then(
    (friendsCount) => {
        console.log('Successfully removed friends. Total friends count: ' + friendsCount);
    }, 
    (error) => {
        console.log('Exception while removing a friend: ' + error);
    });

Next steps

  • Learn how to query social graph here

Give us your feedback! Was this article helpful?

😀 🙁