Auto-tag orders from recurring client IP addresses
Use this task to regularly scan for repeat orders from a single IP address, by tagging any orders from an IP address that's placed more than a configurable threshold of orders in the last x days of order history.
Try this taskWhen it runs
Occurs when a user manually triggers the task
What you choose
- Days of order history to scan
- Threshold order count per IP address
- Order tag to apply
Show 3 more
- Run every 10 minutes
- Run hourly
- Run daily
Documentation
Use this task to regularly scan for repeat orders from a single IP address, by tagging any orders from an IP address that's placed more than a configurable threshold of orders in the last x days of order history.
This task works via manual or scheduled scans of order history - it does not run immediately when orders are created. Instead, whenever it runs, it scans the last x days of order history, tagging orders that come from an IP address whose order count has met or exceeded your threshold.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
mechanic/user/trigger
{% if options.run_every_10_minutes__boolean %}
mechanic/scheduler/10min
{% elsif options.run_hourly__boolean %}
mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
How task subscriptions work
Option types and flags
- Days of order history to scan (number)
- Threshold order count per IP address (number, required)
- Order tag to apply (required)
- Run every 10 minutes (boolean)
- Run hourly (boolean)
- Run daily (boolean)
Task code
{% assign orders_query = nil %}
{% if options.days_of_order_history_to_scan__number != blank %}
{% assign days_s = 60 | times: 60 | times: 24 | times: options.days_of_order_history_to_scan__number %}
{% assign beginning_date = "now" | date: "%s" | minus: days_s | date: "%FT%T%:z" %}
{% assign orders_query = beginning_date | json | prepend: "processed_at:>=" %}
{% endif %}
{% log orders_query: orders_query %}
{% assign cursor = nil %}
{% assign orders_by_ip = hash %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
after: {{ cursor | json }}
query: {{ orders_query | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
tags
clientIp
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"orders": {
"edges": [
{% for n in (0..options.threshold_order_count_per_ip_address__number_required) %}
{
"node": {
"id": "gid://shopify/Order/1234567890",
"tags": [],
"clientIp": "0.0.0.0"
}
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for order_edge in result.data.orders.edges %}
{% assign order = order_edge.node %}
{% if order.clientIp == blank %}
{% continue %}
{% endif %}
{% if orders_by_ip[order.clientIp] == nil %}
{% assign orders_by_ip[order.clientIp] = array %}
{% endif %}
{% assign ip_count = orders_by_ip[order.clientIp].size %}
{% assign orders_by_ip[order.clientIp][ip_count] = order %}
{% endfor %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% log orders_by_ip: orders_by_ip %}
{% assign order_ids_to_tag = array %}
{% for pair in orders_by_ip %}
{% assign ip = pair[0] %}
{% assign orders = pair[1] %}
{% if orders.size >= options.threshold_order_count_per_ip_address__number_required %}
{% for order in orders %}
{% unless order.tags contains options.order_tag_to_apply__required %}
{% assign order_ids_to_tag[order_ids_to_tag.size] = order.id %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
{% for id in order_ids_to_tag %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ id | json }}
tags: {{ options.order_tag_to_apply__required | json }}
) {
node {
... on Order {
name
processedAt
clientIp
tags
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% if event.preview %}
{% break %}
{% endif %}
{% endfor %}
Mechanic Liquid reference
Default option values
- Days of order history to scan
3- Threshold order count per IP address
2- Run every 10 minutes
false- Run hourly
false- Run daily
false