How Suprsend identifies a user
SuprSend identifies users with immutable distinct_id
. It’s best to map the same identifier in your DB with distinct_id
in SuprSend. Do not use identifiers that can be changed like email or phone number. You can view synced users by searching distinct_id
on Users page.
Please note: you cannot change a user’s id once it has been set, so we recommend you use a non-transient id like a primary key rather than a phone number or email address.
Create User
To create a new user or to update an existing user, you’ll have to fetch user instance. Call supr_client.user.get_instance
to instantiate user object.
import org.json.JSONObject;
import suprsend.Suprsend;
import suprsend.Subscriber;
public class UserEdit {
public static void main(String[] args) throws Exception {
getInstance();
}
private static Subscriber getSuprClient() throws SuprsendException {
Suprsend suprsendClient = new Suprsend("_workspace_key_", "_workspace_secret_");
return suprsendClient;
}
private static Subscriber getInstance() throws SuprsendException {
Suprsend suprsendClient = getSuprClient();
// Instiantiate user
String distinctId = "_distinct_id_";
Subscriber user = suprClient.user.getInstance(distinctId);
return user;
}
}
Edit User
To Edit user, you need to first fetch user instance, call all the update methods and save changes using user.save
method.
import org.json.JSONObject;
import suprsend.Suprsend;
import suprsend.Subscriber;
public class UserEdit {
public static void updateProfile() throws Exception {
Suprsend suprsendClient = getSuprClient();
// User Edit Instance
String distinctID = "_distinct_id_";
Subscriber user = suprsendClient.user.getInstance(distinctID);
// Edit Helper methods
user.addEmail("example@example.com");
user.setTimezone("America/New_York");
// Save
JSONObject response = user.save();
System.out.println(response);
}
}
Here’s a list of all edit methods:
After calling add_*/remove_*/unset
methods, don’t forget to call users.save()
since user edit is async update and the changes will be sent to SuprSend only after calling this method.
Bulk Update Users
There isn’t any limit on number-of-records that can be added to bulk_users
instance.
Use .append()
on bulk_users instance to add however-many-records to call in bulk.
//Creating bulk instance
BulkSubscribers bulkIns = suprClient.bulkUsers.newInstance();
// Prepare multiple users
String distinctID1 = "__distinct_id1__"; // User 1
User u1 = suprsendClient.user.getInstance(distinctID1);
u1.addEmail("u1@example.com");
String distinctID2 = "__distinct_id2__"; // User 2
User u2 = suprsendClient.user.getInstance(distinctID2);
u2.addEmail("u2@example.com");
// --- use .append on bulk instance to add one or more records
bulkIns.append(u1);
bulkIns.append(u2);
// OR
bulkIns.append(u1, u2);
// Save
JSONObject response = bulkIns.save();
System.out.println(response);
Bulk API supported in SDK version 0.2.0 and above:
Bulk API is supported in SuprSend python-sdk version 0.2.0 and above. If you are using an older version, please upgrade to the latest SDK version.
Get User Details
Fetch User by passing distinct_id
String distinctId = "_distinct_id_";
JSONObject res = suprClient.users.get(distinctId);
System.out.println(response);
Delete User
Delete User by passing distinct_id
. Delete action will take into immediate effect.
String distinctId = "_distinct_id_";
JSONObject res = suprClient.users.delete(distinctId);
System.out.println(response);
Get list of objects subscribed by user
You can pass optional query parameters -limit
,before
,after
String distinctId = "_distinct_id_";
// optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
HashMap < String, Object > opts = new HashMap < String, Object > () {
{
put("limit", 10);
put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
}
};
JSONObject res = suprClient.users.getObjectsSubscribedTo(distinctId, opts);
System.out.println(response);
Get lists subscribed by user
You can pass optional query parameters -limit
, before
, after
String distinctId = "_distinct_id_";
// optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
HashMap < String, Object > opts = new HashMap < String, Object > () {
{
put("limit", 10);
put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
}
};
JSONObject res = suprClient.users.getListsSubscribedTo(distinctId, opts);
System.out.println(response);