OneTrust setDataSubjectID (#1937)

* leverage OneTrust's setDataSubjectId() method, leverage SessionStorage for utm params (not cookies)

* update setDataSubjectId formatting: https://developer.onetrust.com/onetrust/docs/javascript-api#set-data-subject-id
This commit is contained in:
jasonbryant84
2025-10-06 13:15:46 +01:00
committed by GitHub
parent 9d2302c883
commit 31ada8c911
4 changed files with 43 additions and 19 deletions
@@ -110,22 +110,27 @@ export function addGA4Properties(properties) {
export function persistUTMParams() {
if (!window.location.search) return;
const utmParams = getUTMParams();
const urlUtmParams = getUTMParams();
if (!Object.keys(utmParams).length) return;
if (!Object.keys(urlUtmParams).some(key => urlUtmParams[key])) return;
let filteredParams = '';
let ampersand = false;
for (const key in utmParams) {
if (utmParams[key]) {
ampersand = filteredParams.length;
filteredParams += `${ampersand ? '&' : ''}${key}=${utmParams[key]}`;
for (const key in urlUtmParams) {
if (urlUtmParams[key]) {
ampersand = filteredParams.length > 0;
filteredParams += `${ampersand ? '&' : ''}${key}=${urlUtmParams[key]}`;
}
}
const oneYearInSeconds = 365 * 24 * 60 * 60;
document.cookie = `${UTM_PARAMS_KEY}=${filteredParams}; path=/; max-age=${oneYearInSeconds}`;
if (filteredParams) {
sessionStorage.setItem(UTM_PARAMS_KEY, filteredParams);
}
}
export function getStoredUTMParams() {
return sessionStorage.getItem(UTM_PARAMS_KEY);
}
export function getUTMParams() {
@@ -148,15 +153,17 @@ export function getUTMParams() {
}
export function addUTMToLinks() {
const utmParams = getCookie(UTM_PARAMS_KEY);
const utmParams = getStoredUTMParams();
// Add url params to outbound links to product site
if (utmParams) {
const links = document.querySelectorAll('a[href*="cloud.qdrant.io"]');
const links = document.querySelectorAll('a[href*="cloud.qdrant.io"]');
links.forEach(link => {
const href = link.href;
const separator = href.indexOf('?') === -1 ? '?' : '&';
link.href = `${href}${separator}${utmParams}&qdrant_ref=qdrant_tech`;
if (!href.includes(UTM_PARAMS_KEY)) {
link.href = `${href}${separator}${utmParams}&qdrant_ref=qdrant_tech`;
}
});
}
}
}
@@ -1,6 +1,10 @@
import scrollHandler from './scroll-handler';
import { XXL_BREAKPOINT } from './constants';
import { initGoToTopButton, persistUTMParams } from './helpers';
import {
addUTMToLinks,
initGoToTopButton,
persistUTMParams
} from './helpers';
import { handleSegmentReady } from './segment-helpers';
import { registerAndCall } from './onetrust-helpers';
import TableOfContents from './table-of-content';
@@ -9,6 +13,8 @@ persistUTMParams();
// on document ready
document.addEventListener('DOMContentLoaded', function () {
addUTMToLinks();
const handleOneTrustLoaded = () => { // One Trust Loaded
window.OneTrust.OnConsentChanged(async () => { // One Trust Preference Updated
registerAndCall();
@@ -32,4 +32,15 @@ export function registerAndCall() {
.catch(() => false);
return true;
} // registerAndCall
} // registerAndCall
export function setOneTrustDataSubjectId() {
const analytics = window.analytics;
const activeGroups = window.OnetrustActiveGroups;
if (!analytics || !activeGroups) return;
const anonymousId = analytics.user?.()?.anonymousId?.();
if (!anonymousId) return;
window.OneTrust.setDataSubjectId(anonymousId, true, 'AnonymousID');
}
@@ -1,5 +1,5 @@
import { addGA4Properties, addUTMToLinks, getCookie, getUTMParams, tagCloudUILinksWithAnonymousId } from './helpers';
import { registerAndCall } from './onetrust-helpers';
import { addGA4Properties, getCookie, getUTMParams, tagCloudUILinksWithAnonymousId } from './helpers';
import { registerAndCall, setOneTrustDataSubjectId } from './onetrust-helpers';
const PAGES_SESSION_STORAGE_KEY = 'segmentPages';
const INTERACTIONS_SESSION_STORAGE_KEY = 'segmentInteractions';
@@ -161,9 +161,9 @@ function cleanSegmentUtmKeys(obj) {
/* Handle Segment Ready */
/************************/
export function handleSegmentReady() {
addUTMToLinks();
analytics.ready(() => {
setOneTrustDataSubjectId();
const utmParams = getUTMParams()
const cleanUtmParams = cleanSegmentUtmKeys(utmParams);