I'm currently seeking new Product Management opportunities.

How to Build an AI Agent : Case study - Shopify CRO Agent

How to Build an AI Agent

Everyone is talking about AI right now. But 99% of developers are just building fancy chatbots. You ask a question. It gives an answer. It waits for you. That is passive. It is a tool, not a worker.

If you want to know how to build an AI agent that actually works, you need to think differently. An agent has agency. It doesn't wait for you to ask, "What is wrong with my website?" It is already looking at your website. It analyzes the data. It fixes the code while you sleep.

In this guide, we will move beyond theory. We will show you exactly how to create an AI agent for a real-world Shopify store. This agent will audit pages, design better layouts, and write code automatically.

How to Build an AI Agent

The Theory - Stop Building Chatbots

Before we write code, you must understand the brain of an agent. Most people fail because they treat agents like scripts. A script follows a straight line. An agent follows a loop.

! Niche ko find karo dosti se. We are building a friend for Shopify store owners. Their pain is manual testing. It is hard. It is boring. Our agent takes that pain away.

The Agent Loop: Perception → Decision → Action

To understand how to build an AI agent, you must master the Brain Loop.

  1. Perception (The Eyes): The agent must see the world. In our example, it uses tools to scrape your website and read your sales data.
  2. Decision (The Brain): It analyzes this data against a goal. It asks, "Why are sales down?" It sees a button color issue. It forms a plan.
  3. Action (The Hands): This is the magic. It doesn't just suggest a fix. It executes it. It writes the code and pushes it to your live store.

The Blueprint - The Shopify CRO Agent

We are building a Conversion Rate Optimization (CRO) Engine. This is not a simple tutorial. This is an enterprise-grade system.

Visual Workflow and JSON File 

The Workflow Logic:

  1. Input: You give the agent a URL (e.g., .../products/red-shoes).
  2. Data Collection: It asks Shopify: "How many units did we sell?". It asks Browserless: "Take a photo of this page." It asks Google: "Is this page slow?".
  3. The Brain (OpenAI): It looks at the photo. It says, "The 'Buy' button is invisible. Move it up."
  4. The Action: It writes new HTML/CSS. It updates Shopify's Metafields to change the live site instantly.

The Tech Stack to Build AI Agents

You need the right tools. We will use a Low Code stack. It is powerful. It is fast.

Tool Role Why we use it
n8n + Shopify API The Orchestrator + Data Source The nervous system. It connects the apps. Shopify gives us product data like price and inventory.
Browserless The Eyes It takes high-quality screenshots of the site for the AI to "see".
Google PageSpeed The Analyst It measures site speed (Core Web Vitals) to check for technical lag.
OpenAI (GPT-4o) The Brain It analyzes the visual screenshot and writes the actual code.

Step by Step: How to Build an AI Agent

We will break this down into 11 clear phases. Follow these steps to build AI agents that work.

  1. PHASE 1: The Input Trigger
    Goal: Tell the agent which product to fix. We do not want the agent guessing. We give it a target.
    Action: You paste a URL like https://yourstore.myshopify.com/products/red-running-shoes into an n8n Webhook or Manual Trigger node.
  2. PHASE 2: Understanding the Product
    Goal: Get the official ID and details. A URL is just text. The agent needs the Product ID.
    Step 1: Extract the handle (red-running-shoes) from the URL.
    Step 2: Query the Shopify Admin API to get the ID, Title, and Inventory count.
  3. PHASE 3: Sales Performance Check
    Goal: Establish a baseline. Is the product failing? If it sells 1000 units, do not touch it. The agent checks the last 30 days of revenue and units sold via the Shopify API to decide if the product needs improvement.
  4. PHASE 4: The Eyes (Visual Analysis)
    Goal: See what the customer sees. Shopify's API gives text. It does not show layout errors. We need eyes.
    Tool: Browserless or Playwright.
    Action: Fetch the raw rendered HTML and take a full page screenshot. The AI will look at this image to find contrast issues.
  5. PHASE 5: Performance Speed Audit
    Goal: Check for technical lag. A slow page kills conversions.
    Tool: Google PageSpeed Insights API. We collect metrics like LCP (loading speed), CLS (visual stability), and the overall Score (0-100).
  6. PHASE 6: The Snapshot (Audit Record)
    Goal: Save the "Before" state. You must record history.
    Action: Save all data (URL, Revenue, Screenshot, Speed) to a database like Google Sheets or Notion. This proves the agent worked later on.
  7. PHASE 7: The Brain (AI Analysis & Design)
    Goal: Analyze problems and generate code.
    Action: Send the Screenshot, Sales Data, and Speed Score to OpenAI. The AI performs 3 tasks: Audit the design, Invent a strategy (e.g., "Make button red"), and Engineer the JSON object with new Heading and CSS.
  8. PHASE 8 & 9: The Hands (Auto Updating Shopify)
    Goal: Push the design to the live store.
    The Concept: You cannot inject HTML safely. We use Metafields. n8n sends a GraphQL Mutation to fill the metafields with the AI's content (e.g., ai_cta.heading → "Buy Now - Ships Today").
  9. PHASE 10: Live Deployment
    Goal: Instant gratification.
    Result: The n8n workflow finishes. The product page updates instantly. No developer needed. No theme editing. Zero manual work.
  10. PHASE 11: The Feedback Loop
    Goal: Continuous improvement.
    Action: The agent waits 7 days. It checks stats again. Did revenue go up? If yes, keep it. If no, the agent tries a new design.

The Code Vault

Here is the code to help you build an AI agent today.

1. The Shopify Liquid Receiver
Paste this into a new Section called aismart-cta.liquid.

{% comment %}
This section listens to the AI Agent via Metafields.
{% endcomment %}

{% if product.metafields.ai_cta.enabled %}

{{ product.metafields.ai_cta.heading }}

{{ product.metafields.ai_cta.subheading }}

{{ product.metafields.ai_cta.trust_text }}
{% endif %}

2. The GraphQL Mutation
Use this in your n8n Shopify node to update the data.

mutation SetCtaMetafields {
  metafieldsSet(metafields: [
    {
      namespace: "ai_cta", 
      key: "heading", 
      value: "Limited Time Offer", 
      type: "single_line_text_field",
      ownerId: "gid://shopify/Product/12345"
    },
    {
      namespace: "ai_cta", 
      key: "button_label", 
      value: "Buy Now", 
      type: "single_line_text_field", 
      ownerId: "gid://shopify/Product/12345"
    },
    {
      namespace: "ai_cta", 
      key: "enabled", 
      value: "true", 
      type: "boolean", 
      ownerId: "gid://shopify/Product/12345"
    }
  ]) {
    userErrors {
      message
    }
    metafields {
      key
      value
    }
  }
}

3. The Brain Prompt
Paste this into the OpenAI node System Prompt.

You are a CRO Expert.
I will give you a screenshot and sales data.
Your Goal: Analyze why users are not converting. 
Generate a NEW Call to Action strategy.
Output: A JSON object with heading, button_label, and trust_text.

* This is how you create an AI agent that generates revenue.