Customer Queries with Metafield Value as Filter

gunesc
Shopify Partner
3 0 1

Hi,

 

I was wondering if there was a way to query Customers, depending on their custom Metafield values.

 

I see that this is possible (and incredibly quick and easy I must say) while creating a customer segment. So I can see that internally shopify is able to query customer metafields and use that as a segment definition and show all the customers that fit the criteria, based on the filter that I set in this screen.

 

Screenshot 2024-04-29 at 14.20.28.png

Is there a way to access this API? I dont see it anywhere in default admin api documentation for querying customers.

 

 

Replies 4 (4)

kjchabra
Shopify Partner
26 1 5

@gunesc for this particular use, you can use the Segments API.

 

API makes it possible to query the list of members in the segments as well.

gunesc
Shopify Partner
3 0 1

@kjchabrathanks, but I've mentioned that already in my question.

 

And this way is not really efficient where you:

 

  • Create a customer segment with the correct metafield key/value pair (returns segment id)
  • Query customers that falls into this segment via segment id (returns member id's, not customer id's)
  • Query customers with the given emails to find the customer id (returns customer id)
  • Delete customer segment with the segment id

I was more asking if there is a way to do what they are doing with step 1, without all the other steps and get the customer id.

 

Because in the admin UI, you immediately see members of a segment WITH their customer page links (aka customer id's), as soon as you apply the filter that you created. Which is not the case for the segments api.

 

kjchabra
Shopify Partner
26 1 5

@gunesc thanks for the explanation.

Currently with GraphQL Admin API, you can only query Customers based on the properties listed here. Unfortunately metafield by value is not one of them. 

 

You can pull the metafield value by providing key and namespace in the metafield attribute of the customers query. For example:

query {
  customers(first: 100) {
    edges {
      node {
        id
        metafield(namespace: "custom", key: "favourite_color"){
          id
          value
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

returns an array of customer ids and the metafield values for those customers.

{
  "data": {
    "customers": {
      "edges": [
        {
          "node": {
            "id": "gid://shopify/Customer/7070853234943",
            "metafield": null
          }
        },
        {
          "node": {
            "id": "gid://shopify/Customer/7070853267711",
            "metafield": null
          }
        },
        {
          "node": {
            "id": "gid://shopify/Customer/7070853300479",
            "metafield": null
          }
        },
        {
          "node": {
            "id": "gid://shopify/Customer/7071058657535",
            "metafield": {
              "id": "gid://shopify/Metafield/26440047132927",
              "value": "#00c4f5"
            }
          }
        },
        {
          "node": {
            "id": "gid://shopify/Customer/7071098077439",
            "metafield": {
              "id": "gid://shopify/Metafield/26157069009151",
              "value": "#00d4f0"
            }
          }
        }
      ],
      "pageInfo": {
        "hasNextPage": false,
        "endCursor": "eyJsYXN0X2lkIjo3MDcxMDk4MDc3NDM5LCJsYXN0X3ZhbHVlIjoiNzA3MTA5ODA3NzQzOSJ9"
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 20,
      "actualQueryCost": 8,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1992,
        "restoreRate": 100
      }
    }
  }
}

If the metafield value doesn't exist, then the query results display null.

 

You can use the resulting array and filter out the elements that don't match your criteria or are null. Also the query runs on getting first 100 customers, you will have to have handle pagination via the pageInfo cursor to get all customers. Not sure if this is more helpful.

gunesc
Shopify Partner
3 0 1

No it's not 🙂 I'm not going to run a query for all our customers and filter it.

 

You can pull the metafield value by providing key and namespace in the metafield attribute of the customers query. For example:

And did you just explain to me how to get a metafield value? lol