How Complex uses AI to match products with articles

  • 80 hours
    saved per month
  • 60 lines
    of code
  • Zero
    added services

AI automatically adds relevant product recommendations to every article. ~60 lines of code. No added infrastructure. All native to Sanity.

With 1,000 new articles a month and over 15,000 products in the catalog, this begs the question, "How do we connect articles with products?".

Complex has asked itself this in the past, but the process was manual and time-consuming, and even if they could overcome that, the products that surfaced in articles weren't the highest-converting.

They revisited this process with AI and found a solution that saves them 80 hours per month and surfaces the most relevant products, helping them sell more by doing less.

Here's the "why" and "how" they made this happen.

Challenge: Too many products for manual linking

Before Sanity, Complex had two options:

  1. Building a costly system to automate the linking of products to articles
  2. Keep things manual, which meant inefficiency and lower conversion rates

"Can we solve this?" is often the first question engineers ask when seeing some inefficient process.

If the answer is "yes," then it moves to "Should we solve this?" This opens the floor to many more questions that usually boil down to the cost of services and ownership. Each service added to the tech stack will take away precious engineering time from other initiatives.

Complex could have automated adding products to articles with:

  • A webhook that fires from Sanity, containing the article data
  • A serverless function on some other platform to catch the webhook and run the automation
  • A vector database to store embeddings, which enables semantic queries to match products with articles. This would also require another webhook to sync products to it.

Worth it? Arguably, but for Complex, it was "Not right now."

Without automation, they had the problem of too much information for humans to go through. There are 1,000 articles published per month, each requiring an editor to add related products based on the context of the post. With a catalog of over 15,000 products, this means a time-consuming search of about 5 minutes per article.

To make matters worse, the most relevant products were hard to find. Users skim over "related" products that aren't related. But if they read an article about Lollapalooza and see their favorite Chicago-based artist's collection, they are much more likely to purchase.

When Sanity released serverless functions, Complex didn't need to choose between the lesser of two evils.

Solution: What they wanted, without the baggage

Complex now had everything it needed in one system, including AI and functions. This meant Complex could create its ideal automation without adding any extra infrastructure, services, or glue code.

First, let's examine what happens when editors click "Publish":

  1. An editor adds a new piece of content and leaves the "Related Products" field empty
  2. They click publish
  3. A check is performed to see if the function should run based on the user-defined criteria (e.g., document type is "article" and it doesn't have related products)
  4. The function runs and is provided the document as context
  5. The function asks the embeddings index for the top four most relevant products that meet a relevancy threshold
  6. The returned products are saved back to the document

This all happens within seconds. In addition to the function logic, publishing automatically populates the embeddings index.

The automation in action

Now, let's look at the building blocks. They are native to Sanity, which makes this easy to implement and less to maintain.

  • Embeddings Index: This is an experimental feature. It enables semantic searches for products. Every time a new product is published, AI analyzes it and converts it to an array of numbers representing meaning and relationships to other content (searches also go through this conversion, which is how a search derives relevant results). To do this, Complex added a new index with the Embeddings UI, added a filter only to add products published in the last two years, and a projection to store just the relevant info (i.e., title, tags, and body).
  • Functions: Serverless compute to run the automation logic. Complex wrote about 60 lines of code, and Sanity handles the rest. The system triggers the function on publish if the filter condition returns true. For example, _type == 'article' && !defined(relatedProducts), which they use to only execute on articles that don't have related products yet. The system provides the function with the published document, and the code queries the embeddings index for the four most relevant products. It then adds the returned products to the document.

Results: 33% less time per article, no added infrastructure

Because all the building blocks are native to Sanity, Complex said, "Yes, we can solve this," and "Yes, we should solve this."

The results:

  • 80 hours of editorial time saved per month. Editors used to spend 5 minutes per post searching for related products and published 1,000 posts/month.
  • ~60 lines of code. Most of it is boilerplate code, initializing the client and handling errors. Very little code is needed to bring this automation to life, which means less to maintain.
  • Easier onboarding. When new freelancers join, they just publish without having to be trained on workflows.
  • Millions of dollars in e-commerce sales. Every article now serves as an avenue to sell products. The products are highly aligned with the context of the article, leading to higher conversion rates.
  • Nearly zero maintenance. This was the kicker for Complex. They can add immense value to their editors and visitors without spinning up extra services or maintaining more tech.

Complex now uses AI to connect products to articles

"Can we do this?"

"Should we do this?"

The Sanity Content Operating System is built knowing that every business is unique. So, it provides the primitives to configure it to yours while keeping maintenance to an absolute minimum.

Watch Complex demo this automation

See how the engineering team at Complex uses AI, embedding indexes, and serverless compute to increase sales with less effort.