CMS Development

Hubsforero
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi people!
We have to implement a solution via Hubl so we can take an URL parameter and create a cookie with its value.
I have been searching this on the forum but nobody have catched an URL parameter and no one have create a cookie this way (maybe just I didn't find it, sorry then).
We can't use Javascript because we need to set some properties in the cookie like "Http" or "Secure Flags"... so we need server-side code...
Thanks to everybody in advance!
Regards!

0 Upvotes
3 Accepted solutions
John
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

How can access URL parameters and set cookies using Hubl?

SOLVE

You can access cookie values in hubl via request.cookies.

 

Beware that using this on a page will break page caching. 

 

In order to set a cookie, you'll have to use javascript to read a url param as there is no way to do this via hubl. Finally, see this stack overflow article on how to set a secure flag using js.

Edit:
you might could set it via hubspot if you have access to serverless functions (available on enterprise)



I like kudos almost as much as cake – a close second.

View solution in original post

Hubsforero
Solution
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi John!

First of all, thank's a lot for your help!

If I get you:
- Accesing cookies values with Hubl --> Breaks page caching --> Page more slower
- I can't read url param with Hubl --> I've to mix different techonologies for a single thing...
- You can set secure flag with Javasctipt --> But we can set HttpOnly...
- It can be possible to use serverless functions (on enterprise) --> For this client, we have a pro solution...

So the best way (and I can say, the only one possible) is to have an enterprise account so you can use servelss functions, haven't it? Tell me if I undertanded all that you said and I put your answer like the solution of the post!

Thanks again and regards.

View solution in original post

0 Upvotes
piersg
Solution
Key Advisor

How can access URL parameters and set cookies using Hubl?

SOLVE

You can read URL param with HubL using {{request.query}}. More info and other methods here.

View solution in original post

0 Upvotes
Hubsforero
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Helo Piersg:

Thanks for correct the part of the URL param, so the definitive solution is:
- I can access cookies values with Hubl but breaking page caching.
- I can read url param with Hubl.
- I can set secure flag with Javasctipt

- I can't set the HttpOnly flag.
- It can be possible to use serverless functions (on enterprise).

Thanks Piersg and thanks John, I`ll try to mark you both as accepted solution.

Regards.

0 Upvotes
Hubsforero
Solution
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi John!

First of all, thank's a lot for your help!

If I get you:
- Accesing cookies values with Hubl --> Breaks page caching --> Page more slower
- I can't read url param with Hubl --> I've to mix different techonologies for a single thing...
- You can set secure flag with Javasctipt --> But we can set HttpOnly...
- It can be possible to use serverless functions (on enterprise) --> For this client, we have a pro solution...

So the best way (and I can say, the only one possible) is to have an enterprise account so you can use servelss functions, haven't it? Tell me if I undertanded all that you said and I put your answer like the solution of the post!

Thanks again and regards.

0 Upvotes
piersg
Solution
Key Advisor

How can access URL parameters and set cookies using Hubl?

SOLVE

You can read URL param with HubL using {{request.query}}. More info and other methods here.

0 Upvotes
John
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

How can access URL parameters and set cookies using Hubl?

SOLVE

You can access cookie values in hubl via request.cookies.

 

Beware that using this on a page will break page caching. 

 

In order to set a cookie, you'll have to use javascript to read a url param as there is no way to do this via hubl. Finally, see this stack overflow article on how to set a secure flag using js.

Edit:
you might could set it via hubspot if you have access to serverless functions (available on enterprise)



I like kudos almost as much as cake – a close second.

Hubsforero
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi Piersg!
Thanks again, but I need to create the cookie in Hubspot, it's a priority, setting all the flags.
Can I create the cookie using Javascript and then access it with Hubl and modify the flags in Hubl too? I suposse if I can't create de cookie in Hubl also I can't change any ot its properties...
Regards.

0 Upvotes
Hubsforero
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi Piersg!


Thanks a lot for the answer.
Sorry,  but it has to be a cookie (can't be a session storage), and we need to activate the flags. I have done the same code in PHP, maybe it will help us:

 

Captura de pantalla 2020-10-16 a las 12.13.08.png

 

But I take note about {{request.query}} and {{request.cookies.myCookieName}}, thanks again for that. I assume you can access to this"myCookieName" only if it have been created before (out of hubl)..., is that?

Regards!

 

 

0 Upvotes
piersg
Key Advisor

How can access URL parameters and set cookies using Hubl?

SOLVE

You can access "myCookieName" via request.cookies if it exists on the site, it doesn't matter how it was set (but yes it has to have been created before you access it, otherwise it will return null/be empty). So if you manage to set it via PHP on the server, you'll be able to access it in HubL.

0 Upvotes
Hubsforero
Contributor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi Dennisedson!

 

Thanks for the answer!
I'll try to show you the problem:
We have this URL for example (landing.superpage.com?abc=randomvaluewewanttokeepinacookie).
Our customer wants to store the bold parameter in a cookie, but we can't use Javascript because with Javascript cookies creation intructions we can't set the flags, you have to use server-side code. We need HTTP Only Flag, Secure Flag and main domain we use (superpage.com). Here an example of the cookie we want:

Captura de pantalla 2020-10-16 a las 9.43.00.png

Thanks again! Regards.

0 Upvotes
piersg
Key Advisor

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi. @Hubsforero,

 

You can get the URL parameter in HubL with {{request.query}}. You can also access cookies via {{request.cookies}} which is a dict, so you can do {{request.cookies.myCookieName}}.

 

Setting an HTTP cookie, however, I'm not sure about. I know you can do this in PHP but of course you'd need access to the server for that. You could use session storage instead?

dennisedson
Community Manager
Community Manager

How can access URL parameters and set cookies using Hubl?

SOLVE

Hi @Hubsforero absent javascript, I do not know how this would work.  Can you be more descriptive and provide an example?

 

@John , @piersg @BarryGrennan -- maybe you have some ideas that will boggle my mind?

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes