CMS Development

JLiles
Participant

Detect whether page visitor is new or returning

SOLVE

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.

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

Detect whether page visitor is new or returning

SOLVE

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
}

 

View solution in original post

5 Replies 5
JLiles
Participant

Detect whether page visitor is new or returning

SOLVE

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!

jonchim
Guide | Diamond Partner
Guide | Diamond Partner

Detect whether page visitor is new or returning

SOLVE

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.






Jon Chim
VP of Design & Development
Hypha HubSpot Development


check Did my post help answer your query? Help the Community by marking it as a solution
piersg
Solution
Key Advisor

Detect whether page visitor is new or returning

SOLVE

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
}

 

dennisedson
HubSpot Product Team
HubSpot Product Team

Detect whether page visitor is new or returning

SOLVE

@JLiles 

How does smart content not work for this situation?

@jonchim , @piersg  any thoughts here?

0 Upvotes
JLiles
Participant

Detect whether page visitor is new or returning

SOLVE

Hi Dennis,

 

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.

 

JLiles_0-1620401566705.png

 

0 Upvotes