Skip to main content

Saving a CID to Cookie While Using Redirect Tracking

Save the Click ID to a cookie when using redirect tracking so scripts can create conversions.

This guide explains how to capture and store a Click ID (CID) in a browser cookie when using a redirect tracking flow. This configuration is beneficial when you want to route traffic through redirect links but still use scripts to create conversions at the same time.

By implementing this script, your landing page will detect the tracking parameter from the URL, format it to be compatible with standard Trackdesk Click scripts, and save it as a cookie.

⚠️ Important:

  • Cookie Naming: The cookie name trakdesk_cid is intentional and must not be changed. The system specifically looks for this string to attribute conversions.


​

Before you start

Before implementing the script, ensure you have the following:

  • Your Tenant ID: Found in your Trackdesk account under Settings.

  • Landing Page Access: You must be able to edit the HTML code of the page where users land after the redirect.

  • Parameter Name: Identify which URL parameter will carry the Click ID (e.g., utm_term).

Step-by-step instructions

Step 1: Identify your tracking parameter

Decide which parameter you will use in your redirect link to pass the Click ID. While this guide uses utm_term as an example, you can use any parameter name as long as it matches the script configuration in the next step.

Step 2: Customize the redirect script

Copy the code block below:

<!-- Trackdesk help script redirect flow begin -->
<script>
// Function to get a query parameter by name from the URL
function getQueryParameter(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}

// Function to set a cookie
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
// Secure and SameSite attributes for better modern security practices
document.cookie = name + "=" + value + ";" + expires + ";path=/;Secure;SameSite=Lax";
}

// Main script execution
(function() {
const utmTerm = getQueryParameter("utm_term");

// Regex for standard UUID format: 8-4-4-4-12 alphanumeric hex characters
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

// Check if utmTerm exists and matches the UUID pattern
if (utmTerm && uuidRegex.test(utmTerm)) {
const cookieContent = {
tenantId: "TENANT_ID",
cid: utmTerm
};

// Convert the cookie content to a JSON string
const cookieValue = encodeURIComponent(JSON.stringify(cookieContent));

// Set the cookie
setCookie("trakdesk_cid", cookieValue, 30); // Cookie expires in 30 days
} else {
console.log("utm_term parameter is missing or is not a valid UUID. Cookie not set.");
}
})();
</script>
<!-- Trackdesk help script redirect flow end -->

Replace these values:

  • TENANT_ID: your Tenant ID

  • utm_term (optional): if you are not using utm_term, replace it in the script (in the getQueryParameter call and in the log message) with your chosen parameter name

Step 3: Insert the script into your landing page

  1. Open your landing page HTML file or your CMS editor (e.g., WordPress, Webflow).

  2. Paste the customized script as high as possible in the <head> of your landing page, so the Click ID is saved as soon as the page loads.

Did this answer your question?