Access Smart Content data from outside of the HubSpot ecosystem?

contacts

#1

Is there a way custom JavaScript could have access to the Smart Content data (from outside of a HubSpot module)?


#2

Hi @persianturtle,

Smart content rules are executed server-side, so it's not directly possible to access smart content data using Javascript. You could possibly use the 'Get contact by usertoken' endpoint to pull contact info when a visitor loads your site (see below). Note: If you go this route, it's not recommended to hit this endpoint on every page load, as you could quickly reach your API limits. Any data you pull from this endpoint should be cached.


#3

Hi Derek,

I was thinking one way would be to create a Smart Content block that renders some custom HTML when the conditions have been met. The HTML would contain a script tag that would fire a JavaScript event, link so:

<script>window.dispatchEvent(new CustomEvent("some:event"))</script>

Then, in the custom JavaScript, listen for that event.

To get specific user information, the Smart Content block could render the data somewhere (maybe in a script tag also), and the custom JavaScript would know where to look for that data.

Does this sound like it may work?


#4

Hi @persianturtle,

I'm not sure I'm totally clear on the process you're describing. The smart content block will be decided server side based on the rule that the visitor meets; if the resulting block includes a script, that would trigger an event. The next part I'm not entirely clear on, though; smart content only works on HubSpot pages, and in that case you'd already have access to personalization. Can you give me a bit more info on your workaround?


#5

Hi Derek,

Yes, we are using HubSpot pages exclusively, however, we are also using custom JavaScript. I'd like to be able to have that custom JavaScript be aware of when Smart Content is being rendered, and with that current visitor's data.

I'm thinking of using HubSpot Smart Content to render a script that 1) fires a custom event and 2) provides the current visitor's data.

Best,

Raph


#6

Hi @persianturtle,

I understand, thank for that information. I've never tried anything like that, so I'm not entirely clear on the details of what would happen. Smart content is rendered server side before the page is server to the visitor, so the event wouldn't technically fire when the content was 'rendered', but rather when the parser reached that point in your page.

Regardless, if you dispatched a different event for each smart content rule, you'd be able to know which version of the page is being loaded. Additionally, you can use HubL tokens to populate your page with any contact information you might need: