Send an event to Klaviyo when a fulfillment is delayed, with Mechanic.

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

Send an event to Klaviyo when a fulfillment is delayed

This task will watch for new order fulfillments, waiting a configurable number of days after each one. When the waiting period is over, if the fulfillment's status matches an item in the list, this task will send an event to Klaviyo, allowing you to use Klaviyo to contact the customer appropriately.

Runs Occurs whenever an order is fulfilled, with a 5 day delay. Configuration includes qualifying fulfillment statuses, klaviyo api token, klaviyo event name, days to wait before checking, and test mode.

15-day free trial – unlimited tasks

Documentation

This task will watch for new order fulfillments, waiting a configurable number of days after each one. When the waiting period is over, if the fulfillment's status matches an item in the list, this task will send an event to Klaviyo, allowing you to use Klaviyo to contact the customer appropriately.

Configure the "Qualifying fulfillment statuses" option with values from the shipment_status property on Shopify's Fulfillment resource documentation.

Enable "Test mode" to make available a "Run task" button - press this button to have Mechanic send a test event to Klaviyo, allowing you to verify that everything's connected properly.

Note: This task has been updated to work with a newer Klaviyo API than the v1 and v2 versions being sunset at the end of June 2024. Specifically, it uses revision v2023-06-15.

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
{% if options.test_mode__boolean %}
  mechanic/user/trigger
{% else %}
  shopify/orders/fulfilled+{{ options.days_to_wait_before_checking__number_required | default: 5 }}.days
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
qualifying fulfillment statuses (array, required), klaviyo api token (required), klaviyo event name (required), days to wait before checking (number, required), test mode (boolean)
Code
{% assign qualifying_fulfillment_statuses = options.qualifying_fulfillment_statuses__array_required %}
{% assign klaviyo_api_token = options.klaviyo_api_token__required %}
{% assign klaviyo_event_name = options.klaviyo_event_name__required %}
{% assign days_to_wait_before_checking = options.days_to_wait_before_checking__number_required %}
{% assign test_mode = options.test_mode__boolean %}

{% if test_mode or event.preview %}
  {% capture order_json %}
    {
      "name": "#1234",
      "email": "customer@example.com",
      "fulfillments": [
        {
          "shipment_status": {{ qualifying_fulfillment_statuses.first | json }}
        }
      ]
    }
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% else %}
  {% assign order = order.reload %}
{% endif %}

{% assign fulfillments_qualify = false %}

{% for fulfillment in order.fulfillments %}
  {% if qualifying_fulfillment_statuses contains fulfillment.shipment_status %}
    {% assign fulfillments_qualify = true %}
  {% endif %}
{% endfor %}

{% comment %}
  -- NOTE: Due to a bug in more recent versions of the Klaviyo API, this task explicitly uses revision v2023-06-15. Klaviyo is using a 2 year API release cycle, so this task will be updated with the latest API revision once the issue is resolved.
{% endcomment %}

{% if fulfillments_qualify %}
  {% action "http" %}
    {
      "method": "post",
      "url": "https://a.klaviyo.com/client/events/?company_id={{ klaviyo_api_token }}",
      "headers": {
        "revision": "2023-06-15"
      },
      "body": {
        "data": {
          "type": "event",
          "attributes": {
            "metric": {
              "name": {{ klaviyo_event_name | json }}
            },
            "profile": {
              "$email": {{ order.email | json }}
            },
            "properties": {
              "order_name": {{ order.name | json }}
            }
          }
        }
      },
      "verify": "true",
      "error_on_5xx": "true"
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Qualifying fulfillment statuses
["in_transit", "failure", "attempted_delivery"]
Klaviyo event name
Fulfillment delayed or failed
Days to wait before checking
5