How can we help?

Browser Contact Lookup

Web applications may often benefit from access to both the anonymous Browser ID (or bid) and the known Customer ID (or cid) from a visitor's browser session. Cordial makes these accessible with a few simple JavaScript calls. See below for examples of how to perform this retrieval.

You need to have Cordial's JavasScript Listener V2 installed on your site to use this feature.

Code snippet for browser contact lookup

// Example 1: Using async/await (recommended)
async function checkCurrentBrowserContact() {
  try {
    const { browserID: bid, contactID: cid, found, message } = await crdl.browserContactLookup.forCurrentBrowser();
  
    if (found) {
      console.log('Contact found!');
      
      // Handle DynamicYield scenarios
      DY.API("event", {
          name: "Identify",
          properties: {
              dyType: "identify-v1",
              cuid: cid,
              cuidType: "ContactID"
          },
          secondaryIdentifiers: [{ type: "browserID", value: bid },]
      });
    } else {
      console.log('Contact not found:', message);
      console.log('Browser ID:', bid);
      //Handle unknown visitor
    }
  } catch (error) {
    console.error('Error looking up browser contact:', error);
  }
}

// Call the function
checkCurrentBrowserContact();

// Example 2: Using promises
crdl.browserContactLookup.forCurrentBrowser()
  .then(({ browserID: bid, contactID: cid, found, message }) => {
    if (found) {
      console.log(`Browser ${bid} is linked to contact ${cid}`);
      // Your success logic here
    } else {
      console.log(`No contact found for browser: ${message}`);
      // Handle unknown visitor
    }
  })
  .catch(error => {
    console.error('Lookup failed:', error);
  });
  

Response Object Structure

Success case (contact found):

{
  browserID: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  contactID: "12345",
  found: true
}

Failure cases:

// No browser ID in cookies
{
  browserID: null,
  contactID: null,
  found: false,
  message: "Browser ID not found"
}

// Browser ID exists but no associated contact
{
  browserID: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  contactID: null,
  found: false,
  message: "Contact not found for this browser"
}

Comments

0 comments

Please sign in to leave a comment.