Hi people,
I’m playing with GraphQL, I’m somewhat new with this. The thing is that I need to replicate a ShipifyQL query into a GraphQL query, that thing is that it contains UTM parameters of orders, and for what I know those can’t be directly accesible via GraphQL, is there a way to this?
This is the query that I want to transform:
FROM sales
SHOW net_sales, customers, net_items_sold, orders
GROUP BY day, billing_country, new_or_returning_customer, order_utm_source, order_utm_medium, order_utm_campaign, order_utm_content, order_utm_term, product_type, sales_channel, billing_region, shipping_region, shipping_country
WITH TOTALS
SINCE 2025-02-04
UNTIL 2025-02-18
ORDER BY day ASC
LIMIT 1000
VISUALIZE orders
THANKS
Accessing UTM parameters directly isn’t possible because they aren’t exposed as fields in the GraphQL schema. However, you can work around this limitation by using available fields and custom attributes. Here’s a general approach:
- Use Metafields: If you have control over how UTM parameters are stored, consider storing them in metafields when orders are created. Metafields are accessible through the Shopify GraphQL API.
- Custom Attributes: If you’re using a custom app, you can add UTM parameters as custom attributes to orders when they are created or updated.
- API Extension: If possible, extend your backend APIs to capture and store UTM parameters in a way that makes them accessible via GraphQL.
- Analytics Tools: Consider using Shopify’s built-in analytics or external analytics tools that can track UTM parameters and provide insights.
Example GraphQL Query
Here’s a basic example query structure in Shopify’s GraphQL to retrieve order data:
{
orders(first: 100) {
edges {
node {
id
createdAt
billingAddress {
country
region
}
shippingAddress {
country
region
}
customer {
firstName
lastName
}
lineItems(first: 10) {
edges {
node {
product {
productType
}
quantity
}
}
}
tags
metafields(first: 10) {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}
Steps that may help Implement
- Check for Existing Metafields: See if UTM parameters are already stored as metafields.
- Add Metafields: If not, create a process to add UTM data to orders, which can then be queried.
- Query Metafields: Use the GraphQL query to access these metafields along with order data.
If you’re using a tool or service that adds UTM data to orders, ensure it’s set up correctly to store this information in a way that makes it accessible via GraphQL.
Hopfully this will help you.
1 Like