Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tegansnyder/4541860 to your computer and use it in GitHub Desktop.
Save tegansnyder/4541860 to your computer and use it in GitHub Desktop.
An example of creating an Abandon Cart report utilizing Mixpanel. This queries Magento for abandon carts then updates few people properties at Mixpanel. Then in Mixpanel you can create an "Engage" report that sends emails to folks that have "cart_created_at" less than 1 day and it will send emails to these people. Pretty niffted idea.
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$log = '';
$db_conn = Mage::getSingleton('core/resource');
$r_conn = $db_conn->getConnection('core_read');
$sql = "SELECT `main_table`.*, (main_table.base_subtotal_with_discount*main_table.base_to_global_rate) AS `subtotal`, `cust_email`.`email`, `cust_fname`.`value` AS `firstname`, `cust_lname`.`value` AS `lastname`, CONCAT_WS(' ', cust_fname.value, cust_lname.value) AS `customer_name` FROM `sales_flat_quote` AS `main_table`
INNER JOIN `customer_entity` AS `cust_email` ON cust_email.entity_id = main_table.customer_id
INNER JOIN `customer_entity_varchar` AS `cust_fname` ON cust_fname.entity_id = main_table.customer_id AND cust_fname.attribute_id = 5
INNER JOIN `customer_entity_varchar` AS `cust_lname` ON cust_lname.entity_id = main_table.customer_id AND cust_lname.attribute_id = 7 WHERE (items_count != '0') AND (main_table.is_active = '1') AND (main_table.created_at >= '".date('Y-m-d 00:00:00')."') ORDER BY updated_at DESC";
$dat = $r_conn->fetchAll($sql);
foreach ($dat as $c) {
21$orders = Mage::getResourceModel('sales/order_collection')
21 ->addFieldToSelect('customer_id')
->addFieldToFilter('customer_id', $c['customer_id'])
->addFieldToFilter('created_at', array('from' => date('Y-m-d 00:00:00')));
if ($orders->getSize() == 1) {
} else {
// Update Mixpanel People records
$params = array(
'$set' => array(
'$email' => $c['customer_email'],
'$first_name' => $c['customer_firstname'],
'$last_name' => $c['customer_lastname'],
'cart_created_at' => $c['created_at'],
'cart_updated_at' => $c['updated_at'],
'cart_items_count' => $c['items_count'],
'cart_grand_total' => $c['grand_total']),
'$token' => 'YOUR_MIXPANEL_TOKEN',
'$distinct_id' => $c['customer_id']
);
$url = 'http://api.mixpanel.com/engage/?data=' . base64_encode(json_encode($params));
exec("curl '" . $url . "' >/dev/null 2>&1 &");
$log .= 'customer_id: ' . $c['customer_id'] . ' - cart_items_count: ' . $c['cart_items_count'] . ' - cart_grand_total: ' . $c['grand_total'] . PHP_EOL;
}
}
$fh = fopen('/cron/log/abandon.log', 'w') or die('no file found');
fwrite($fh, $log);
fclose($fh);
$mail = Mage::getModel('core/email');
$mail->setToName('John Doe');
$mail->setToEmail('[email protected]');
$mail->setBody($log);
$mail->setSubject('Daily Abandon Cart Log');
$mail->setFromEmail('[email protected]');
$mail->setFromName("John Doe");
$mail->setType('text');
$mail->send();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment