Sharing my learning and implementation for the Sitecore CDP
Recently, I got work to implement the Sitecore CDP to capture the Quiz Question and Answers and when the user subscribe to the site, I need to capture the data, here is a live site.
/* eslint-disable no-return-assign */ import axios from "axios"; export function GetBrowserId() { if (window?.Boxever && window?.Boxever?.getID()) { const result = window?.Boxever?.getID(); return result; } } export function PushIdentifyEvent({ event, onSuccess }) { const browserID = GetBrowserId(); event.browser_id = browserID; const message = JSON.stringify(event); const clientKey = process.env.REACT_APP_CLIENT_KEY; const boxeverAPIEndpoint = `https://api-ap-southeast-2-production.boxever.com/v1.2/event/create.json?client_key=${clientKey}&message=${message}`; axios .get(boxeverAPIEndpoint) .then(function (response) { onSuccess(true); }) .catch(function (error) { // handle error }); }
On Success, Used below code to send the message
export default function CardSuccess() {const totals = useSelector((state: RootState) => state.totals);const [formData, setFormData] = useState({ email: "" });const [success, setSuccess] = useState(false);const onChange = (e: React.ChangeEvent<HTMLInputElement>): void =>setFormData({ ...formData, [e.target.name]: e.target.value });const { email } = formData;const handleSubmit = (e: React.SyntheticEvent) => {e.preventDefault();const event = {channel: "WEB",type: "IDENTITY",currency: "AUD",language: "EN",pos: "betheboss",email,identifiers: [{provider: "email",id: email,},],};PushIdentifyEvent({ event, onSuccess: (val: boolean) => setSuccess(val) });};Also used useEffectuseEffect(() => {if (success) {setFormData({email: "",});}}, [success]);