Is there a simple way to detecth whether a page visitor is new or returning in HubSpot CMS? I would like to display different content depending on whether they are new or returning, regardless of whether they have filled out a form and become a contact.
Ideally, it would be a smart content option, but it doesn't look like it is supported there. Less ideally, I would like to use some combination of JavaScript and HubL to use (set, if necessary) cookies.
I think @jonchim is correct. Smart content will work for testing new/existing contacts but to determine if a visitor is new or returning you would have to use JS: for example, by testing if the hubspotutk cookie has been set (which is an id given to a visitor after they've accepted cookies on your site).
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0, len = ca.length; i < len; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
var hutk = readCookie('hubspotutk');
if (hutk != null || hutk != undefined) {
// do things
}
Great, thank you @piersg, @jonchim, and @dennisedson. That is along the lines of what I was thinking, but I just wanted to make sure there wasn't a simpler solution that I was unaware of. I appreciate the prompt, respectful, and ultimately helpful assistance!
For existing contacts, using smart content would definitely work. But I guess there's no way to determine if a visitor is a return visitor if they're not already a contact. 🤔Not sure there's an easy solution without having to use javascript, etc. Let me see if I can find any solutions.
I think @jonchim is correct. Smart content will work for testing new/existing contacts but to determine if a visitor is new or returning you would have to use JS: for example, by testing if the hubspotutk cookie has been set (which is an id given to a visitor after they've accepted cookies on your site).
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0, len = ca.length; i < len; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
var hutk = readCookie('hubspotutk');
if (hutk != null || hutk != undefined) {
// do things
}
I may be misunderstanding Smart Content, but I thought it was based on either contact information or "anonymous" information like device type. I want to display different content based on whether a user has visited the page before, regardless of whether they are a contact. But perhaps I am missing an option or trick.