Setup custom Cookie at User's browser

Hi,

I want to set Custom Cookie Variable with it’s value and expire date. My main goal is to set Cookie variable is depend upon the login user, Once the user is log into website the cookie variable at user’s browser is set with email address and it wll remove once the user is logged out.

I am trying to use Javascript code for this functionality.

Here is my Javascript code:

function setCookie(name, value, days) {
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + “=” + value + “;path=/;expires=” + d.toGMTString();

}

-----------------------------------------------------------------------

Unfortunately the code is not working at Hubspot platform. Can you please help me this? How we can set, get and remove the custom cookie variable at HubSpot either using Javascript or HubL.

Thanks in advance.

Hi @DProdigitas, I hope that you are well!
Thanks for asking the Community!
I’d love to put you in touch with some of our Top Experts: Hi @stefen, @alysamwilie and @Syeda_Fatima do you have suggestions to help @DProdigitas, please?
Thanks so much and have a beautiful day!
Best,
Bérangère

Hi @DProdigitas

Hey, Here is a slightly modified version of your code.

 function setCookie(name, value, days) {
 var d = new Date();
 d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
 var expires = "expires=" + d.toUTCString();
 document.cookie = name + "=" + value + ";path=/;" + expires;
}

// Example usage
setCookie("userEmail", "example@email.com", 7);

Also please check your cookie settings in HubSpot and make sure it’s enabled.

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

Hi @GRajput Thanks for your response,

I tried the same code but it was not working, unable to set cookie value at user’s browser. Can you please provide us better option using JavaScript or HubL code.

Thanks in advance.

Hi @DProdigitas, I hope that you are well!
I have found similar threads that might guide you in the right direction:
- The solution from @bpotter on this post “Javascript Cookie Values passed into Hubspot Form
- The solution from @stefen on this post “Set Cookie from URL Parameter and using it in HTML on a different page.
- The solution from @Teun on this post “Utilizing cookie data in a form id
- The solution from @tjoyce on this post “Javascript Cookie Values passed into Hubspot Form
Also, I’d love to put you in touch with some of our Top Experts and Community Members: Hi @gweaver, @thompsonsj, @MTicudean, @REVEALiO, @DevM, @zach_threadint, @Kevin-C and @jeremymandle do you have suggestions to help @DProdigitas, please?
Have a wonderful day and thanks so much!
Best,
Bérangère