How it works

Follow these 10 simple steps to start generating daily product rankings from sales data automatically

schedule icon

Schedule

App connector: Schedule • Time to complete: 0 minutes (Auto-configured)
Why this matters: This trigger runs ranking calculations on a consistent daily schedule, ensuring your best-sellers collection stays current with sales trends without manual analysis or product reordering.

This scheduled trigger runs once per day at midnight. The timing provides complete coverage of the previous day's sales and processes rankings before peak shopping hours.

Customization: You can adjust the schedule to run more frequently (like "hourly" for real-time rankings) or at different times (like early morning before staff arrives).

Setup Variables

App connector: Transform • Time to complete: 3 minutes
Why this matters: Establishes key configuration parameters including analysis time period, number of products, and target collection ID, providing centralized control over ranking behavior without modifying multiple steps.

This transform step sets up configuration variables using custom JavaScript (from transform.js). The mapping creates three variables:

  1. Days Back: Number of days to analyze for sales (e.g., 7 for weekly rankings, 30 for monthly)
  2. Limit: Maximum number of products to rank (e.g., 10 for top 10, 50 for top 50)
  3. Collection ID: The Shopify collection ID where best-sellers are featured

These variables are used throughout the workflow in SQL queries, loops, and API calls, enabling easy configuration changes in one central location.

Setup requirement: You need to configure these values in the transform mapping to match your business needs and collection ID.

Custom SQL Query (Sales Analysis)

App connector: Data • Time to complete: 0 minutes (Auto-configured)
Why this matters: Analyzes order data to calculate which products sold the most over the configured time period, providing the ranked list that determines collection ordering and metafield values.

This step runs a SQL query against the "Shopify Orders" MESA Data table.

Query breakdown:

SELECT 
    ROW_NUMBER() OVER (ORDER BY sum("Product Quantity") DESC) AS "Best Sellers Index",
    "Product ID",
    "Product Title",
    sum("Product Quantity") AS "Quantity"
FROM "Shopify Orders" 
WHERE "Order Created At" > CURRENT_DATE - INTERVAL '{{Days Back}} days' 
GROUP BY "Product ID", "Product Title"
ORDER BY "Quantity" DESC
LIMIT {{Limit}}

Query components:

  1. ROW_NUMBER(): Assigns ranking position (1, 2, 3, etc.)
  2. sum("Product Quantity"): Calculates total units sold per product
  3. WHERE clause: Filters to recent orders within configured time period
  4. GROUP BY: Aggregates sales by product
  5. ORDER BY: Sorts by quantity descending (highest sales first)
  6. LIMIT: Returns only top N products

The result is a ranked list of products with their sales quantities and ranking positions.

Data requirement: This assumes you have a MESA Data table called "Shopify Orders" populated with order data including Product ID, Product Title, Product Quantity, and Order Created At fields.

Get List of Products from Best Sellers Collection

App connector: Shopify • Time to complete: 0 minutes (Auto-configured)
Why this matters: Retrieves all products currently in the best-sellers collection to enable resetting their ranking metafields before applying new rankings, ensuring products removed from top sellers get cleared rankings.

This step fetches products from the Shopify collection specified in the Collection ID variable. It returns up to 100 products including product IDs, titles, and existing metafields. This list is used to reset all rankings to zero before applying new rankings.

Why reset existing rankings: Products that were previously in the top sellers but have dropped out need their rankings cleared. This step ensures clean slate before new rankings apply.

Loop over Products in Best Sellers Collection

App connector: Loop • Time to complete: 0 minutes (Auto-configured)
Why this matters: Processes each product currently in the collection individually to reset ranking metafields, ensuring no products retain outdated rankings from previous analysis cycles.

This loop iterates through all products retrieved from the collection. For every product, the loop executes a metafield update that sets the ranking to zero. This clears old rankings before new rankings apply.

Set Best Sellers Index Product Metafield to 0

App connector: Shopify • Time to complete: 0 minutes (Auto-configured)
Why this matters: Resets each product's ranking metafield to zero, clearing previous rankings to ensure only current top sellers display rank numbers and dropped products show as unranked.

This step sets a product metafield to zero for each product in the collection.

Metafield structure:

  1. namespace: "custom"
  2. key: "best_sellers_index"
  3. type: "number_integer"
  4. value: "0"

This metafield can be displayed on product pages, collection pages, or used for sorting/filtering. Resetting to zero ensures products that fall out of top sellers don't retain old rankings.

Loop over Best Selling Products

App connector: Loop • Time to complete: 0 minutes (Auto-configured)
Why this matters: Processes each top-selling product from the sales analysis to set accurate ranking metafields and collection positions based on current sales performance.

This loop iterates through the ranked products from the SQL query. For every best-seller, the loop executes product retrieval, metafield update, and collection reordering. This applies the new rankings.

Retrieve Product

App connector: Shopify • Time to complete: 0 minutes (Auto-configured)
Why this matters: Fetches complete product information for each best-seller using the Product ID from sales analysis, providing the product data needed for metafield updates and GraphQL mutations.

This step retrieves the product record using the Product ID from the SQL results. It returns complete product data including product ID (in both REST and GraphQL formats), title, variants, and existing metafields.

Set Best Sellers Index Product Metafield By Order

App connector: Shopify • Time to complete: 0 minutes (Auto-configured)
Why this matters: Updates each product's ranking metafield with its actual sales-based position (1 for top seller, 2 for second, etc.), enabling theme templates to display ranking badges or sort products by performance.

This step sets the product metafield to the ranking position from the SQL query.

Metafield structure:

  1. namespace: "custom"
  2. key: "best_sellers_index"
  3. type: "number_integer"
  4. value: The "Best Sellers Index" from SQL (1, 2, 3, etc.)

This metafield can be referenced in Shopify themes to display "#1 Best Seller" badges, sort products, or create filtered views.

code icon

Sort Products in Best Sellers Collection

App connector: Code • Time to complete: 1 minute
Why this matters: Reorders the product's position within the collection to match its sales ranking, ensuring the collection displays products in best-seller order when customers browse.

This custom JavaScript step uses Shopify's GraphQL API to reorder products in the collection.

// GraphQL mutation for collection reordering
let query = `mutation collectionReorderProducts($id: ID!, $moves: [MoveInput!]!) {
  collectionReorderProducts(id: $id, moves: $moves) {
    job { id }
    userErrors { field message }
  }
}`;

// Execute mutation with collection ID and new position
const response = ShopifyGraphql.send(query, {
  "id": "gid://shopify/Collection/" + Collection ID,
  "moves": [{
    "id": "gid://shopify/Product/" + product ID,
    "newPosition": ranking position
  }]
});

The code constructs a GraphQL mutation that moves the product to the position matching its ranking (product ranked #1 moves to position 1, #2 to position 2, etc.).

GraphQL requirement: This uses Shopify's collectionReorderProducts mutation which is more reliable than REST API for collection ordering.

*Required: These following must be installed before using this workflow.

Make it your own

Customize this workflow even further:

Add revenue-based rankings
Modify the SQL query to rank by total revenue (sum of line item prices) instead of quantity sold, prioritizing high-value products over high-volume items.
Create multiple ranking periods
Duplicate the workflow with different time periods (7 days for weekly, 30 days for monthly, 90 days for quarterly) updating different metafields, enabling multiple best-seller views.
Filter by product type or collection
Add WHERE clauses to the SQL query that filter by product type, vendor, or tags, creating category-specific best-seller collections like "Best Selling T-Shirts" or "Best Selling Supplements".
Send ranking reports
Add email or Slack steps after the analysis that send formatted reports of top sellers with rankings, quantities, and revenue to merchandising teams.

Frequently asked questions

How do I change the time period for rankings?
Edit the "Setup Variables" transform step and change the "Days Back" value in the mapping. For example, set to 7 for weekly rankings, 30 for monthly, or 90 for quarterly best-sellers.
Can I display the ranking number on product pages?
Yes, the best_sellers_index metafield can be accessed in Shopify themes. Use Liquid like {{ product.metafields.custom.best_sellers_index }} to display the ranking, or create conditional logic to show "#1 Best Seller" badges.
What happens to products that drop out of the top rankings?
They remain in the collection with their ranking metafield set to 0 (from the reset step). To automatically remove dropped products, you'd need to add logic that compares the current collection to the new rankings and removes products not in the top N.
What is a template?
Templates are pre-made workflows by our team of experts. Instead of building a workflow from scratch, these have all the steps needed to complete the task.
Can I personalize a template?
Yes! Every step can be customized to meet your exact requirements. Additionally, you can even add more steps and make it more sophisticated.
Are templates free?
Yes! Our entire library containing hundreds of templates are free to use and customize to your exact needs.

Ready to start generating daily product rankings from sales data automatically?

Join thousands who've automated their work and saved an average of 3.5 hours every week.

Start with this template — It's free
7-day free trial • 12 min setup • Cancel anytime