-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ras data library #2451
feat: ras data library #2451
Conversation
0b62fbc
to
35d4afb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've played around a little and everything works as expected. I have one maybe-blocker comment and another non-blocking suggestion.
The maybe blocker is that this won't work well in Multi-sites running on subfolders, because the local storage is tied to the root of the domain. I know this is not a pressing requirement for us, but maybe easier to address it now than in the future if the need arises.
And the suggestions for the api:
- what do you think of renaming
dispatch
todispatchActivity
to make it easier to understand what it does - also, why not create a similar wrapper for
store.set
andstore.delete
? also with specific names likesetData
for example. - Then we can maybe reconsider if we need to include the
store
to the the main object, and leave it only for internal usage. wdyt?
Nice catch! We can make the store prefix a filterable localized value, so each site can have its own prefix and store preserved. WDYT? It's already an isolated config value, so making that change is not a big lift.
Good call! Done in 13e0672
I'm not sure, the wrapper for "activity" exists because it's a specific use of the store's "collections". I don't see a reason to justify wrapping the store's get/set/delete. |
@leogermani implemented the filterable store prefix in a26ca63 |
Cool, I think that instead of filtering the prefix, you could simply inform the blog id with
The only reason I see is to simplify the API. So the developer only needs to interact with one thing and the methods have self-explanatory names |
Hub & Spoke is not MS, is it?
I actually like being able to pass the store object. You can see how it's implemented in |
It's not.
Ok! If you think that's a good approach I'm totally fine with it! I can help writing some docs later on as this matures |
Thanks! I added the |
# [2.1.0-alpha.1](v2.0.1...v2.1.0-alpha.1) (2023-06-22) ### Bug Fixes * allow campaign prompts to render on metered content ([#2516](#2516)) ([bba79ef](bba79ef)) * allow high-res youtube thumbs via constant ([#2507](#2507)) ([235757b](235757b)) * **donations:** empty cart on donation checkout ([#2505](#2505)) ([35d9a91](35d9a91)) ### Features * do not override data on popup events ([#2506](#2506)) ([051769b](051769b)) * move RSS image to plugin and increase size ([#2504](#2504)) ([3dab16f](3dab16f)) * **plugin-installer:** handle premium plugins in PluginInstaller ([#2482](#2482)) ([b35aeeb](b35aeeb)) * reader data library ([#2451](#2451)) ([2ad5f06](2ad5f06))
🎉 This PR is included in version 2.1.0-alpha.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
# [2.1.0](v2.0.3...v2.1.0) (2023-07-03) ### Bug Fixes * allow campaign prompts to render on metered content ([#2516](#2516)) ([bba79ef](bba79ef)) * allow high-res youtube thumbs via constant ([#2507](#2507)) ([235757b](235757b)) * **donations:** empty cart on donation checkout ([#2505](#2505)) ([35d9a91](35d9a91)) ### Features * do not override data on popup events ([#2506](#2506)) ([051769b](051769b)) * move RSS image to plugin and increase size ([#2504](#2504)) ([3dab16f](3dab16f)) * **plugin-installer:** handle premium plugins in PluginInstaller ([#2482](#2482)) ([b35aeeb](b35aeeb)) * reader data library ([#2451](#2451)) ([2ad5f06](2ad5f06))
🎉 This PR is included in version 2.1.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
A reader data library using localStorage. Along with the library implementation, this PR also tweaks the content gate's frontend metering strategy to use it.
Reader data
Gets and sets reader data using localStorage. Each item is a localStorage entry prefixed with
np_reader_
and a stringified JSON value.Updating a value emits a
data
event:Data sync
By default, every data update will attempt to sync with the database as user meta. For anonymous sessions, an internal
unsynced
array will store the keys that are pending synchronization until there's an authenticated session that allows the data push.Items can also skip synchronization on
.set()
:For authenticated readers, synced items will rehydrate the store on page load, which allows for systems that rely on the store to have consistent reader data across different sessions.
Backend handling of synced data
Using the synchronization strategy, data can be originated and managed on the backend:
The following will update the reader localStorage store on the next page load:
Note that the value should always be stringified JSON, so make sure to always use
wp_json_encode()
. Even for strings so'test'
becomes the json compliant'"test"'
You can also delete and get store items:
Reader activity
The store supports "collections", which will be used for managing reader activity with a schema that integrates with data events.
The collection can hold up to 1,000 items and each lives for 30 days.
The following code:
np_reader_activity
localStorage entry to include:activity
event:Activities can be fetched with:
The next step is to have proper integration with data events: a new activity can dispatch a data event on the backend and a data event can dispatch a reader activity to the frontend.
How to test the changes in this Pull Request:
newspackReaderActivation.store.set( 'foo', 'bar' );
np_reader__unsynced
(the queue polling strategy picked it up the synced it to the backend)newspack_reader_data_item_foo
newspackReaderActivation.store.delete( 'foo' );
np_reader__unsynced
item contains[ 'foo', 'boolean', 'array' ]
foo
user meta value should update frombar
tobaz
newspackRAS.push( [ 'test-activity', { foo: 'bar' } ] );
np_reader_activity
entry is updated to include the activity with atimestamp
propertyOther information: