User IDs

Assign your own user IDs to integrate WonderPush with your systems

You can assign your own user IDs to installations in WonderPush. This allows you to target users by user ID and lets you tightly integrate WonderPush with your systems.

Assigning your own user IDs

Assigning your own user IDs depends on the platform (adapt THIS_USER_ID) with your own user ID:

window.WonderPush = window.WonderPush || [];
WonderPush.push(['setUserId', "THIS_USER_ID"]);
WonderPush.setUserId("THIS_USER_ID")
[WonderPush setUserId:@"THIS_USER_ID"];
WonderPush.setUserId("THIS_USER_ID");
WonderPush.setUserId("THIS_USER_ID");

You only need to specify the userId once: it persists until next time you specify a userId or null.

Removing a user ID

User IDS can be disassociated from the current device Installation id using the Wonderpush SDK unsetUserId method. It is recommended to call this method when the user logs out of your app or website.

window.WonderPush = window.WonderPush || [];
WonderPush.push(['unsetUserId']);
WonderPush.setUserId(null);
[WonderPush setUserId:nil];
WonderPush.setUserId(nil)

When to change the user ID

You can change the current user ID:

  • when anonymous users log into your app / website, user ID goes from null to something,
  • when a user logs out of your app / website, user ID goes back to null,
  • when a user directly switches from one account to another, we encourage you to directly set the user ID to the new value without unsetting it in between, if this matches your UI flow.

You do not have to set the user ID everytime your application starts, WonderPush remembers the last used ID; but if it's easier for you, feel free to.

Changing the user ID is akin to loading a new profile

When the user ID changes, a new separate installation is used, it is also created if it's the first time a given user ID is used on a given device. This means that all the tags, properties and events you've sent before changing the user ID will not be attached to the new installation. This ensures that data doesn't get mixed up between different user accounts. The push token is also moved from the old to the new installation, meaning the old installation is no longer pushable.

If you would like to keep all or some tags or properties, you can choose one of the following strategies:

  • change the user ID; then set all tags and properties according to your own external source of truth.
  • get all tags and properties of the current profile; then change the user ID; then set the tags and properties on the new profile

Getting all the current tags is done using WonderPush.getTags(). Getting all the current properties is done using WonderPush.getProperties().

Adding multiple tags at once is done by giving an array of tags to WonderPush.addTag() (WonderPush.addTags() on iOS). Setting multiple properties at once is done by giving an object to WonderPush.putProperties().

You might want to pay attention to the existing tags and properties if you want to avoid leftovers. You can remove all tags using WonderPush.removeAllTags(). You can remove all properties by calling assigning null to every existing property.

Targeting users with the API

You can use the targetUserIds parameter of the Send notifications API call to target one or more users.

# Sending a notificaton to 2 users: userID1 and userID2 
# Replace YOUR_ACCESS_TOKEN with your Management API access token
curl -XPOST https://management-api.wonderpush.com/v1/deliveries?accessToken=YOUR_ACCESS_TOKEN \
    -H 'Content-Type: application/json' \
    --data-binary '{
        notification: {"alert":{"text":"test"}},
        targetUserIds: ["userID1","userID2"]
    }'
// Sending a notificaton to 2 users: userID1 and userID2
// Adapt WONDERPUSH_ACCESS_TOKEN, WONDERPUSH_APPLICATION_ID
$client = new \WonderPush\WonderPush(WONDERPUSH_ACCESS_TOKEN, WONDERPUSH_APPLICATION_ID);
$client->deliveries()->create(array(
    'notification' => array(
      'alert' => array(
        'text' => 'test',
      ),
    ),
    'targetUserIds' => ['userID1','userID2'],
  ));

Targeting users via the online dashboard

To target one or more users from the target tab of the notification edition interface, setup a criterion of type User id is equal to.

Using the Management API to enrich users

The user objects are meant to bear additional properties to help you further segment your audience.

You can use the Management API PATCH /v1/users/someUserId endpoint to add new properties to a user:

curl -XPATCH https://management-api.wonderpush.com/v1/users/someUserId?accessToken=YOUR_ACCESS_TOKEN \
    -H 'Content-Type: application/json' \
    --data-binary '{
        "custom": {
            "string_subscribedPlan": "premium"
        }
    }'

You can then segment your users in the dashboard using the criterion of type User property (string):

This will select every installations whose associated user has the value premium in the string_subscribedPlan property.