Shopify Query Language
The query language of the Analytics API is called ShopifyQL. A valid ShopifyQL query consists of parts similar to a valid SQL request. Let's look at an example:
SHOW sum(pageview_count) FROM visits SINCE -7d UNTIL -1d
This query will fetch the total pageviews for the last 7 days. The result would look something like:
Fetching data (SHOW)
There are a number of ways to select, filter and aggregate data using ShopifyQL.
Each schema is comprised of many columns.
SHOW utm_campaign_name, utm_campaign_term, utm_campaign_source
A number of functions exist to aid with gathering or aggregating data.
|sum||returns the summation of rows scanned|
|min||returns the minimum value of rows scanned|
|max||returns the maximum value of rows scanned|
|count||returns the count of objects|
|DISTINCT||returns the unique count of rows|
|avg||returns the average value of rows scanned|
|median||median value of results|
|any||returns one value from the results|
Commonly used aggregation expressions are provided as named aliases.
SHOW total_pageviews vs
Named aliases on the
Result columns can be explicitly labeled with expression AS label, otherwise a label derived from the expression will be assigned which can often be a bit unwieldy.
SHOW sum(pageview_count) AS pageviews
ShopifyQL provides support for conditions within the
SHOW statement. These can be used to filter rows from inclusion into aggregation functions. Aggregate conditions make it possible to get results back in a single query that would usually require two or more.
Selecting schemas (FROM)
Schemas are selected by the key word
FROM. This works in a similar fashion to SQL. The following query is using the
SHOW sum(pageview_count) FROM visits
Shopify provides several schemas that you can fetch data from:
WHERE clause indicates the condition or conditions that rows must satisfy to be selected.
Unlike aggregate conditions, which only filter for one column of the result.
WHERE conditions filter for all columns that will be returned.
WHERE clauses can simplify queries, such as below:
Using aggregate conditions
Valid operators are:
Grouping by results (BY)
BY statement can be used to group the result-set by one or more columns. In the following example, we use the BY clause to group data based on day. Note that there is no row for 2016-02-01. This is because there is no data for that day. This behaviour differs from that of the OVER clause explained below.
Total pageviews by day (last 7 days)
SHOW sum(pageview_count) BY day(timestamp) FROM visits SINCE -7d UNTIL -1d
Grouping with backfill (OVER)
OVER clause can also be used to group result sets. The difference is that missing rows will be backfilled. In the following example, we use the OVER clause to group data based on day. Note that there is a backfilled row for 2016-02-01.
Total pageviews per day (last 7 days)
SHOW sum(pageview_count) OVER day(timestamp) FROM visits SINCE -7d UNTIL -1d
It is also possible to combine
Total pageviews by browser per day (last 7 days)
SHOW sum(pageview_count) OVER day(timestamp) BY ua_browser FROM visits SINCE -7d UNTIL -1d
Time functions can be used in conjuction with
OVER to bucket data into a specific period. For example, you can bucket
pageview_count by month using the following query.
SHOW sum(pageview_count) OVER month(timestamp) AS month
|Function Name||Description||Output Type||Output on
|year||Rounds the timestamp down to the nearest year||Timestamp||
|quarter||Rounds the timestamp down to the nearest quarter||Timestamp||
|month||Rounds the timestamp down to the nearest month||Timestamp||
|week||Rounds the timestamp down to the nearest week (starting on monday)||Timestamp||
|day||Rounds the timestamp down to the nearest day||Timestamp||
|three_hour||Rounds the timestamp down to the nearest hour that is a multiple of 3||Timestamp||
|hour||Rounds the timestamp down to the nearest hour||Timestamp||
|minute||Rounds the timestamp down to the nearest minute||Timestamp||
|hour_of_day||Extracts the numerical hour of the day from the timestamp||Number||
|day_of_week||Extracts the literal day of the week from the timestamp||String||
|day_of_month||Extracts the literal day of the month from the timestamp||String||
|day_of_year||Extracts the numerical day of the year from the timestamp||Number||
|week_of_year||Extracts the numerical week of the year from the timestamp||Number||
|month_of_year||Extracts the literal month of the year from the timestamp||String||
|weekday||Returns a boolean to indicate if the day is a weekday||Boolean||
Time Range (SINCE/UNTIL)
When writing ShopifyQL queries, the use of time/date constraints is strongly recommended. Not including the
UNTIL clauses will result in a query that scans a shops entire dataset, which can include years of data and take quite a long time.
Explicit dates are supported, such as
2016-01-28. There is also support for relative dates.
|d||day eg: SINCE -7d|
|w||week eg: SINCE -3w|
|m||month eg: SINCE -2m|
|y||year eg: SINCE -1y|
SINCE will be taken from the beginning of the day, while
UNTIL will be to the end of the day.
For example. Assume today is
SINCE -7d UNTIL -1d would be equivalent to
SINCE 2016-01-28 UNTIL 2016-02-03
You can use the following expression functions to manipulate column values. These functions are outlined below:
||Collapses repeating whitespace into a single space while removing leading and trailing whitespace|
||Returns the titleized version of the input|
||Returns the host's domain without the top level domain (e.g. `.com`, `.org`)|
||Returns the host's domain and top level domain from the URL-string|
||Returns the hostname of a URL|
||Returns a string including the host name and path, without a trailing slash|
||Returns a [canonical form](https://www.shopify.ca/partners/blog/canonical-urls) of an address field|
||Returns a string where all instances of '.' are replaced with ' '|
||Returns the input number as a percentage|
||Concatenates multiple arguments into a single string|
||Returns the input number rounded to a certain precision|
||Returns the sum of arg1 and arg2|
||Returns the difference of arg1 and arg2|
||Returns the product of arg1 and arg2|
||Returns the quotient of arg1 and arg2|