Introduction:

In this article, you will learn how to capture hyphenated parameters and save them as custom fields in LeadsHook using JavaScript.

Here’s a clear step-by-step process using a sample script:

function setFieldsFromUrl() {
    // Get the URL parameters
    const urlParams = new URLSearchParams(window.location.search);
    
    // Get the values of the 'first-name' and 'last-name' parameters
    const firstName = urlParams.get('first-name');
    const lastName = urlParams.get('last-name');
    
    // Check if the values are not null or undefined
    if (firstName !== null && firstName !== undefined) {
        // Log the captured value
        console.log(`Captured first-name: ${firstName}`);
        
        // Call DT.setField with the captured value
        DT.setField('first_name', firstName);
    } else {
        console.log("First name not found in the URL parameters.");
    }
    
    if (lastName !== null && lastName !== undefined) {
        // Log the captured value
        console.log(`Captured last-name: ${lastName}`);
        
        // Call DT.setField with the captured value
        DT.setField('last_name', lastName);
    } else {
        console.log("Last name not found in the URL parameters.");
    }
}

// Assuming that DT.setField is defined, you can call the function
setFieldsFromUrl();

In the above script:

  • Extract URL parameters: Using URLSearchParams, you can parse the URL’s query string to extract parameters.
  • Retrieve hyphenated parameters: Use .get to retrieve the values of parameters with hyphens, like ‘first-name’ and ‘last-name’.
  • Save to LeadsHook custom fields: If these values exist, use DT.setField to save them in LeadsHook as custom fields (‘first_name’ and ‘last_name’ in this case).

You can place the script at the node level of your decision tree. Adjust parameters and custom field names as needed.

Important Note: When testing in Version 2 Decision Trees, you can display the custom field in the next node where you put the script. Just keep in mind that in V2, the nodes are pre-rendered on the server side, so the variable won’t be available in the first node.

Leave a Reply

Your email address will not be published. Required fields are marked *