Mini Tutorial: Using Customer Segmentation based on Cookie Data

Recently I had one of my more strange requests from a client. They wanted to take advantage of Magento’s native Customer Segmentation feature. Nothing strange so far. The strange part is that they aren’t using customer accounts at all on their site.

Ideally the customer segmentation will work based on whether or not a specific cookie value is set. So if a customer clicks on a promotional link somewhere on the internet, they are taken to the website, a cookie value is set, and segmentation conditions are satisfied.

Fortunately, there was a fairly uncomplicated solution for this. For the sake of the example, and since this worked best in my particular clients case, I will be basing the segmentation on customer group.

So first we create a customer group in Customers->Customer Groups. Then we create a customer in Customers->Manage Customers and associate that customer to the group. Then we can create the segmentation in Customers->Customer Segments, ensuring that the condition “Customer Group is [enter group you just created]“ is set.

Then, the customer is brought to a page where a cookie value is set. To do that, you will want to create a function which does the following:

$cookie = Mage::getModel('core/cookie');
$cookie_content = “cookie_value”;
$cookie->set(“cookie_variable”,$cookie_content,null,null,null,null,null);

This sets the cookie, however, this case, we won’t necessarily be setting the segmentation based on the cookie, as you will see. I still set the cookie, as it is important on my clients end, but to fulfil the actual segmentation, we will log in the user as the customer we created by entering the following code after setting the cookie:

$customer = Mage::getModel('customer/customer')->([id of customer we just created]);

$customerSess = Mage::getSingleton('customer/session');
$customerSess->setCustomer($customer);
Mage::dispatchEvent('customer_login',array('customer'=>$customer));

Now all segmentation conditions will apply to the user. This was a pretty specific case that happened to work for my client, but if you can find any use from it, then great!