Implementing cookies on a dismissible slide-in panel with AMP
up vote
0
down vote
favorite
I'm implementing a slide-in-on-scroll newsletter signup form on our site, which is built as AMP-native.
I added a button that hides the div on tap using amp-bind, but I'm struggling to get my head around how to save the closed state with cookies/localStorage in AMP.
I've gone through the favorite button example code - https://ampbyexample.com/advanced/favorite_button/ (as recommended here on SE), but I don't see how it relates to this particular use-case, especially with the use of amp-list.
Based on what I've read and few examples I found the credentials="include" attribute is needed, as is a valid CORS json endpoint and an auto generated client id appended to the endpoint url using AMPs variable substitution, but I'm not sure how to put it all together.
I took a stab hacking something together using the favorite button example, but the tutorial doesn't say much about how to setup the CORS endpoint and that particular example is for storing multiple likes to a single endpoint, as apposed to storing a specific users viewing preferences.
Here is my rough (poking-around-in-the-dark) stab at the code:
<form method="post"
id="side-newsletter-wrap"
action-xhr="/prefs&clientId=CLIENT_ID(prefs)"
target="_top"
on="submit:AMP.setState({
showSideNewsletter: !showSideNewsletter
});
submit-error:AMP.setState({
showSideNewsletter: !showSideNewsletter
});">
<button on="tap:side-newsletter-wrap.hide" class="close-button"><i
class="fa fa-times"></i></button>
<amp-list width="320"
height="360"
credentials="include"
items="."
single-item
src="/prefs&clientId=CLIENT_ID(prefs)">
<template type="amp-mustache">
{{#.}}
<?php winefolly_load_fragment('newsletter-embed'); ?>
{{/.}}
{{^.}}{{/.}}
</template>
</amp-list>
</form>
For the prefs endpoint, I'm guessing I'd need to register a new endpoint in WordPress that outputs simple array with the preferences?
Something along the lines of:
{
showSideNewsletter: "true",
winesIndexView: "grid",
winesIndexSort: "title"
}
I also tried the amp-user-notification component (which has the closed state built in) but that felt a bit hacky and the newsletter embed code (via iframe) doesn't get loaded due to a known bug - https://github.com/ampproject/amphtml/issues/18995).
Any suggestions would be much appreciated.
Chris
wordpress cookies amp-html amp-bind
add a comment |
up vote
0
down vote
favorite
I'm implementing a slide-in-on-scroll newsletter signup form on our site, which is built as AMP-native.
I added a button that hides the div on tap using amp-bind, but I'm struggling to get my head around how to save the closed state with cookies/localStorage in AMP.
I've gone through the favorite button example code - https://ampbyexample.com/advanced/favorite_button/ (as recommended here on SE), but I don't see how it relates to this particular use-case, especially with the use of amp-list.
Based on what I've read and few examples I found the credentials="include" attribute is needed, as is a valid CORS json endpoint and an auto generated client id appended to the endpoint url using AMPs variable substitution, but I'm not sure how to put it all together.
I took a stab hacking something together using the favorite button example, but the tutorial doesn't say much about how to setup the CORS endpoint and that particular example is for storing multiple likes to a single endpoint, as apposed to storing a specific users viewing preferences.
Here is my rough (poking-around-in-the-dark) stab at the code:
<form method="post"
id="side-newsletter-wrap"
action-xhr="/prefs&clientId=CLIENT_ID(prefs)"
target="_top"
on="submit:AMP.setState({
showSideNewsletter: !showSideNewsletter
});
submit-error:AMP.setState({
showSideNewsletter: !showSideNewsletter
});">
<button on="tap:side-newsletter-wrap.hide" class="close-button"><i
class="fa fa-times"></i></button>
<amp-list width="320"
height="360"
credentials="include"
items="."
single-item
src="/prefs&clientId=CLIENT_ID(prefs)">
<template type="amp-mustache">
{{#.}}
<?php winefolly_load_fragment('newsletter-embed'); ?>
{{/.}}
{{^.}}{{/.}}
</template>
</amp-list>
</form>
For the prefs endpoint, I'm guessing I'd need to register a new endpoint in WordPress that outputs simple array with the preferences?
Something along the lines of:
{
showSideNewsletter: "true",
winesIndexView: "grid",
winesIndexSort: "title"
}
I also tried the amp-user-notification component (which has the closed state built in) but that felt a bit hacky and the newsletter embed code (via iframe) doesn't get loaded due to a known bug - https://github.com/ampproject/amphtml/issues/18995).
Any suggestions would be much appreciated.
Chris
wordpress cookies amp-html amp-bind
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm implementing a slide-in-on-scroll newsletter signup form on our site, which is built as AMP-native.
I added a button that hides the div on tap using amp-bind, but I'm struggling to get my head around how to save the closed state with cookies/localStorage in AMP.
I've gone through the favorite button example code - https://ampbyexample.com/advanced/favorite_button/ (as recommended here on SE), but I don't see how it relates to this particular use-case, especially with the use of amp-list.
Based on what I've read and few examples I found the credentials="include" attribute is needed, as is a valid CORS json endpoint and an auto generated client id appended to the endpoint url using AMPs variable substitution, but I'm not sure how to put it all together.
I took a stab hacking something together using the favorite button example, but the tutorial doesn't say much about how to setup the CORS endpoint and that particular example is for storing multiple likes to a single endpoint, as apposed to storing a specific users viewing preferences.
Here is my rough (poking-around-in-the-dark) stab at the code:
<form method="post"
id="side-newsletter-wrap"
action-xhr="/prefs&clientId=CLIENT_ID(prefs)"
target="_top"
on="submit:AMP.setState({
showSideNewsletter: !showSideNewsletter
});
submit-error:AMP.setState({
showSideNewsletter: !showSideNewsletter
});">
<button on="tap:side-newsletter-wrap.hide" class="close-button"><i
class="fa fa-times"></i></button>
<amp-list width="320"
height="360"
credentials="include"
items="."
single-item
src="/prefs&clientId=CLIENT_ID(prefs)">
<template type="amp-mustache">
{{#.}}
<?php winefolly_load_fragment('newsletter-embed'); ?>
{{/.}}
{{^.}}{{/.}}
</template>
</amp-list>
</form>
For the prefs endpoint, I'm guessing I'd need to register a new endpoint in WordPress that outputs simple array with the preferences?
Something along the lines of:
{
showSideNewsletter: "true",
winesIndexView: "grid",
winesIndexSort: "title"
}
I also tried the amp-user-notification component (which has the closed state built in) but that felt a bit hacky and the newsletter embed code (via iframe) doesn't get loaded due to a known bug - https://github.com/ampproject/amphtml/issues/18995).
Any suggestions would be much appreciated.
Chris
wordpress cookies amp-html amp-bind
I'm implementing a slide-in-on-scroll newsletter signup form on our site, which is built as AMP-native.
I added a button that hides the div on tap using amp-bind, but I'm struggling to get my head around how to save the closed state with cookies/localStorage in AMP.
I've gone through the favorite button example code - https://ampbyexample.com/advanced/favorite_button/ (as recommended here on SE), but I don't see how it relates to this particular use-case, especially with the use of amp-list.
Based on what I've read and few examples I found the credentials="include" attribute is needed, as is a valid CORS json endpoint and an auto generated client id appended to the endpoint url using AMPs variable substitution, but I'm not sure how to put it all together.
I took a stab hacking something together using the favorite button example, but the tutorial doesn't say much about how to setup the CORS endpoint and that particular example is for storing multiple likes to a single endpoint, as apposed to storing a specific users viewing preferences.
Here is my rough (poking-around-in-the-dark) stab at the code:
<form method="post"
id="side-newsletter-wrap"
action-xhr="/prefs&clientId=CLIENT_ID(prefs)"
target="_top"
on="submit:AMP.setState({
showSideNewsletter: !showSideNewsletter
});
submit-error:AMP.setState({
showSideNewsletter: !showSideNewsletter
});">
<button on="tap:side-newsletter-wrap.hide" class="close-button"><i
class="fa fa-times"></i></button>
<amp-list width="320"
height="360"
credentials="include"
items="."
single-item
src="/prefs&clientId=CLIENT_ID(prefs)">
<template type="amp-mustache">
{{#.}}
<?php winefolly_load_fragment('newsletter-embed'); ?>
{{/.}}
{{^.}}{{/.}}
</template>
</amp-list>
</form>
For the prefs endpoint, I'm guessing I'd need to register a new endpoint in WordPress that outputs simple array with the preferences?
Something along the lines of:
{
showSideNewsletter: "true",
winesIndexView: "grid",
winesIndexSort: "title"
}
I also tried the amp-user-notification component (which has the closed state built in) but that felt a bit hacky and the newsletter embed code (via iframe) doesn't get loaded due to a known bug - https://github.com/ampproject/amphtml/issues/18995).
Any suggestions would be much appreciated.
Chris
wordpress cookies amp-html amp-bind
wordpress cookies amp-html amp-bind
asked Nov 22 at 15:15
Chris
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You're right amp-user-notification is the correct approach. Is there a way to implement the newsletter form in AMP until the amp-iframe bug is fixed?
Another way is to use amp-access, which allows you to change the layout of the page on page load. You have to store the user preference server-side though using the READER_ID to identify the user. Storing this server-side is required as you might not be able to write cookies if the page is served from the AMP Cache due to ITP 2.0.
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You're right amp-user-notification is the correct approach. Is there a way to implement the newsletter form in AMP until the amp-iframe bug is fixed?
Another way is to use amp-access, which allows you to change the layout of the page on page load. You have to store the user preference server-side though using the READER_ID to identify the user. Storing this server-side is required as you might not be able to write cookies if the page is served from the AMP Cache due to ITP 2.0.
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
add a comment |
up vote
0
down vote
You're right amp-user-notification is the correct approach. Is there a way to implement the newsletter form in AMP until the amp-iframe bug is fixed?
Another way is to use amp-access, which allows you to change the layout of the page on page load. You have to store the user preference server-side though using the READER_ID to identify the user. Storing this server-side is required as you might not be able to write cookies if the page is served from the AMP Cache due to ITP 2.0.
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
add a comment |
up vote
0
down vote
up vote
0
down vote
You're right amp-user-notification is the correct approach. Is there a way to implement the newsletter form in AMP until the amp-iframe bug is fixed?
Another way is to use amp-access, which allows you to change the layout of the page on page load. You have to store the user preference server-side though using the READER_ID to identify the user. Storing this server-side is required as you might not be able to write cookies if the page is served from the AMP Cache due to ITP 2.0.
You're right amp-user-notification is the correct approach. Is there a way to implement the newsletter form in AMP until the amp-iframe bug is fixed?
Another way is to use amp-access, which allows you to change the layout of the page on page load. You have to store the user preference server-side though using the READER_ID to identify the user. Storing this server-side is required as you might not be able to write cookies if the page is served from the AMP Cache due to ITP 2.0.
edited Nov 23 at 16:51
answered Nov 23 at 7:55
Sebastian Benz
2,63111415
2,63111415
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
add a comment |
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi Sebastian, Thanks for the reply. Does that mean there isn't currently a way to save a users preference, like view (grid/list), sorting, order, etc? I tried getting the cookies working on our amp-list update, but couldn't find any relevant examples, so had to push it forward for the next update. As for the iframe not working in the amp-user-consent, the only work around I can think of would be to create a custom Klaviyo subscribe form, using their api. Just seems odd that there isn't an easy, consistent way to do this in amp.
– Chris
Nov 23 at 9:57
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
Hi again, Regarding setting cookies to save the users viewing preferences, I noticed that after I appended &clientId=CLIENT_ID(prefs) to my amp-list src url, the prefs cookie is actually being set. Is there not a way in AMP to do a conditional check to see whether the cookie has been set or not? I can also see that there is an amp-store entry being added to the localStorage when the amp-user-notification is closed, so it seems that the ability to save the state is there. Cheers, Chris
– Chris
Nov 23 at 16:25
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
You can serve user-data server-side based on the reader id. Client-side might not work if the AMP page is served from an AMP Cache as you won't be able to write cookies due to ITP 2.0.
– Sebastian Benz
Nov 23 at 16:52
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
Hey Sebastian, Thanks for the heads up on ITP 2.0, I hadn't heard of that before. Do you know of any examples of saving user-data server-side? I managed to find this help doc about setting up the amp client id api, but that seems more related to ga tracking and I can't find any mention of using the reader id.
– Chris
Nov 29 at 7:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433903%2fimplementing-cookies-on-a-dismissible-slide-in-panel-with-amp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown