APIs & Integrations

RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

I'm looking to have JavaScript code get called on a webpage only if certain property values are confirmed in HubSpot. For example, a cookied visitor will only get the JavaScript call (which will show an NPS survey) if the company type property = Customer.

Is it possible to add custom code to the footer html of our pages/site to be checked prior to the NPS JavaScript code being called?

0 Upvotes
1 Accepted solution
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick. I'm not sure of an easy way to do this using javascript, but if your pages are built out in the HubSpot CMS, you could leverage the contact object in the developer info. For example, when I go to a page I know I've submitted on in my own HubSpot account. I can see every property associated with that cookied visitor (myself). Thus, I have access to the lifecyclestage property and can check with HubL, whether or not that is equal to "Customer"

The HubL to achieve that would be:

{% if contact.lifecyclestage == "Customer" %}
        Run JS here, or use CSS
{% else %}
        Run JS here, or use CSS
{% endif %}

Here's a screenshot of the contact object that's available to use:

Screenshot

Captured with Lightshot

View solution in original post

0 Upvotes
16 Replies 16
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hey Rick,

Sorry I meant to be a bit more explicit with my wording. I can go ahead and open up our own ticket so that you and I can communicate directly and in a more efficient manner than via the forums. I'll tell Shane to close his ticket out. You'll see a response from me momentarily. Please respond with the page that your code lives on once you see my response to you on our new ticket.

-Connor

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

No worries, @Connor_Barley. I actually already have a Support ticket open (Ticket number: #2189233), that's how I got referred here. It had been closed, but since the code doesn't seem to be working anymore, I'm reopening. Would you be able to hop into it?

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, unfortunately I'm not able to hop on a call due to the structure of my day and varying availability but happy to either open up a Support ticket so that it's a bit easier to respond, continue helping here, or move this to a DM. That said, the link above that you provided doesn't work for me so I can't seem to check out your page anymore to test to see if it's working.

For my own page here's my code: http://prntscr.com/m74qc2

And as you can see in the console, the code in the first if block is running http://prntscr.com/m74qx4

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, you can use the and operator. For example:

{% if company.name == "HubSpot" and company.website == "hubspot.com" %}
<script>
	console.log("{{ company.name }}");
 	console.log("{{ company.website }}");
</script>
{% else %}

In your case, that'd be:

{% if company.type == "Vendor - Customer" and contact.page_admin == "yes" %}
some script
{% endif %}
0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

Thanks! It now seems like the code just isn't being called anymore. Is there some sort of limit set on HubSpot domains? Is this something that we could have a live call to troubleshoot? I'm checking with one other detail on the Delighted side, but nothing seems to be working out.

Thanks for all of your help!

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

@Connor_Barley Excellent! All is working perfectly, BUT I'm wondering if I could add one more parameter to the JavaScript. How would I need to update the below code to also include contact.page admin = yes?

{% if company.type == "Vendor - Customer" %}

Thanks!

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Yep! It shows for me: http://prntscr.com/m4psxu :blush:

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, it actually does look like your script that you injected in the Footer HTML of that page is respecting the if statement:

If you're not seeing this work, its likely that your cookied contact doesn't have that value set, or your contact doesn't have it's hutk set at all (maybe you cleared browser cache recently?). I'd suggest opening the page in incognito mode, opt into cookie tracking, then reconvert on that form and wait a little bit. You can make sure that the company associated with your contact has the type property set to "Vendor - Customer", or that you have an {% else %} statement in your footer that handles companies of contacts that do not have the type property set to Vendor - Customer

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

Hey @Connor_Barley,
I've confirmed that both my and your (cbarley+test2@hubspot.com) records have a company type of Vendor - Customer. The Delighted survey is not showing when I visit the page. Does anything pop up for you when you visit?

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, nope, but looks like the script is getting loaded in, so it seems to be an issue with the script tag itself. Does the issue resolve when the script tag is not within any sort of HubL delimiters? (just pasted in the footer HTML as just the normal script tag)

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

I think I've fixed it, but I'm not 100% sure. Can you give it one more shot?

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, Looks like that value is showing up in the developer info for my contact so this code should theoretically work.

Would you mind if I updated that page so that I can test out some things such as logging values to the console if that statement evaluates to true? Nothing visual would be updated. Thanks!

0 Upvotes
cbarley
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick, that looks right. To check, I'd go to the page you're looking to implement this on then click the Sprocket in the upper right hand corner http://prntscr.com/m47b7z and then go to the Developer Info. Then check to see if type exists on the company object

Is there a page I can check out that has the code you wrote above on it? Can I also create myself as a test contact in your HubSpot Account? If so, can you please respond with your HubID?

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

@Connor_Barley it doesn't look like company.type exists on the company object. How would I go about getting it on?

Here is a page with the code: https://vendors.g2crowd.com/customer-here-for-you. Yes, you can go ahead and create yourself as a test contact. HubID: 4099946

0 Upvotes
RickSiegfried
Contributor

Call JavaScript based on Contact/Company values

SOLVE

Hey @Connor_Barley. Thanks for the insight. I'm actually trying to use a company level property. The pages are built on the HubSpot CMS, so I'm planning on adding this to the footer HTML of a few specific domains. The end game is to only have the code called if the person's company type is Vendor - Customer.

Does this look correct?

{% if company.type == "Vendor - Customer" %}
DELIGHTED JAVASCRIPT CODE
{% endif %}

0 Upvotes
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

Call JavaScript based on Contact/Company values

SOLVE

Hi @Rick. I'm not sure of an easy way to do this using javascript, but if your pages are built out in the HubSpot CMS, you could leverage the contact object in the developer info. For example, when I go to a page I know I've submitted on in my own HubSpot account. I can see every property associated with that cookied visitor (myself). Thus, I have access to the lifecyclestage property and can check with HubL, whether or not that is equal to "Customer"

The HubL to achieve that would be:

{% if contact.lifecyclestage == "Customer" %}
        Run JS here, or use CSS
{% else %}
        Run JS here, or use CSS
{% endif %}

Here's a screenshot of the contact object that's available to use:

Screenshot

Captured with Lightshot

0 Upvotes