Scripting Cookie Consents

By default synthetic tests behave like a some visiting a site for the first time – the cache is empty, there are no cookies set, localStorage is empty etc.

This empty state means that a Cookie Consent or Consent Management Platform (CMP) dialog is displayed during the test and this introduces two issues:

  1. The dialog often covers up more important content e.g. LCP
  2. Without consent many third-party tags won't load so the influence they have over performance metrics isn't measured.

For these reasons we often recommend adding a script to synthetic tests that sets the appropriate cookies and localStorage entries so that consent manager behave as if a visitor has already given consent for third-parties to load.

A typical script might look something like this:

setCookie	%ORIGIN%	name=value

injectScript	localStorage.setItem("key", "value");

navigate	%URL%

Common Consent Managers

Here's some consent managers we commonly encounter, along with a list of cookies or localStorage entries they use and a snippet of JavaScript that will generate a skeleton SpeedCurve synthetic script.

To use the snippet:

  • Load the page you want to create a synthetic test for and accept cookies
  • Open DevTools console, and paste the snippet in (you may have to type allow pasting first)
  • Execute the snippet. When it completes it will dump the generated synthetic script to the console and also copy the same script to the clipboard
  • Paste the generated script into the Script field for the URL you're testing

LiveRamp

LiveRamp requires the following localStorage entries to be set

  • euconsent-v2
  • cconsent-v2
  • addtl_consent
  • gdpr-dau
  • gdpr-last-interaction
  • gdpr-config-version
  • gdpr-auditId
  • geo-location

Executing this JavaScript snippet in the console, or as a DevTools snippet will generate a skeleton Synthetic Test script and copy it to your clipboard. You can paste the clipboard contents directly into the script field for a URL in site settings.

const items = ["euconsent-v2","cconsent-v2","addtl_consent","gdpr-dau","gdpr-last-interaction","gdpr-config-version","gdpr-auditId","geo-location"]
let commands = items.map(key=>`injectScript\tlocalStorage.setItem('${key}','${localStorage.getItem(key)}');`).join('\n');
commands += "\nnavigate\t%URL%\n"

console.log(commands);
copy(commands);

OneTrust

OneTrust requires two cookies to be set:

  • OptanonConsent
  • OptanonAlertBoxClosed

Executing this JavaScript snippet in the console, or as a DevTools snippet will generate a skeleton Synthetic Test script and copy it to your clipboard. You can paste the clipboard contents directly into the script field for a URL in site settings.

// Cookies
var cookies = document.cookie.split('; ');
var OptanonConsent = cookies.find(entry => entry.startsWith('OptanonConsent='));
var OptanonAlertBoxClosed = cookies.find(entry => entry.startsWith('OptanonAlertBoxClosed='));

// Generate script
var output = 
 `setCookie\t%ORIGIN%\t${OptanonConsent}\n` +
 `setCookie\t%ORIGIN%\t${OptanonAlertBoxClosed}\n` +
 `navigate\t%URL%`;

console.log(output);

copy(output);

Quantcast Choice

Quantcast Choice needs both localStorage entries and cookies to be set

localStorage

  • CMPList
  • noniabvendorconsent
  • _cmpRepromptHash

cookies

  • euconsent_v2
  • addtl_consent

Executing this JavaScript snippet in the console, or as a DevTools snippet will generate a skeleton Synthetic Test script and copy it to your clipboard. You can paste the clipboard contents directly into the script field for a URL in site settings.

// Local storage items
var CMPList = localStorage.getItem('CMPList');
var noniabvendorconsent = localStorage.getItem('noniabvendorconsent');
var _cmpRepromptHash =localStorage.getItem('_cmpRepromptHash');


// Cookies
var cookies = document.cookie.split('; ');
var euconsent_v2 = cookies.find(entry => entry.startsWith('euconsent-v2='));
var addtl_consent = cookies.find(entry => entry.startsWith('addtl_consent='));

// Generate WPT Script
var output = `injectScript\tlocalStorage.setItem('CMPList', '${CMPList}');\n` +
 `injectScript\tlocalStorage.setItem('noniabvendorconsent', '${noniabvendorconsent}');\n` +
 `injectScript\tlocalStorage.setItem('_cmpRepromptHash', '${_cmpRepromptHash}');\n` +
 `setCookie\t%ORIGIN%\t${euconsent_v2}\n` +
 `setCookie\t%ORIGIN%\t${addtl_consent}\n` +
 `navigate\t%URL%\n`;

console.log(output);

copy(output);

TrustArc

TrustArc needs the following cookies to be set:

  • TAconsentID
  • notice_preferences
  • cmapi_cookie_privacy
  • cmapi_gtm_bl
  • notice_gdpr_prefs

Executing this JavaScript snippet in the console, or as a DevTools snippet will generate a skeleton Synthetic Test script and copy it to your clipboard. You can paste the clipboard contents directly into the script field for a URL in site settings.

// Cookies
var cookies = document.cookie.split('; ');
var TAconsentID = cookies.find(entry => entry.startsWith('TAconsentID='));
var notice_preferences = cookies.find(entry => entry.startsWith('notice_preferences='));
var cmapi_cookie_privacy = cookies.find(entry => entry.startsWith('cmapi_cookie_privacy='));
var cmapi_gtm_bl = cookies.find(entry => entry.startsWith('cmapi_gtm_bl='));
var notice_gdpr_prefs = cookies.find(entry => entry.startsWith('notice_gdpr_prefs='));

// Generate WPT Script
var output = 
 `setCookie\t%ORIGIN%\t${TAconsentID}\n` +
 `setCookie\t%ORIGIN%\t${notice_preferences}\n` +
 `setCookie\t%ORIGIN%\t${cmapi_cookie_privacy}\n` +
 `setCookie\t%ORIGIN%\t${cmapi_gtm_bl}\n` +
 `setCookie\t%ORIGIN%\t${notice_gdpr_prefs}\n` +
 `navigate\t%URL%`;

console.log(output);

copy(output);

Usercentrics

Usercentrics requires the localStorage entries to be set:

  • uc_settings
  • uc_user_interaction

Executing this JavaScript snippet in the console, or as a DevTools snippet will generate a skeleton Synthetic Test script and copy it to your clipboard. You can paste the clipboard contents directly into the script field for a URL in site settings.

// Local storage items
var UCSettings = localStorage.getItem('uc_settings');
var UCUserInteraction = localStorage.getItem('uc_user_interaction');

// Generate WPT Script
var output = 
 `injectScript\tlocalStorage.setItem('uc_settings', '${UCSettings}');\n` +
 `injectScript\tlocalStorage.setItem('uc_user_interaction', '${UCUserInteraction}');\n` +
 `navigate\t%URL%\n`;

console.log(output);

copy(output);