SitecoreSitecore CDP

React’s higher order component to stream events to Sitecore CDP

Sitecore Customer Data Platform (CDP) tracks customers’ data across all the channels. This data can later be used by Sitecore Personalize to run personalization for a single or a segment of customers. If you are a React developer and planning to integrate your website to Sitecore’s CDP(f.k.a Boxever), this post will touch base on one of the cleanest ways of achieving this. Tracking a customer’s activities on your website for analytics and personalization is a cross-cutting concern in its nature. React documentation recommends using Higher-Order Component(HOC) or Hooks for cross-cutting concerns.

If you are not familiar with the higher-order components, please have a look at https://reactjs.org/docs/higher-order-components.html official documentation.

Sitecore CDP provides a few different modes of integration. The focus of this post is primarily integrating a React website to CDP using javascript library. If you are not across this integration option, please get yourself familiarized here.

Since the detail of this integration option is not the focus of this post, I am providing certain pointers in case you are new to Sitecore CDP.

  • Sitecore CDP’s JS library exposes a global js object using ES6 features. This object is called Boxever and you can access it in the javascript using window.Boxever.
  • The Boxever object can then be utilized to send out the events(customer activities) to CDP.
  • In order to make event calls asynchronous, the JS library also exposes an event queue called _boxeverq. This queue can be accessed using window._boxeverq.
  • All we have to do is to prepare(json object) and push these events to the queue and then send out the events from the queue to CDP. Why use the queue? have a look at the documentation here.

A screenshot of the chrome console showing both the objects.

ChromeConsole

In my React website, I have different components that are supposed to send out events to CDP. In order to keep the interactions streamed lined, I have created a React HOC component.

HOC takes care of sending events to CDP as well as adds fixed data attributes like channel, pos etc.  It passes a function called createdCdpEvent using props to wrapped components. Then my ProductCard component(which triggers add to cart) calls createdCdpEvent to raise the AddProduct event.

Here is my ProductCard component.

In order to render my ProductCard component, instead of using it directly, in my products gird, I used BoxeverHoc to render ProductCard as a wrapped component.

 

Happy coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *