Segment (Twilio) CDP Integration Guide
Filter Segment destinations in real-time based on visitor consent preferences to prevent unauthorized downstream data dissemination.
1. Analytics.js Source Middlewareโ
Add the Consent Shield middleware to your Segment analytics.js instance before calling analytics.load():
// Register Consent Shield middleware with Segment
analytics.addSourceMiddleware(function({ payload, next, integrations }) {
// Query active Consent Shield state
const consent = window.ConsentShield ? window.ConsentShield.getConsent() : null;
if (consent) {
// If advertising consent is not granted, disable ad destinations
if (!consent.advertising) {
payload.obj.integrations = payload.obj.integrations || {};
payload.obj.integrations['Facebook Pixel'] = false;
payload.obj.integrations['Google Ads (Classic)'] = false;
payload.obj.integrations['TikTok Ads'] = false;
}
// If analytics consent is not granted, disable analytics destinations
if (!consent.analytics) {
payload.obj.integrations = payload.obj.integrations || {};
payload.obj.integrations['Google Analytics 4'] = false;
payload.obj.integrations['Mixpanel'] = false;
payload.obj.integrations['Amplitude'] = false;
}
}
next(payload);
});
// Load your Segment write key
analytics.load("YOUR_SEGMENT_WRITE_KEY");
analytics.page();
2. Server-to-Server Consent Syncโ
Use Consent Shield webhooks to update Segment User Profiles with privacy traits:
consent_opt_out_all:true/falsegpc_signal_received:true/falselast_consent_timestamp: ISO 8601 string