AMP set and get cookies values

Deadly 提交于 2020-04-16 03:43:28

问题


I am new in this AMP. In web I have scenario like below.

Example I have a page with 100 paragraphs content ... For the user first time visit the page displaying only 10 paragraphs of content. then will ask to user email address in input form. after user provide the email address then will display remaining 90 paragraphs content... The same user visit 2nd time that page we displayed the content without asking email.

Implementation Logic in WEB

  1. After user enter the email address we stored the values in cookies.
  2. If the user visit 2nd time based on cookie values we display the content..

So same logic needs to implements the AMP pages.

I design the form in amp and other stuff but struggling to set the cookie values..

The following code I am used in WEB:

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

Once user enter the email and submit the form

setCookie('article-page','email','XXXXX');

回答1:


You can't directly access cookies from within an AMP page. However, you can use the amp-access component to implement this behavior server-side.

You can ignore the login/logout features provided by amp-access. The only thing you need to do is to change the response for the authorization endpoint to either return true or false depending on whether the user has provided an email address. Based on this information you can then adjust the content that is displayed on the page.



来源:https://stackoverflow.com/questions/57000913/amp-set-and-get-cookies-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!