Set Cookie from URL Parameter and using it in HTML on a different page.


I am setting up an affiliate link area for my site and need to be able to store a URL query string into a cookie that expires in a 7 days and pull it out into HTML later down the road.


Users will click on a link like:


I want to pull the key 'afil' and the value 'randomvalue' and put it in a cookie in the browser.


This cookie will be used later (on another page) in a link from one of to but I can't keep the parameter (query string) in the URL.


I can do this on another site using PHP but HubSpot doesn't let me use PHP. I saw other posts mentioning the use of HUBL. 


My PHP code is:



$value=$_GET['afil']; //the value from the url

setcookie("afilCookie", $value, strtotime( '+7 days' )); /* expire in 7 days */




And to create a link I would then add this code to any page:

if (isset($_COOKIE['afilCookie'])) {
$cookie = $_COOKIE['afilCookie'];
echo '<a href="' . $cookie . '" class="button">Order Now</a>';


Can anyone help me with creating the correct code in HUBL to do all of this?

1. Add a cookie with the key/value from the URL query string (parameter).

2. Pull the cookie value to use as a variable in a link.


I am very unfamiliar with it and haven't been able to find any posts that match this request.

4 Replies 4

HubSpot has told me that there is no way to set a cookie using PHP, Javascript, jQuery, or HubL. If anyone knows otherwise please let me know.

Key Advisor

I don't see why you could not use JavaScript to set a cookie?

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

setCookie("afil", "{{ request.query_dict.afil }}", 30);

I'd give it a try and see if it works!



I asked Hubspot and they said that they support Javascript but not when related to cookies

Key Advisor | Partner

To get just the value from a specific query field you can use this HubL:

{{ request.query.get('afil') }}

So if your URL looks like this:

The code will return the string "randomkey". Of course you could also just do this without HubL at all and with just vanilla javascript.

Stefen Phelps, Community Champion, Kelp Web Developer