CMS Development

Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

I'm trying to show specific content if a user is part of a list in HubSpot.

 

Psuedo:

 

If contact_id is part_of_this_list then do this

The contact ID at the moment is being obtained from the query string. I'm trying to check if the user is part of said list, but it's not working.

 

Approach:

 

{% set id_querystring = request.query_dict.id %}
{% set registration_list_id = "6136" %} <!-- id of the list I'm checking -->

{% if id_querystring in registration_list_id %}
 contact is part of list
{% else %}
 contact is not part of list
{% endif %}
0 Upvotes
10 Replies 10
Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

Any ideas?

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to check what lists a contact is part of by contact ID?

Hey @Amit_95 

 

Check out the "request_contact.list_memberships" on this page, I think that might be exactly what you need.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

Hi Kevin,

 

I've tried something equivelant to that already:

 

{% set id_querystring = request.query_dict.id %}
{% set registration_list_id = "6136" %} <!-- id of the list I'm checking -->
{% set list_memberships = request_contact.list_memberships %}

{% if id_querystring in registration_list_id %}
 contact is part of list
{% elif id_querystring in list_memberships %}
  contact has valid query string and is part of list
{% else %}
 contact is not part of list or no valid query string
{% endif %}
0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to check what lists a contact is part of by contact ID?

Hey @Amit_95 

 

When you set you "registration_id" you're setting it as a string,

then in your IF your checking if that string is in a list of integers, which should return false.

 

So if you declare the variable like so:

{% set registration_list_id = 6136 %}

Does this then work as expected?

 

 

 

Edit:

Random string of thought…I wonder if you should also check if the query string exists in you if statement?

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

Hey,

 

Unfortunatly, it does not. It is still executing the else statement.

 

Looking at my elif statement again:

 

{% elif id_querystring in list_memberships %}

 

Lets say my URL is the following:

 

/home?id=31955951

 

id_querystring will equal 31955951 (which is my contact ID).

 

In that elif statement, I'm querying (in pseudo):

 

elif 31955951 is in list_memberships

The above doesn't seem correct to me. How can I get the query to be:

 

elif get all lists contact is part of (using the contact id) and then check if the user is part of a specific list (registration_list_id)

 

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to check what lists a contact is part of by contact ID?

So I a little got too confused and jumped back to your original post:

 

{% set registration_list_id = 6136 %} <!-- id of the list I'm checking -->
{% set list_memberships = request_contact.list_memberships %}

{% if registration_list_id in list_memberships %}
    Content to show if the user IS a memeber of the list
{% else %}
    Content to show if the the user IS NOT a memeber of the list
{% endif %}

Am I missing anything?

 

 

You can test this like so:

{% set registration_list_id = 6136 %} <!-- id of the list I'm checking -->
{% set list_memberships = [6136,1234,987645,4567,7654,23,7654] %}

{% if registration_list_id in list_memberships %}
    Content to show if the user IS a memeber of the list
{% else %}
    Content to show if the the user IS NOT a memeber of the list
{% endif %}

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

Let me just clarify:

 

I'm creating a gated page which is accessible if they've registered for the event (filled out a form).

 

I can currently registered (complete the form) and it works: so this if statement is working:

 

{% if registration_list_id in list_memberships %}

 

The above works because on form submission, HubSpot creates a cookie and using that, it's able to find out what lists a member is part of.

 

Now, let's assume that the user completes the form (registers for the event), but, clears there cookies or returns to the site when the cookie has expired. To counter this, emails will be sent out to contacts where there contact IDs will be in the URL (i.e. /home?id=contact_id.

 

I've then created a list which anyone who has filled out the registeration form into a list (which is that list with the ID of 6136.

 

Now, what I'm looking to do is:

 

  1. Get query string from URL
  2. Check what lists this user is part of
  3. Check if the user is part of the registration list (with the ID of 6136) 
  4. If yes, for now, just print the message "contact has valid query string and is part of list"

 

Does that make sense?

 

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to check what lists a contact is part of by contact ID?

I think I see. So we just add an OR operator to the IF statement

{% set id_querystring = request.query_dict.id %}
{% set registration_list_id = 6136 %} <!-- id of the list I'm checking -->
{% set list_memberships = request_contact.list_memberships %}

{% if id_querystring == registration_list_id or registration_list_id in list_memberships %}
    Content to show if the user IS a memeber of the list
{% else %}
    Content to show if the the user IS NOT a memeber of the list
{% endif %}

 

this should cover both the use case and the edge case

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

How to check what lists a contact is part of by contact ID?

Thanks, but it's still not working.

 

I've gone into Chrome Incognito mode and parsed the following in the URL:

 

/home?id=31955951

As mentioned 31955951 is my contact ID and I am part of the registration. However, when on the page, I'm seeing the else statement:

 

Content to show if the the user IS NOT a memeber of the list

 

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to check what lists a contact is part of by contact ID?

Hey @Amit_95 

 

Sorry again.

 

crm_bject would be the tool you needed, but I'm pretty sure that can't be done on public pages, and can only be done on a login page or password protected page. See CRM_object ref here.

 

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes