CMS Development

Hubsforero
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
3 Soluciones aceptadas
John
Solución
Colaborador líder | Partner nivel Platinum
Colaborador líder | Partner nivel Platinum

How can access URL parameters and set cookies using Hubl?

resolver

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.

Ver la solución en mensaje original publicado

Hubsforero
Solución
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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.

Ver la solución en mensaje original publicado

0 Me gusta
piersg
Solución
Asesor destacado

How can access URL parameters and set cookies using Hubl?

resolver

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

Ver la solución en mensaje original publicado

0 Me gusta
10 Respuestas 10
Hubsforero
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
Hubsforero
Solución
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
piersg
Solución
Asesor destacado

How can access URL parameters and set cookies using Hubl?

resolver

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

0 Me gusta
John
Solución
Colaborador líder | Partner nivel Platinum
Colaborador líder | Partner nivel Platinum

How can access URL parameters and set cookies using Hubl?

resolver

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
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
Hubsforero
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
piersg
Asesor destacado

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
Hubsforero
Colaborador

How can access URL parameters and set cookies using Hubl?

resolver

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 Me gusta
piersg
Asesor destacado

How can access URL parameters and set cookies using Hubl?

resolver

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
Equipo de producto de HubSpot
Equipo de producto de HubSpot

How can access URL parameters and set cookies using Hubl?

resolver

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?

0 Me gusta