Skip to main content

Using customer groups

info

This recipe assumes you've already familiarized yourself with the Core API getting started, Authentication, and Error Handling pages.

Customer groups

Customer groups can be used to group your customers into different tiers, e.g. free tier, design partner, enterprise etc.

Customers can be part of one or multiple groups. You can filter your customers by group allowing you to quickly focus on a subset of your customers.

In this recipe we will be will show you how to add and remove customers from customer groups.

This recipe assumes you've already made some customer groups in your workspace settings!

Customer groups

Add customer to customer groups

A customer can be added to a customer group using the addCustomerToCustomerGroup mutation or the upsertCustomer mutation. Here are some examples of when you might want to add a customer to a group:

  • Off the back of the Plain customer.changed Webhook. This will allow you to lazily fill out which groups customers are in as they are created in Plain.
  • When they do something that affects their groups. Say you want to group customers by their ‘tier’ then when their tier changes you’ll want to make sure their group is updated.
  • Anywhere you are ‘creating’ customers in Plain.

Mutation

The GraphQL mutation is the following:

mutation addCustomerToCustomerGroup($input:AddCustomerToCustomerGroupsInput!) {
addCustomerToCustomerGroups(input:$input) {
customerGroupMemberships {
customerGroup {
id
name
key
color
}
}
error {
message
code
type
fields {
field
message
type
}
}
}
}

Variables

{
"input": {
"customerId": "c_01GTC6ZHCMAGR06FMPN9VY5J95",
"customerGroupIdentifiers": [
{
"customerGroupKey": "free-tier"
},
{
"customerGroupKey": "design-partner"
}
]
}
}
info

If you prefer you can also use the customer group id instead of the key. You can do this like so:

{
"input": {
"customerId": "c_01GTC6ZHCMAGR06FMPN9VY5J95",
"customerGroupIdentifiers": [
{
"customerGroupId": "cg_01GX8H2BWS7Z4EP9RRGMF9NXX2"
},
]
}
}

Response

{
"data": {
"customerGroupMemberships": [{
"customerId": "c_01GTC6ZHCMAGR06FMPN9VY5J95",
"customerGroup": {
"id": "cg_01GX8H2BWS7Z4EP9RRGMF9NXX2",
"name": "Free Tier",
"key": "free-tier",
"color": "#1D4ED8",
},
{
"id": "cg_05GXF4FGGS2Z7EW9RRGKL9NLL9",
"name": "Design partner",
"key": "design-partner",
"color": "#06B6D4",
},
}],
"error": null
}
}

Remove customer from customer groups

A customer can be removed from a customer group by using the removeCustomerFromGroup mutation.

Mutation

The GraphQL mutation is the following:

mutation removeCustomerFromCustomerGroup($input:RemoveCustomerFromCustomerGroupsInput!) {
removeCustomerFromCustomerGroups(input:$input) {
error {
message
code
type
fields {
field
message
type
}
}
}
}

Variables

{
"input": {
"customerId": "c_01GTC6ZHCMAGR06FMPN9VY5J95",
"customerGroupIdentifiers": [
{
"customerGroupKey": "free-tier"
},
{
"customerGroupKey": "design-partner"
}
]
}
}
info

If you prefer you can also use the customer group id instead of the key. You can do this like so:

{
"input": {
"customerId": "c_01GTC6ZHCMAGR06FMPN9VY5J95",
"customerGroupIdentifiers": [
{
"customerGroupId": "cg_01GX8H2BWS7Z4EP9RRGMF9NXX2"
},
]
}
}

Response

{
"data": {
"error": null
}
}

If you have any problems, please get in touch with us by email on help@plain.com, and we will be happy to help.