Auto-tag orders from recurring client IP addresses, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs when a user manually triggers the task. Configuration includes days of order history to scan, threshold order count per ip address, order tag to apply, run every 10 minutes, run hourly, and run daily.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
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 %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
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)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Days of order history to scan
3
Threshold order count per IP address
2