Sync your users
This guide keeps your Wakio contacts in step with your own user table, so a name you correct, a newsletter box someone unticks or an account someone closes reaches Wakio without anyone touching the dashboard.
What you'll end up with
One Wakio call for each thing that can happen to a user in your app, a first backfill that sends nobody a welcome email, and a sync that is safe to run every night because it can never subscribe someone who left.
You need these before you start:
- A key that can manage contacts. Issue one under API keys and tick Can manage contacts. Keep it away from the key your mailer uses. A key without the scope gets
403 insufficient_scopeon every call below except adding a contact. - A stable id for each user. Send your own user id as
external_idand every later call can name the person by it, so you never store Wakio's ids. Do not use an email address as theexternal_id: the id appears in request paths, which proxies and access logs record, so the address would sit in your logs and Wakio's. Wakio does not refuse it, but do not. Escape a/in an id as%2F.
One call for each event
- Someone signs up and ticks the newsletter box. Add a contact with
subscribed: trueandconsented_atset to the moment they ticked it. - Someone signs up without ticking it. Add a contact without
subscribed. - Someone ticks the box later and never subscribed before. Add a contact with
subscribed: true. - Someone ticks the box after unsubscribing. Resubscribe a contact, because an add cannot reactivate them and its response
statussays so. A resubscribe applies only after an unsubscribe: for someone who never unsubscribed it answers409 not_withdrawn, and an add withsubscribed: trueis the call. - Someone unticks the box. Unsubscribe a contact from everything. Send it when the box goes from ticked to unticked, not for every user who has it unticked.
- Someone changes their details or email. Update a contact.
- Someone closes their account. Unsubscribe them from everything if you keep their data, and erase them if you delete it.
- Someone asks to be forgotten. Erase a contact. Wakio still keeps their opt-out and delivery history, and the erase page lists what.
Use Get a contact when you want to read what Wakio holds, and Unsubscribe a contact from some lists when the person leaves one list and not all of them.
Adding the same email again is safe. On a contact that already exists, the call fills in external_id, source, names and custom fields only where they are blank. To change a value that is set, send an update.
Things your sync should expect
- A
404on an unsubscribe, a delete or an erase means the person is already gone. There is nobody left to stop mailing, so treat it as done. - A
409 email_takenon an email change means another contact already has that address. Wakio does not merge contacts, because a merge would destroy the other contact's consent record. If that contact is not a real customer, erase it, then send the change again. If it had been subscribed, its opt-out stays on the address, so the contact you moved is unsubscribed and needs a resubscribe before it receives campaigns. - A
409 external_id_conflicton an add means the address belongs to a contact that holds another of your ids. Two of your users swapped emails, for example, and the other one has not been synced yet. Update that other user first, then send the add again. - A resubscribe sends at most three confirmations per address in 30 days, 24 hours apart. A call over the limit answers
202withalready_sentand aretry_aftertime in ISO 8601 UTC, and sends nothing. Wait until then. - Nightly runs cannot undo an unsubscribe. Sending
subscribed: truefor someone who left changes nothing. Readstatusin the response to see what Wakio did. - Read
listsandsuppressionas well asstatus, and mirror them back. A person can leave one list and stay on another, and an address can be blocked for a bounce or a complaint without anyone unsubscribing.suppressionisnullwhen nothing blocks the address, and otherwise names thereason. Untick the box in your app whenstatusisunsubscribed, and show a blocked address as one you cannot send campaigns to when thesuppression'sscopeismarketing, and as one you cannot mail at all when it isall.
Run it every night
Sync only the users that changed since the last run, rather than every user every night: it is the same calls for far fewer of them. For each user, update the contact by your external_id first. A 404 means Wakio has no contact with that id, so add the contact with the same external_id. An add finds an existing contact by email, so it also adopts one that was created without your id.
For a user whose box is ticked, also send an add with subscribed: true and the consented_at of the tick, and send the add you make on a 404 with them too. It changes nothing for someone already subscribed or for someone who left, so it is safe every night, and it subscribes someone who ticked the box since the last run.
A 422 external_id_taken means another contact in your account already has that external_id. Two of your users cannot share one id, so look for a duplicated id or a contact that was given the wrong one, and fix that rather than retrying.
Send the unsubscribe only on a change from ticked to unticked. If you send it for every unticked user instead, it is still safe: for a contact who never subscribed it changes nothing, and for one who already left it answers the same contact again.
Backfill your existing customers
Your first sync adds everyone already in your user table. Joining a list starts any live flow that begins when someone joins it, so a backfill that does not say otherwise sends a welcome email to every existing customer at once. Send enroll: false with every call. They join the list, no flow starts, and the consent record notes that the flows were skipped.
Send subscribed: true only for customers who actually agreed to your newsletter, and set consented_at to the date they did. Wakio records your word, dated that day, as their consent. A customer who never agreed goes in without subscribed and receives no campaigns.
Without lists, a subscribed: true add puts the customer on Subscribers, the list Wakio creates for the brand. Name the list with lists if your customers belong on another one, and do it from the first call, because an account with more than one brand refuses an add without lists.
There is no batch call. Send one add per customer. Each call is a network round trip with writes behind it, so plan on a few hours for 35,000 customers from a single worker, and use several workers to go faster. In Ruby, the wakio gem reuses one connection per client.
Next
Read Add a contact for every field, and Erase a contact for what Wakio still holds about an address afterward.