0

I am using code below to get cookie and assign it to ampscript variable @user

function checkCookie() {
    var user = getCookie("email");
    if (user != "") {
        alert("Checking cookie: " + user);
        Variable.SetValue("@user", user);
    } else {

    }
}

I am calling check cookie function on body on load:

<body onload="checkCookie()">

I am using code below to display different content based on whether cookie is present or not.

%%[IF EMPTY(@user) THEN]%%

%%[ELSE]%%

%%[ENDIF]%%

I am able to set cookie, get cookie and check cookie. Check cookie alert displays correct information. However, the value of cookie is not getting assigned to @user by Variable.SetValue("@user", user);

Am I missing something?

2

Yeah, another way of doing this is to use ampscript to grab and store the value of the cookie.

So something like this. This will parse out the value out of the cookie.

%%[

set @cookieName = 'cookieName' 
set @subject = HTTPRequestHeader("Cookie")
set @pattern = Concat(@cookieName, '=(.*?)(;\s|$)') 
set @cookieValue = RegExMatch(@subject, @pattern, 1) 
set @user = @cookieValue 

IF EMPTY(@user) THEN]%%

%%[ELSE]%%

%%[ENDIF]%%
| improve this answer | |
0

You can't mix server-side JavaScript and client-side JavaScript. Think of SSJS as you would PHP.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.