Magento automatic gender recognition plugin

Hello!
Today we’re going to create an extension for automatic gender recognition for customers. Extension is based on Rapleaf personalization API. Rapleaf provides demographic and lifestyle data (age, gender, marital status, income, etc.) on personal consumer email addresses (but not for business emails). They partner with dozens of large (and small) data companies to aggregate data and tie it to email addresses.
You can create a free Rapleaf account to get an API key for 1000 free age and gender queries.
Let’s get started…

1. Inchoo_Autogender.xml

Firstly we have to create the Inchoo_Autogender.xml file in /app/etc/modules folder.

<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_Autogender>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_Autogender>
    </modules>
</config>

2. config.xml

Second step is to create the config.xml file in /app/code/local/Inchoo/Autogender/etc folder. The content is as follows:

<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_Autogender>
            <version>0.1.0</version>
        </Inchoo_Autogender>
    </modules>
    <global>
        <models>
            <inchoo_autogender>
                <class>Inchoo_Autogender_Model</class>
            </inchoo_autogender>
        </models>
        <events>
            <customer_register_success>
                <observers>
                    <inchoo_autogender_add_email>
                        <class>inchoo_autogender/observer</class>
                        <method>getGender</method>
                    </inchoo_autogender_add_email>
                </observers>
            </customer_register_success>
        </events>
    </global>
</config>

3. Observer.php

The final step is to create the Observer.php file in /app/code/local/Inchoo/Autogender/Model folder with the following content:

<?php
 
class Inchoo_Autogender_Model_Observer
{
    public function getGender($observer = null)
    {
        if($observer)
        {
            try
            {
                $customer = $observer->getCustomer();
 
                $api_key = '... your api key ...';
 
                $client = new Zend_Http_Client();
 
                $client->setUri('https://personalize.rapleaf.com/v4/dr?api_key=' . $api_key .
                                '&email=' . urlencode($customer->getEmail()) .
                                '&first=' . urlencode($customer->getFirstname()) .
                                '&last=' . urlencode($customer->getLastname())
                );
 
                $client->setConfig(array('maxredirects' => 0, 'timeout' => 2));
 
                $response = $client->request();
 
                if ($response->getStatus() < 200 || $response->getStatus() >= 300)
                {
                    Mage::log(
                        sprintf("Rapleaf query failed. (status: %s), (message: %s)",
                            $response->getStatus(),
                            strip_tags($response->getBody())),
                        null,
                        'rapleaf_api.log',
                        false);
                }
                else
                {
                    $data = json_decode($response->getBody(), true);
                    if(array_key_exists('gender', $data))
                    {
                        $customer->setGender(
                            Mage::getResourceSingleton('customer/customer')
                                ->getAttribute('gender')
                                ->getSource()
                                ->getOptionId($data['gender'])
                        );
                    }
                }
            }
            catch (Exception $e)
            {
                Mage::log(
                    sprintf("Exception in Rapleaf query. (message: %s)",
                       strip_tags($e->getMessage())),
                    null,
                    'rapleaf_api.log',
                    false);
            }
        }
    }
}

Explanation

Extension listens to the customer_register_success event that is dispatched when ever the new customer is registered (check the /app/code/core/Mage/Customer/controllers/AccountController.php file, createPostAction method).
Extension then hits the Rapleaf API server with customers data and if Rapleaf has any information about the customer, it sends that back.
Notes

  • Use the extension at your risk.
  • Check Rapleaf’s privacy policy.
  • Check var/log/rapleaf_api.log from time to time.

GitHub Repository