Thursday, May 4, 2023
In this two-part blog series, I'll be introducing you to a brand-new JavaScript SDK for Sitecore Discover. In this first part, we'll dive into the different implementation options for Sitecore Discover, explore the JavaScript SDK, and discuss its eventing model. This blog is the accompaniment for the YouTube video highlighting the same features. You can watch the video below:
When working with Sitecore Discover there are three main integration options:
The JavaScript SDK for Sitecore Discover is a React library, making it suitable for React-based applications and frameworks like Next.js. Don't worry if you're using another JavaScript library or language, though – you can still choose from the other integration options mentioned earlier.
Some key features of the SDK:
To start with, we'll focus on the Eventing Model. Eventing is crucial for collecting valuable information about user interactions on your site, such as page views, product details, and cart actions. This data is then used by Sitecore Discover's AI to provide personalized product experiences for users.
Several events are pre-wired in the SDK, but others require manual wiring. The good news is that once you've integrated the SDK and wired up your events, Sitecore Discover automatically begins collecting data for use in its AI-powered recommendations.
In the second part of this series, we'll explore the Component Model, which allows you to connect your event data with personalized product experiences.
To use the SDK, you'll need to wrap your application in a WidgetProvider
, which serves as the main entry point. After doing so you can begin to connect the events, some of which are automatically wired up by the SDK, while others require manual configuration. Here's a breakdown of which events fall into each category:
Automatically Wired:
Manually Wired:
For the manual events, you'll need to call the corresponding event function provided by the SDK and pass in any required event data (for example, the product SKU).
Below you can see a couple of examples of how to wire up the manual events, we’re going to use the Product Details Page View and Add to cart events as examples…
To wire up the Product Details Page View event you need to follow a couple of steps:
useEffect()
hook from React and the trackPDPView
function from the Discover SDK.trackPDPView
function with the product's SKU inside the useEffect
hook. An example of this isuseEffect(() => {
trackPDPViewEvent(product.id)
}, [product.id])
To wire up the Add to cart Action you need to follow a couple of steps:
useRouter()
hook from Next.js and the trackCartEvent
function from the SDK.trackCartEvent()
function with the product's SKU, price, and quantity after the original add-to-cart code executes.const { asPath } = useRouter()
const eventModel: ProductEventModel[] = [
{
sku: product.id,
quantity: 1,
price: product.price.value,
},
]
trackAddToCartEvent(eventModel, asPath)
By wiring these events, you'll start collecting valuable user interaction data that Sitecore Discover can use to generate personalized product recommendations.
That brings us to the end of part one of this series. Be sure to check out part two where we'll look into the component model, and how you can use the event data we've collected to create contextual product interactions in your storefront. Thanks for reading!