How to use AI for automatic Shopify product categorization
Learn how to leverage ChatGPT and Cloudhooks to automatically categorize your Shopify products using Google's taxonomy, saving hours of manual work while ensuring accurate classifications.
Managing product categories in Shopify is a time-consuming challenge. With over 6,000 categories in the Google Product Taxonomy, manually classifying each product can become overwhelming.
Our Shopify app Cloudhooks, combined with ChatGPT, automates this entire process, potentially saving hours of manual categorization work while ensuring consistent and accurate product classifications.
In this tutorial we'll show you step by step how to set up this automation, and how you can save both time and money by automating this process.
Time to complete: ~15 minutes Difficulty level: Intermediate
Prerequisites
This tutorial assumes that you have:
Cloudhooks installed in your Shopify store
An active ChatGPT subscription
Access to your OpenAI account
Set up OpenAI
First, we'll create an OpenAI project and generate the necessary API credentials. This setup will enable AI-powered product categorization through ChatGPT's API.
3) Click "+Create new secret key" and configure these security settings:
Owned by: You
Name: Enter a descriptive name
Permissions: Restricted
Permission details:
Models: Read
Model capabilities: Write
Assistants: Read
Threads: Write
Fine-tuning: None
Files: None
⚠️ Important: Copy and securely store your API key immediately. You won't be able to see it again after leaving this page.
Create an assistant
Now we'll set up an AI assistant specifically trained for product categorization. This assistant will use the Google Product Taxonomy to accurately classify your products.
3) Click “+Create” button to create a new assistant.
Assistant configuration
Name
Fill it in as you see fit.
Instructions
Use the following instructions:
You are a product categorization expert. You receive a product description and you return a product category entry based on the Google Product Taxonomies text file which is attached.
The taxonomy is returned as a JSON object, which has three properties: "categoryId", "category", and "tags". The "categoryId" property contains the numeric identifier of the category. The "category" property contains the entire category description, starting with the general category all the way down to the specific category. The "tags" property is an array containing the category chopped up into its pieces, separated by the ">" character.
Here are some examples about how a category translates into a JSON object.
Category: 3237 - Animals & Pet Supplies > Live Animals JSON: {"categoryId": 3237, "category": "Animals & Pet Supplies > Live Animals", tags: ["Animals & Pet Supplies", "Live Animals"]}
Always and only return the JSON, not descriptive text. A life of a human might depend on it if you don't return only a JSON.
Model
gpt-4o (you can change it later to “gpt-4o-mini” if you’d like to experiment with a cheaper alternative)
File search
Leave it turned off, and upload the attached Google Product Taxonomy text file.
Code interpreter
Leave it turned off.
Response format
Set it to “json_object”.
Here is how the assistant setup looks like:
After configuring your assistant, copy and save the assistant ID - you'll need it for the Cloudhooks integration.
Set up the hook in Cloudhooks
Now we'll create a hook in Cloudhooks that automatically triggers the AI categorization whenever a new product is added to your store. This hook will:
Detect when a new product is added
Send the product details to your OpenAI assistant
Receive the categorization response
Apply the categories as tags to your product
Create the hook
Follow these steps:
1) From your Cloudhooks dashboard, click "Create hook" in the top-right corner:
2) Configure the trigger:
Open the "Trigger" tab
Navigate to the "Product" category
Select "A product is added"
Click "Select trigger"
3) Add the hook code - navigate to the “Hook” tab
Copy the code from the end of this article and replace the example hook code in the editor.
4) Update the authentication credentials:
Locate lines 2 and 3 in the hook code
Replace the placeholder OpenAI Assistant ID with yours
Replace the placeholder OpenAI API key with yours
5) Configure hook settings:
Switch to the "Settings" tab
Enter a descriptive name (e.g., "AI Product Categorizer")
Toggle the hook to "Active"
6) Click "Save" in the top-right corner to finalize your hook configuration.
Testing your hook
When properly configured, your hook should appear active on the dashboard.
To verify your setup:
Add a new product to your Shopify store
Wait approximately 30 seconds
Check the product's tags in Shopify
Review the hook's execution logs in Cloudhooks
Understanding the hook
How it works
When a new product is added to Shopify, this hook:
Receives the product data from Shopify
Creates an AI categorization task with the assistant
Waits for the AI to process the request
Retrieves the categorization results
Updates the product tags in Shopify
Detailed Process
The hook follows these steps, each consuming Cloudhooks actions:
Checks if the product has already been categorized (via a special tag) and exits if it has
Creates a thread for assistant communication (1 action)
Sends the product details to the assistant for categorization (1 action)
Initiates the assistant's processing run (1 action)
Polls the assistant at set intervals until the response is ready (minimum 1 action)
Retrieves the final categorization response (1 action)
Updates the product's tags in Shopify, including a tag to mark it as categorized (1 action)
This process consumes a minimum of 6 Cloudhooks actions per product, with potential additional actions if multiple polling attempts are needed.
Customizing and optimizing
Hook customization options
You can customize several aspects of the hook's behavior to better suit your needs:
Timeout and polling settings
The hook periodically checks the AI assistant's progress until it receives a response. You can adjust these timing parameters in lines 58-59 of the hook code:
const timeout = 20000; // Timeout is 20 seconds const interval = 3000; // Poll every 3 seconds
Important considerations:
Total execution time must stay under 30 seconds (Cloudhooks timeout limit)
More frequent polling means more API calls and higher costs
Allow enough time for both initial API call and polling
Note: Each hook execution consumes a minimum of 6 OpenAI actions. Additional actions are consumed for each polling request if the AI doesn't complete within the polling interval.
Batch Processing
Consider implementing batch processing for bulk product updates
Contact support for guidance on batch implementation