Thursday, May 8, 2014

Proprietary Analytics

If you want to go beyond what you can get from Google Analytics and start recording information from customers such as browser version, operating system and the use of mobile or full desktop template on a per-order level, you may want to consider programming that capability into your website.
Following is an example on how you can do that.

1) Create a table in the database to store that information:

CREATE TABLE `add_analytics` (
             `analytics_id` int(11) NOT NULL AUTO_INCREMENT,
             `orders_id` int(11) NOT NULL,
             `ismobile` tinyint(1) NOT NULL,
             `browser` varchar(32) CHARACTER SET utf8 NOT NULL,
             `version` varchar(16) NOT NULL,
             `platform` varchar(32) CHARACTER SET utf8 NOT NULL,
             `template` varchar(16) CHARACTER SET utf8 NOT NULL,
             `user_agent` varchar(128) CHARACTER SET utf8 NOT NULL,
             `datetime` datetime NOT NULL,
       PRIMARY KEY (`analytics_id`),

       KEY `orders_id` (`orders_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

2) Register that table with Zen Cart:
File

./includes/extra_datafiles/added_tables_definitions.php

Add:
define("TABLE_ANALYTICS","add_analytics");

3) Create the object that will be used to produce the information. The following code can be placed in a php file inside the ./includes/extra_configures/ folder:
class tracking{
  function analytics_capture($oID){
    global $db, $mobile, $template_dir;
    if(isset($oID) and $oID>0){
      $browser = $this->browser($_SERVER['HTTP_USER_AGENT']);
      $platform = $this->platform($_SERVER['HTTP_USER_AGENT']);
      $db->Execute("INSERT INTO `".TABLE_ANALYTICS."` (`orders_id`, `ismobile`, `browser`, `version`,
                                                                `platform`, `template`, `user_agent`, `datetime`)
                    VALUES ('".(int)$oID."', '".$mobile->ismobile()."', '".$browser['name']."', '".$browser['version']."',
                            '".$platform."','".$template_dir."', '".$_SERVER['HTTP_USER_AGENT']."','".date("Y-m-d h:i:s")."')");
    }
  }
  
  function browser($agent){
    $browsers = array('firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
    'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol');
    $user_agent = strtolower($agent);
    
    $browser = array('name'=>'', 'version'=>'');
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
      foreach($browsers as $b){
        if(preg_match("/($b)[\/ ]?([0-9.]*)/", $user_agent, $match)){
          $browser = array('name'   =>$match[1],
                           'version'=>$match[2]);
          if($match[1]=='msie')
            $browser['name']="Internet Explorer";
          else
            $browser['name']=ucfirst($browser['name']);
          
          
          return $browser;
        }
      }
    }
    return $browser;
  }
  
  function platform($agent){
    $os_platform    =   "Unknown OS Platform";
    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );
    foreach ($os_array as $regex => $value) { 
      if (preg_match($regex, $agent))
        $os_platform = $value;
    }   
    return $os_platform;
  }

}

4) Call that object from the orders class to record the data whenever a new order is placed:
File:
./includes/classes/order.php

Add:
$tracking = new tracking;

$tracking->analytics_capture($insert_id);

Before:
return($insert_id);

No comments:

Post a Comment