visit
This sample application allows you to see mParticle data events and attributes displayed in an eCommerce UI as you perform them and experiment with implementing an mParticle data plan yourself.
Step 1: Create a data plan
Before we implemented data collection, we needed a data plan to work from. Using mParticle’s (which is one of six vertical-specific templates that our Data Plan Builder provides), we instantly had a set of retail-specific identities, events, and attributes defined for us to start with.
Step 2: Turn the data plan into JSON
From there, we could use the Data Plan Builder’s built-in script to automatically translate the data plan (as defined in the tabs of the Google Sheet) into machine-readable JSON:
Step 3: Automatically generate event collection code
With this JSON in hand, we used the mParticle to automatically generate the data collection code that we would implement in our application. This Snippet tool’s interface immediately represents each event in the data plan as a discrete code block that can be transported directly into your source code:
In this case we used the “Web SDK” option (which outputs JavaScript). As you can see in the example above, the interface also supports code generation for iOS (Swift and Objective-C) as well as Android (Kotlin and Java). Since the Snippet tool generates this code directly from the JSON representing the data plan, it eliminates many of the errors that commonly arise when manually translating data plans into code––for example, misspelling attribute names or using incorrect data types.
Step 4: Implement events throughout the application
While the Snippet tool does the work of generating the event collection code, we still have to implement these events throughout the application using the appropriate user-based triggers to fire them.
Many of our commerce events in this application are tied to users viewing or customizing products. Since we’re using a component-based architecture with React, our application has one template, ItemView.js
, that we use to dynamically display each individual product to the user. Therefore, we only had to implement each product-related event once on this page for it to work for each product the site displays.
useEffect(() => {
// isBrowser refers to a variable that checks whether the window object is defined
if (isBrowser) {
let customAttributes = {
id: props.pageContext.content.id,
name: props.pageContext.content.name,
categories: props.pageContext.content.categories
}
// Log event to mParticle
window.mParticle.logPageView("Product", customAttributes)
// Display event properties in UI
toast(
<ToastSuccess
eventName="Product"
eventCategory="Screen View"
product={customAttributes.name}
></ToastSuccess>,
{
position: toast.POSITION.TOP_RIGHT,
className: "success-toast",
}
)
}
})
Since we wanted this event to run each time the user views a new product, we placed it inside the useEffect hook, which will automatically run each time the component renders. Inside useEffect, we initialize an object called customAttributes which we use to store the event attributes: id, name, and categories. We then pass these attributes as the second argument to logPageView, which forwards this event along to mParticle.
What tools did we use?
We bootstrapped our data planning sample app with this , which provides an out-of-the-box retail user interface built on React, Next.js, and Gatsby. Since most developers working on eCommerce products today leverage these tools or similar component-based frameworks, we felt this stack would be ideal for showcasing this process.
Displaying events in the user interface
As you , you will see notifications appear in the top-right corner of the as you take actions including:
toast(
<ToastSuccess
eventName="Product"
eventCategory="Screen View"
product={customAttributes.name}
></ToastSuccess>,
{
position: toast.POSITION.TOP_RIGHT,
className: "success-toast",
}
)
Tools/Resources discussed in this post: