How to send Cart Abandonment notifications?
1. Save info about products added to cart
When a visitor adds a product to shopping cart, you need to save MaxTraffic Visitor ID, product ID and date in your own database. You can create a table shopping_cart_tmp with 3 columns: MtVisitorId, ProductId, date
To get MaxTraffic Visitor ID, use following script:
<script type="text/javascript">
mt(function(mtsdk) {
// Get push notification permission status
// "unsupported" push notifications not supported
// "default" visitor is ready to receive permission request to allow/block push
// "granted" visitor has allowed push notifications
// "denied" visitor has blocked push notifications
if(mtsdk.pushNotificationPermission == "granted") {
//visitor has granted permission to receive push notifications
//get visitors ID
console.log(mtsdk.visitorId);
}
});
</script>
So when someone adds a product to cart, you need to execute script similar to this one:
<script type="text/javascript">
mt(function(mtsdk) {
if(mtsdk.pushNotificationPermission == "granted") {
//get Maxtraffic Visitor ID
MtVisitorId = mtsdk.visitorId;
//get product id
ProductId = ...
//add info to DB
...
}
});
</script>
2. Delete info from DB, if product is removed from cart
If visitor removes any product from his shopping cart, you also need to delete it from shopping_cart_tmp table.
3. Delete info from DB, if purchase is completed
If visitor completes the purchase, then also delete all rows from shopping_cart_tmp table that are related to the specific visitor.
4. Create a Cron task to send Cart Abandonment notifications
Create a cron task that executes once per minute and checks - if there are any users who have not finished purchases and date is older than 1 hour:
SELECT DISTINCT(MtVisitorId) FROM shopping_cart_tmp
WHERE DATE_ADD(date, INTERVAL 1 HOUR) < NOW()
Then use MaxTraffic's REST API to send out a Push Notification to them. More info how to send a message with our REST API, is available here: dev.maxtraffic.com/#operation/addNotification
To send notification using MaxTraffic user ID (mtsdk.visitorId), you need to use parameter to_uids
And after you have sent the notifications, delete all the rows you just selected (so you don't send notifications to the same visitors again next time the cron task is executed):
DELETE FROM shopping_cart_tmp
WHERE DATE_ADD(date, INTERVAL 1 HOUR) < NOW()