Shopify
@picked/shopify connects the Picked client to a Shopify storefront. It adds Shopify
predicates, writes Picked data to the cart, and gives helpers for the Shopify storefront
APIs.
import { createClient } from 'picked'
import { shopifyPlugin } from '@picked/shopify/plugins/shopify'
const client = createClient({
projectId: 'your-project-id',
plugins: [shopifyPlugin()],
})The plugin has no options. The helpers read window.Shopify and window.pickedConfig.
The Picked Shopify app puts window.pickedConfig on the storefront.
Predicates
The plugin registers these predicates. Each name starts with shopify-.
| Predicate | Returns |
|---|---|
shopify-is-homepage | true on /. |
shopify-is-product-page | true when pickedConfig.product has a handle. |
shopify-get-product-handle | The handle of pickedConfig.product, or false. |
shopify-is-collection-page | true when pickedConfig.collection has a handle. |
shopify-get-collection-handle | The handle of pickedConfig.collection, or false. |
shopify-is-page | true on /pages/<handle>. |
shopify-get-page-handle | The page handle from the path. |
shopify-is-blog-page | true on /blogs/<handle>. |
shopify-get-blog-handle | The blog handle from the path. |
shopify-is-article-page | true on /blogs/<blog>/<article>. |
shopify-get-article-handle | The article handle from the path. |
shopify-is-app-proxy-page | true on /a/, /apps/, /community/ and /tools/ paths. |
shopify-get-app-proxy-handle | The first path segment after the app proxy prefix. |
shopify-is-account-page | true on /account/<path>. |
shopify-is-login-page | true on /account/login. |
shopify-is-register-page | true on /account/register. |
shopify-is-cart-page | true on /cart/<path>. |
shopify-is-checkout-page | true on /checkout/<path>. |
See Predicates for how a condition reads a predicate.
Cart attributes
In a browser with window.Shopify.analytics, the plugin writes these cart attributes
through the AJAX API of the Shopify cart:
| Attribute | Value |
|---|---|
picked.id | The Picked ID of the visitor. The SDK keeps it in browser storage across visits. |
picked.is_user | 'true' or 'false': the result of the condition of the client. |
picked.features.<feature ID> | The bucket of each resolved feature. |
The plugin writes the attributes again on the initialize, session, user_decide,
feature_decide and experience_initialize events. It then reads the cart back after 2
seconds. If a different app changed an attribute, the plugin writes it again, up to 3
times.
On the session event, the plugin also publishes picked:session with { pickedId, id }
through Shopify.analytics.publish. A Shopify web pixel can subscribe to this event. The
id field is deprecated. Use pickedId.
Storefront data
@picked/shopify/plugins/shopify also gives readers for window.pickedConfig. Each
returns undefined outside a browser.
| Function | Returns |
|---|---|
getPickedConfig() | The full window.pickedConfig object. |
getPickedConfigCustomer() | The customer that is logged in. |
getPickedConfigProduct() | The product of the current page. |
getPickedConfigCollection() | The collection of the current page. |
getPickedConfigExperiments() | The experiments of the Picked Shopify app. |
getPickedConfigFeatureExperiments() | Each enabled experiment as a value for the features option of createClient(). |
getPickedConfigStorefrontApiToken() | The public Storefront API token. |
getShopify() | The window.Shopify object. |
Storefront APIs
| Import | Functions |
|---|---|
@picked/shopify/ajax-api | getProduct, getProductRecommendations, getPredictiveSearch, getCart, addToCart, updateCart, changeCart, clearCart, getShippingRates |
@picked/shopify/json-api | getCollection, getCollectionProducts |
@picked/shopify/storefront-api | createStorefrontApiClient, getStorefrontApiClient, cartDiscountCodesUpdate |
@picked/shopify/picked-app | getProductMetadata, getCollectionMetadata |
@picked/shopify/utils | getRequest, postRequest |
Each request goes to the storefront origin, below Shopify.routes.root. A response that
is not ok throws an Error. The message of the error is the status text.
createStorefrontApiClient() needs the Storefront API token in window.pickedConfig. It
throws an Error when the token is not there.
The helpers of @picked/shopify/picked-app read metafields through the
/apps/picked app proxy of the Picked Shopify app.
