Demonstration, Tutorial: Query analytics data with ShopifyQL, with Mechanic.

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

Demonstration, Tutorial: Query analytics data with ShopifyQL

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.

15-day free trial – unlimited tasks

Documentation

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.

Want to Learn More?

Review the Query analytics data with ShopifyQL tutorial for helpful info and development links.

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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
shopifyql query (multiline, required)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Shopifyql query
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