Detect Coupon Code Abuse and Receive Slack Alerts

8 min setup
No coding required
Runs automatically

Safeguard your promotions by monitoring for coupon code abuse with this MESA workflow template. It compares daily order totals and discounts, using AI analysis to identify any unusually high discount rates. If potential abuse is detected, you’ll receive a Slack notification, allowing you to respond quickly. Note: This template requires the “Organize Shopify Orders by Order Date” template to be enabled.

database icon
Record Created
Custom SQL Query
Format order total by day including discounts
Compare discount rates to identify coupon abuse
Filter: Was coupon abuse detected?
slack logo icon
Send Slack Message

You're in good company

"MESA has been a game changer for us. And, if you ever get stuck, their support team is always super helpful."

  • Ico star
  • Ico star
  • Ico star
  • Ico star
  • Ico star
PetFriendly

"It's like Zapier but exactly designed for Shopify. I have been able to complete all the workflows that I've needed."

  • Ico star
  • Ico star
  • Ico star
  • Ico star
  • Ico star
Zailys

"The MESA team has been amazing at helping us set up our automations. We would highly recommend this app!"

  • Ico star
  • Ico star
  • Ico star
  • Ico star
  • Ico star
Rothy's

How it works

6 steps to start receiving automatic alerts when coupon code abuse is detected

database icon

Record Created

App connector: Data • Time to complete: 0 minutes (Auto-configured)
Why this matters: This trigger regularly monitors your order database to detect new records, providing the foundation for hourly discount analysis without requiring constant manual data checks.

This trigger checks your "Shopify Orders" MESA Data table every hour for new records using the schedule. When new order data is detected, the workflow activates and begins the discount analysis process.

Critical requirement: You must have a MESA Data table named "Shopify Orders" that contains order information including "Created At", "Total Price", and "Total Discount" columns. This table is typically populated by another workflow that syncs Shopify orders to MESA Data storage.

Custom SQL Query

App connector: Data • Time to complete: 1 minute
Why this matters: Aggregates your order and discount data by day to calculate discount percentages, transforming raw order records into actionable metrics that reveal usage patterns and potential abuse.

This step runs a custom SQL query against the "Shopify Orders" MESA Data table to aggregate order data by day. The query calculates:

  1. Date: Orders grouped by date (formatted as yyyy-mm-dd)
  2. total: Sum of Total Price for all orders that day
  3. total_discounts: Sum of Total Discount for all orders that day
  4. percentage: Discount rate calculated as total_discounts divided by total (rounded to 2 decimal places)

SQL query breakdown:

SELECT
  TO_CHAR("Created At", 'yyyy-mm-dd'),  -- Group by date
  SUM("Total Price") AS total,           -- Daily revenue
  SUM("Total Discount") AS total_discounts,  -- Daily discounts
  ROUND(SUM("Total Discount") / SUM("Total Price"), 2) AS percentage  -- Discount rate
FROM "Shopify Orders"
GROUP BY TO_CHAR("Created At", 'yyyy-mm-dd')
ORDER BY TO_CHAR("Created At", 'yyyy-mm-dd');

The results return rows showing each day's totals and discount percentage, enabling trend analysis to identify spikes in discount usage.

Format order total by day including discounts

App connector: Code • Time to complete: 1 minute
Why this matters: Transforms the SQL query results into a clean JSON format that AI can easily analyze, ensuring accurate interpretation of discount patterns without data formatting errors.

This custom JavaScript step formats the SQL results into a JSON string. The code takes the array of daily discount data from the previous step and converts it to a JSON string format that's optimal for AI analysis. The formatted data is returned as json_results containing the structured discount information.

// Creates new payload object
let newPayload = {};
// Converts SQL results to JSON string
newPayload.json_results = JSON.stringify(prevResponse);
// Passes formatted data to next step
Mesa.output.next(newPayload);

This formatting step ensures AI receives data in a consistent, parseable format regardless of variations in SQL output.

Compare discount rates to identify coupon abuse

App connector: AI • Time to complete: 3 minutes
Why this matters: Uses AI to intelligently detect abnormal discount patterns by comparing current rates against your baseline average, providing smart analysis that adapts to your specific business patterns rather than rigid thresholds.

This AI step analyzes the formatted discount data to detect abuse patterns. The AI receives the JSON results containing daily discount rates and compares them against a baseline assumption of 10% typical discount rate.

AI instructions:

  1. Compare each day's discount rate against the 10% baseline
  2. Determine if any recent discount rates are significantly higher than typical
  3. Respond with only "Yes" (abuse detected) or "No" (normal patterns)

The temperature setting of "1" balances between consistent analysis and nuanced pattern recognition. The AI considers context like whether high discount days are isolated incidents or sustained patterns, and whether the increase is dramatic enough to indicate abuse versus normal promotional variation.

Baseline customization: The default assumes 10% average discount rate. You can modify the prompt to specify your actual typical discount rate for more accurate detection.

Filter: Was coupon abuse detected?

App connector: Filter • Time to complete: 1 minute
Why this matters: Acts as a gatekeeper that only allows the workflow to continue and send alerts when AI has confirmed suspicious activity, preventing alert fatigue from false positives or normal discount activity.

This filter checks if the AI response equals "Yes". If true (abuse detected), the workflow proceeds to send a Slack alert. If false (AI responded "No"), the workflow stops here without sending a notification. This filtering ensures your team only receives alerts for genuine concerns, not routine discount activity.

Alert precision: The AI's analysis combined with this filter prevents notification fatigue by distinguishing between normal promotional activity (like legitimate sales events) and suspicious discount abuse patterns.

slack logo icon

Send Slack Message

App connector: Slack • Time to complete: 1 minute
Why this matters: Delivers immediate notification to your team when coupon abuse is detected, enabling quick investigation and response to protect revenue before the situation escalates.

This step sends a Slack message to your configured channel. Configuration: You must set two values during setup:

  1. Slack channel: Select the channel where alerts should post (like #fraud-alerts, #finance, or #operations). First invite the MESA Slack app to the channel by typing @MESA and clicking Invite.
  2. Alert message: Customize the notification text. Default is "Coupon code abuse detected" but you can enhance with additional context, instructions for investigation, or formatting for visibility.

Message customization examples:

  1. "🚨 Coupon abuse detected! Discount rate is significantly higher than normal baseline. Check recent orders for suspicious patterns."
  2. "⚠️ Unusual discount activity detected. Review dashboard for details."
  3. Include links to your order dashboard or analytics tools

The message posts immediately when abuse is detected, ensuring your team can investigate while patterns are recent.

*Required: The following must be used with this workflow.

Ready to set this up? It only takes 8 minutes.

Our support team will even help you personalize this workflow for free.

Get started →

Make it your own!

Customize this workflow even further:

Include detailed discount data in the alert
Modify the Slack message to include specific numbers from {{custom.json_results}}, showing which days had high discount rates and the exact percentages to provide immediate context for investigation.
Adjust the baseline discount threshold
Change the 10% baseline in the AI prompt to match your actual average discount rate, or create different thresholds for different time periods (like higher baselines during known sale events).
Add email alerts for critical spikes
Connect an email step alongside or after the Slack message to notify leadership or finance teams when discount rates exceed severely abnormal levels, ensuring critical issues reach the right people.
Store abuse incidents in Google Sheets
Add a Google Sheets step after the filter to log every detected abuse instance with timestamp, discount percentage, and AI analysis, creating a historical record for pattern analysis and reporting.

Common questions

How do I set up the "Shopify Orders" data table that this workflow requires?

Can I change the 10% baseline discount rate to match my business?

What if I want to check more or less frequently than every hour?

Ready to start receiving automatic alerts when coupon code abuse is detected?

7-day free trial • 8 min setup • Cancel anytime

Need help? Our automation experts will help you personalize this workflow for free. Contact support