Add segmentation opt-in controls to your site
The previous tutorial steps describe how you set up the Customer Data Platform (CDP) to track user events on your website and set up segment rules that group customers based on particular event conditions. This tutorial step describes a very simplified UI implementation you can use to test basic segmentation in this tutorial.
Before you start
This code is not considered a production-ready implementation. You should consider implementing the Consent Banner component for a more robust opt-in experience for your customers.
Steps
-
Add a standard page to your site and name it Change Segments.
Read the Adding assets page in the CMS documentation for information on how to do this.
-
View the Content tab of the standard page.
-
Add a WYSIWYG component with the following instructional text.
Details
This page will help you get placed into a segment so that you can test personalization. To put yourself in a particular segment: 1. Click the 'Give consent' button to simulate a 'Cookie banner'. This lets the CDP know that you consent to be tracked. 2. Click one of the segment buttons: I am a customer. 3. Wait 30 seconds; you should now have been placed into the selected segment. 4. Visit a page with personalized content and test the result. 5. To remove yourself from a segment, click the Revoke Consent button. 6. Repeat the process to put yourself into another segment.
-
Add a Code component.
-
Declare the first block of code that controls the Give Consent and Revoke Consent buttons, and the related functions that pass the user consent action through to the CDP.
Details
<h2>Give or revoke consent to CDP tracking</h2> <div id="component_365"> <div id="consent-buttons"> <button id="consent-button-give" >Give Consent</button> <button id="consent-button-revoke" >Revoke Consent</button> </div> <script> function giveConsent() { setConsent(1); return false; } function revokeConsent() { setConsent(0); return false; } function setConsent(consented) { fetch(`/__dxp/cdp/setConsent`, { method: `POST`, headers: { [`Content-Type`]: `application/json` }, body: JSON.stringify({ "CDPConsent": consented, "documentVersion": "1.0" }), redirect: `follow` }).then((data) => { console.log("testing consent") console.log(data) console.log(`consent set to ${consented}`); //location.reload(); }).catch(() => { alert(`Failed to set consent`); }); } //give button document.getElementById('consent-button-give').addEventListener('click', function(e){ console.log('click') giveConsent(); e.preventDefault(); }); //revoke button document.getElementById('consent-button-revoke').addEventListener('click', function(e){ console.log('click') revokeConsent(); e.preventDefault(); }); </script> </div>
-
In the same Code component, directly below the closing
<div>
element of your consent block, add the Segment buttons that website users will click to place themselves into one of the segments you configured.Read the callouts for helpful information about this code block. Details
<h2>Select a segment </h2> <p><button id="customer">I am a customer</button></p> <p><button id="prospect">I am a prospect</button></p> <div id="response">...</div> <script> function sendCustomEvent(id, type) { var successMessage = "..."; document.getElementById("response").innerHTML = successMessage; var myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); var raw = JSON.stringify({ "source": "custom", "action": id, "event": { "type": type } }); var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow' }; //fetch("https://your-organization.matrix.squiz.cloud/__dxp/events/custom", requestOptions) (1) fetch("/__dxp/events/custom", requestOptions) .then(response => { var successMessage = ""; if (response.status === 202){ successMessage = "Event successfully tracked - persona should be updated within 30 seconds."; }else{ successMessage = "Error with event - please try again, or grant consent first." } document.getElementById("response").innerHTML = successMessage; }) .then((result) => { console.log(result) }) .catch(error => console.log('error', error)); } var demo1 = document.getElementById('customer'); (2) var demo2 = document.getElementById('prospect'); demo1.addEventListener('click', function(e) { (3) sendCustomEvent('persona-selector', 'customer'); }); demo2.addEventListener('click', function(e) { sendCustomEvent('persona-selector', 'prospect'); }); </script> <style> (4) .response{ margin: 1em 0; padding: 1em 2em; border: 1px solid grey; background: #efefef; } </style>
1 Replace https://your-organization.matrix.squiz.cloud
with the name of your Squiz DXP Content Management instance name.2 These vars introduce your segment names from CDP into your frontend code so that CDP can pick up the action on-click. 3 These functions use the persona-selector
events you set up for each segment to present the right content.4 This style section adds a border around the response
<div>
element. -
Select Save.
-
View the Details tab.
-
Make the site live by either:
-
Using the screen header buttons:
Details
-
Click the Unpublished button in the screen header.
-
In the menu that appears, change the status from Under Construction to Approve and Make Live.
-
Click the Apply change now button.
-
-
Use the in-page controls:
Details
-
Open the drop-down menu next to the Change status entry.
-
Select Approve and Make Live. If appropriate, select the Cascade status change box.
-
-
-
Click Save in the screen header to save these changes.
-
Continue to the next part of the tutorial to add content blocks with different personalized content for the segments.