LaunchDarkly Shopify Pixel
Overview
This topic explains how to create a Shopify Custom Pixel that reports checkout events to LaunchDarkly. It uses the JavaScript SDK’s track
and Shopify’s analytics.subscribe
methods to capture checkout_started
and checkout_completed
events and report them to LaunchDarkly.
To learn more about the LaunchDarkly JavaScript SDK before you get started, read JavaScript SDK reference guide.
To learn how to configure a Shopify Custom Pixel, read Shopify’s documentation.
Example configuration code
Here is an example of code to configure a Shopify Custom Pixel that reports checkout events to LaunchDarkly. Depending on your specific implementation, you may need to modify the code to better fit your needs.
This code:
- loads the LaunchDarkly SDK asynchronously
- instructs
waitForLD(fn)
to poll untilldClient.track
exists - subscribes to
checkout_started
andcheckout_completed
- uses the event’s client ID as the LaunchDarkly context key, and falls back to an anonymous context if this is not available
- initializes or re-identifies the LaunchDarkly client inside the handler, then calls
track()
Here’s how:
Best practices and considerations
If you decide to modify the code above, we recommend the following best practices:
- Load the SDK before calling
LDClient.initialize()
. Using<script async>
tags may result in errors. - Do not use modified JavaScript. Shopify’s Pixel environment can be restrictive, so for best results, do not rely on global Liquid variables.
- Poll, do not buffer. Buffering events until the SDK is read can add unnecessary complexity. Polling for readiness is simpler.
If you have unexpected results, remember that Shopify’s analytics.subscribe
fires as soon as the Pixel sandbox starts, which can be before the LaunchDarklySDK script has parsed.