User notification preferences

Hi,

I need to send some notification emails to the single users, the user should be able to change his preferences on what tipe of emails he wants to receive.
I think I can store the user preferences in the database but I need to set up all the preferences to yes as default, so maybe I could save all the preferences in the database when I create the user but what happen if in the future I decide to add more preferences? What is the best way to achieve this? Many thanks

This is just a question of how you want to store the preferences. If you set a simple field to true/false if they are able to change their preferences, that is limiting for sure. However, if you think you might want to store a ton of options, you could store something like a json object in a text field. This is one way many CMS systems do it.

When you want to then read the option back out, you read the JSON text from the database, use your server side language (like PHP) to convert that JSON to an object and start checking for various options.

If you want to change or add an option, you read the database, convert to an object, set its various options, then save the object back to the database. This isnā€™t the only way to do it, but is a simple one to implement.

If you would like to know more about this idea, check out the idea of ā€œserializationā€ (or the process of converting classes/objects to text and back.

Another option is much like how WordPress manages its options where you have a simple table that has a series of keys => value pairs that are tied to a user ID. Each key is for an option like ā€œchange_preferencesā€ => true. The one draw back to this is if you start scaling it up to hundreds of thousands of users and each user has like 20-30 pairs, you have a lot of records and many of them repeat other than the user ID.

I am of the opinion that using a database JSON field that stores a userā€™s preferences as the way to go. Even adding on to the object later is really easy. You can test if a userā€™s JSON has an option, if not, you add it and resave to the database. :slight_smile:

Hi many thanks for your answer. So if I save the json with all the options when I create the user then I can set all the preferences as true (so by default every user will receive the notification) then when the user changes the options I will update the json in the database. The only problem is if in the future I want to add more preferences will be difficult to set the new one as default for each user that has already been created in the past.

In the WordPress option on the other hand will be easier to query the database with mysql if I need to select all the users with a certain preference set to yes

I think, you need relation many-to-many. That meansā€¦

  1. Table users
  2. Table preferences[id, title] with list of preferences.
  3. Table users-to-preferences[user_id, preference_id]

Hi guys many thanks. Even if I like @Martyr2 json suggestion I believe Iā€™ll go with @igor_g suggestion. Then if in the future I add more settings Iā€™ll notify the user about this new option and the possibility for them to activate it from profile preferences as I believe it will be complicated to add the new option in the join table for existing users.

@igor_g has shown the optimal setup (serialized data vs @igor_g solution). It is also infinity scalable.

Which one(s)?

Back of my head was just doing a bitmask, tbh, but I suppose it depends on how many/how complex of an option you want to present.

2 Likes

Well as mentioned before, that is one way to do it but if you are expecting a large number of users be prepared for exponential number of rows. Lets say you have 100k users. Then users-to-preferences will have a 100k new rows for each preference you add.

It has some advantages for sure, easy searching and querying through the join table, just be aware of that fact. :slight_smile:

Well off the top of my head I know that WordPress does this for certain options. Not necessarily JSON, but they use PHP serialization for some of the roles/permission fields. They also serialize arrays etc. I think Joomla use to do something similar back in the day. I donā€™t know if they still do it now.

Many WordPress plugins also do it for storing their options. One option in the options/options meta tables and they can store their entire pluginā€™s settings. Easily retrieve and set it through get_option and update_option methods.

No. Not any user has all preferences.

Hi @igor_g in theory yes because I need to set all the motivations active by default Unless there is another solution

Then call your proxy table users-to-preferences-excluded and save there excluded preferences

1 Like

Be very careful about assuming opt-out with usersā€™ preferences for receiving emails from your site. Depending somewhat on what you mean by ā€˜notification emailsā€™, you may run afoul of several countriesā€™ anti-spam laws.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.