Aug 25, 20208:56 AM - edited Aug 25, 20209:01 AM
Key Advisor
Storing Session Variable
SOLVE
@rohansdeeson Ok, something like this might work for that flow:
// on Page A create cookie and set value on click of item
document.getElementById("myBtn").onclick = function() {
// not sure what value you want to use, change textContent (which gets the text) to whatever attribute you need
var val = this.textContent;
document.cookie = "myNewCookie="+val;
};
// on Page C retrieve value of your cookie
var cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('myNewCookie'))
.split('=')[1];
console.log(cookieValue);
Edit: not sure if you can associate it with a contact in HS. If you create a contact property to upate, you can pair the above with updating that contact property in the same onclick function using the Contacts API https://legacydocs.hubspot.com/docs/methods/contacts/update_contact. Alternatively you can add it as a note to a contact using the Engagements API.
@piersg would this information be present somewhere when rendering HubL when the same user (but not contact) re-enters the site?
We have a specific need where we use a blog with tags, and the user can select a tag of their interest. If the selection has been made we'd like to add an UI element that points to oursite.com/theblog/tag/selectedtag and also include the tag when showing another module that shows recent posts from the blog.
The first one can be done with combination of LocalStorage and JavaScript, but for the second need we'd need to build a separate endpoint for getting the blog posts if the information is not available when rendering module HubL.
Hmm not sure you need JS to do that to be honest, but I'm also not clear on what you're trying to do.
You can get blog tags like this:
{% set my_tags = blog_tags(group.id, 250) %}
{% for item in my_tags %}
slug = {{ blog_tag_url(group.id, item.slug) }}
name = {{ item }}
{% endfor %}
So you could check if the URL is equal to a tag slug and display different elements based on that
{% set my_tags = blog_tags(group.id, 250) %}
{% for item in my_tags %}
{% if request.path is string_containing blog_tag_url(group.id, item.slug) %}
// Element
{% endif %}
{% endfor %}
You can get recent blog posts within a topic like so:
{% set tag_posts = blog_recent_tag_posts('default','[TAG NAME]', 3) %}
{% for tag_post in tag_posts %}
<div class="post-title">{{ tag_post.name }}</div>
{% endfor %}
Combining the above two:
{% set my_tags = blog_tags(group.id, 250) %}
{% for item in my_tags %}
{% if request.path is string_containing blog_tag_url(group.id, item.slug) %}
{% set tagName = blog_tag_url(group.id, item.slug) %}
{% set tag_posts = blog_recent_tag_posts('default', tagName, 3) %}
{% for tag_post in tag_posts %}
<div class="post-title">{{ tag_post.name }}</div>
{% endfor %}
{% endif %}
{% endfor %}
To answer your question about visitor vs contact, the Hubspot cookie for a visitor is hubspotutk, so you could check to see if that's the same? However, I don't think it's necessary for doing what (I think) you're trying to do with blog tags.
Yes I'm well aware of the HubL functions that exist. In the first case I'd need to render a link pointing to the tag the user has selected when they previously visited the site and made the selection
for which I'd obviously need the tag. Same goes for the other module where I'd need this
{% set posts = blog_recent_tag_posts('default', ['default1', 'default2', user.selected_tag], 10) %}
<section>
<h1>Recent posts of selected topics</h1>
{% for post in posts %}
<div>{# render things about the posts here #}</div>
{% endfor %}
</section>
where again the tag is needed.
Consider like when you visit the website of a grocery store chain and you need to select your favorite as you are not interested in the offering of a establishment on the other side of the country. For this, from the list of all stores, you'd click the button saying "Set as my store", and then the next time you come to the site they know you chose a favorite and can then show you the offering of that store regardless of the URL you ended to the site with. ie. pretty basic things that one could do with sessions store (basically when you as a developer are also in control of th backend implementation)
Edit: Hmm indeed cookies could work for this too - they`re available in {{ request.cookies }} on the server side
Aug 25, 20209:32 AM - edited Aug 25, 20209:36 AM
Key Advisor
Storing Session Variable
SOLVE
@vesse Yeah, turns out {{request.cookies}} does retrieve cookies you've set yourself via JS, so you could set the tag using the code I wrote in my above comment, then retrieve using that HubL variable.
e.g. if you set the name of the cookie as "selected_tag" you can do {{request.cookies.selected_tag }} to get the value.
Query strings won't work either because you will lose them at some point when going to other sites, but the cookie does actually solve it. You can set other than session cookies from JavaScript too, eg. this is valid for a year and can be seen serverside from request.cookies:
the only thing that cannot be set is httpOnly, but I don't see a problem here with other security measures like CSP, and due to the fact that we use it only for a minor rendering detail.
Thanks, it was your comment that lead me to this. Rarely have had a need to use cookies in the frontend 🙂
Aug 25, 20208:56 AM - edited Aug 25, 20209:01 AM
Key Advisor
Storing Session Variable
SOLVE
@rohansdeeson Ok, something like this might work for that flow:
// on Page A create cookie and set value on click of item
document.getElementById("myBtn").onclick = function() {
// not sure what value you want to use, change textContent (which gets the text) to whatever attribute you need
var val = this.textContent;
document.cookie = "myNewCookie="+val;
};
// on Page C retrieve value of your cookie
var cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('myNewCookie'))
.split('=')[1];
console.log(cookieValue);