We’ve migrated our documentation to a new site, which means some URLs have changed.
Subscriptions

Setting Campaign Code Cookie

Functionality

  1. Retrieving the tpcc parameter from the URL

    • The function getQueryParamByName('tpcc') searches for the tpcc parameter in the URL.

    • If the parameter is found, its value is stored in the tpcc variable.

  2. Creating cookie data

    • A campaignCodeDate object is created, containing the timestamp of when the cookie was set.

    • The top-level domain is determined using getTopLevelDomain() so that the cookie is accessible across all subdomains.

  3. Setting the cookie

    • The setCookieConsent() function is called, which sets the tpcc_<value> cookie with specific parameters (expiration period: 90 days, path: /, defined domain).

  4. Cookie synchronization (only for Safari)

    • If the browser is Safari or UIWebView, itp.synchronizeCookie() is executed, which:

      • Restores previously saved cookies from localStorage.

      • Adds the current cookies to the list for sending.

      • Saves them in localStorage.

      • Triggers a delayed cookie submission via an AJAX request to pn.cloudflareWorkerUrl.

Limitations

  • Does not work in private browsing modes that block localStorage.

  • Requires proper consent configuration (consent["vx"] must be initialized).

  • For proper functionality in Safari, pn.cloudflareWorkerUrl must be configured and accessible.

  • The cookie is only set if tpcc is present in the URL.

Synchronizing cookies and consents with postMessage and tp.util.setTpccCookies

If a user visits the site for the first time and has not given consent for cookie processing, the cookies will not be set and will have to be synchronized afterwards. In Safari, synchronization is performed via itp.synchronizeCookie(), using localStorage for temporary cookie storage before sending them to the server.

In general case, after consent is granted, cookies can be synchronized via the postMessage mechanism allowing communication between domains. In the example below such synchronization is configured for https://example.com and its consent page https://consent-example.com (please use your URLs instead).

  1. Add the code below to to your consent page.

window.addEventListener('message', function (message) {
    if (message.origin !== 'https://consent-example.com') {
        return;
    }

    let event;
    try {
        event = JSON.parse(message.data);
    } catch (e) {
        return;
    }

    if (event &amp;&amp; event.eventName &amp;&amp; event.eventName === 'setTpccCookie') {
      console.log('Tpcc cookies successfully set');
        tp.util.setTpccCookies();
    }
}, false);

2. Add the postMessage to the click function of your consent button.

function onbuttonClick() {
  setTimeout(() => {
    window.parent.window.postMessage(JSON.stringify({eventName: 'setTpccCookie'}), 'https://www.example.com');
  }, 1000)
}

As a result, a 'tpcc_' + tpcc cookie is set (tpcc here is the name in address bar).

Last updated: