APIs & Integrations

jhuddles
Participant

Only run identify API / set new properties if a person is not already a contact

Hello!

 

We send people to a page with a URL parameter that includes ?email=example@something.com, and this code below does a great job of doing what it's supposed to do – Creating a new contact from the email and giving them this property of "autocreated_pricing_page" as Yes. 

 

However... this also just updates this "autocreated_pricing_page" parameter for someone even if they already were a contact. I'm trying to only have this property be associated with NEW contacts created from this page, not just anyone who happens to visit with the ?email= parameter in the URL.

 

Would there be a way to run this conditionally based on something like is-contact: false? I am fairly a newbie at this, so please excuse me if this is a crazy thing to ask. Thanks!!

 

_hsq.push(['trackPageView']);

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: getParameterByName("email"),
    autocreated_pricing_page: true
}]);

 

0 Upvotes
1 Reply 1
IsaacTakushi
HubSpot Employee
HubSpot Employee

Only run identify API / set new properties if a person is not already a contact

Hi, @jhuddles.

 

No worries, I'm happy to help!

 

The short answer is yes, but you'll need to find a way to determine whether the visitor is a HubSpot contact.

 

Two relevant methods come to mind:

  1. Query the Get contact by email endpoint with the value of the email query parameter. Only trigger the identify script if the endpoint returns a 404.
  2. Query the Get contact by user token endpoint with the visitor's hubspotutk cookie value. Only trigger the identify script if the endpoint returns a 200 with "is-contact": false or a 404. (Of course, the visitor may exist as a contact but be using a different device or browser, so the email-based method above will be more effective.)

A caveat: like most HubSpot endpointsneither of these Contacts API endpoints support AJAX requests, so your client-side JavaScript will have to ping your backend to place the server-side requests.

Isaac Takushi

Associate Certification Manager
0 Upvotes