Skip to content
LogoLogo

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-.

PredicateReturns
shopify-is-homepagetrue on /.
shopify-is-product-pagetrue when pickedConfig.product has a handle.
shopify-get-product-handleThe handle of pickedConfig.product, or false.
shopify-is-collection-pagetrue when pickedConfig.collection has a handle.
shopify-get-collection-handleThe handle of pickedConfig.collection, or false.
shopify-is-pagetrue on /pages/<handle>.
shopify-get-page-handleThe page handle from the path.
shopify-is-blog-pagetrue on /blogs/<handle>.
shopify-get-blog-handleThe blog handle from the path.
shopify-is-article-pagetrue on /blogs/<blog>/<article>.
shopify-get-article-handleThe article handle from the path.
shopify-is-app-proxy-pagetrue on /a/, /apps/, /community/ and /tools/ paths.
shopify-get-app-proxy-handleThe first path segment after the app proxy prefix.
shopify-is-account-pagetrue on /account/<path>.
shopify-is-login-pagetrue on /account/login.
shopify-is-register-pagetrue on /account/register.
shopify-is-cart-pagetrue on /cart/<path>.
shopify-is-checkout-pagetrue 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:

AttributeValue
picked.idThe 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.

FunctionReturns
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

ImportFunctions
@picked/shopify/ajax-apigetProduct, getProductRecommendations, getPredictiveSearch, getCart, addToCart, updateCart, changeCart, clearCart, getShippingRates
@picked/shopify/json-apigetCollection, getCollectionProducts
@picked/shopify/storefront-apicreateStorefrontApiClient, getStorefrontApiClient, cartDiscountCodesUpdate
@picked/shopify/picked-appgetProductMetadata, getCollectionMetadata
@picked/shopify/utilsgetRequest, 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.