Mechanic is a development and ecommerce automation platform for Shopify. :)
This task demonstrates how to structure Mechanic Liquid to send and receive the results of a ShopifyQL query. The default query has been adapted from the Total sales by vendor report available in Shopify Analytics.
Runs Occurs when a user manually triggers the task. Configuration includes shopifyql query.
This task demonstrates how to structure Mechanic Liquid to send and receive the results of a ShopifyQL query. The default query has been adapted from the Total sales by vendor report available in Shopify Analytics.
The task uses a task options field for saving/editing the ShopifyQL query syntax, however, this syntax could be captured directly in the task code instead. Either way, a custom task will need to adapt to the field names returned by that specific ShopifyQL query.
Review the Query analytics data with ShopifyQL tutorial for helpful info and development links.
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?)
mechanic/user/trigger
{% assign shopifyql_query = options.shopifyql_query__multiline_required %}
{% capture query %}
{
shopifyqlQuery(query: {{ shopifyql_query | json }}) {
tableData {
columns {
displayName
name
dataType
subType
}
rows
}
parseErrors
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"shopifyqlQuery": {
"tableData": {
"columns": [
{
"displayName": "Product vendor",
"name": "product_vendor",
"dataType": "STRING",
"subType": null
},
{
"displayName": "Net items sold",
"name": "net_items_sold",
"dataType": "INTEGER",
"subType": null
},
{
"displayName": "Gross sales",
"name": "gross_sales",
"dataType": "MONEY",
"subType": null
},
{
"displayName": "Discounts",
"name": "discounts",
"dataType": "MONEY",
"subType": null
},
{
"displayName": "Returns",
"name": "returns",
"dataType": "MONEY",
"subType": null
},
{
"displayName": "Net sales",
"name": "net_sales",
"dataType": "MONEY",
"subType": null
},
{
"displayName": "Taxes",
"name": "taxes",
"dataType": "MONEY",
"subType": null
},
{
"displayName": "Total sales",
"name": "total_sales",
"dataType": "MONEY",
"subType": null
}
],
"rows": [
{
"product_vendor": "ACME",
"net_items_sold": "20",
"gross_sales": "800.0",
"discounts": "0",
"returns": "-24.0",
"net_sales": "776.00",
"taxes": "7.76",
"total_sales": "783.76"
},
{
"product_vendor": "Company 123",
"net_items_sold": "2",
"gross_sales": "86.55",
"discounts": "-30.25",
"returns": "0",
"net_sales": "56.3",
"taxes": "5.63",
"total_sales": "61.93"
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign columns = result.data.shopifyqlQuery.tableData.columns %}
{% assign rows = result.data.shopifyqlQuery.tableData.rows %}
{% log rows_count: rows.size %}
{% log rows: rows %}
{% log columns: columns %}
FROM sales SHOW net_items_sold, gross_sales, discounts, returns, net_sales, taxes, total_sales WHERE product_vendor IS NOT NULL GROUP BY product_vendor DURING last_year ORDER BY total_sales DESC