Add undefined guard to setCookie