Utils:

DDQuery – little class to help with database queries

0

After spending much time coding queries in Zen Cart I can say that database layer object ($db, includes/classes/db/mysql/query_factory.php) is not quite developer friendly when it comes to ease of use. Let’s have a look at typical query to database using this object:

$cid = 12345; // any customer id
$cur = "USD";
$query = "SELECT * FROM " . TABLE_ORDERS . " WHERE customers_id = :cid AND currency= :currency";
$query = $db->bindVars($query, ":cid",$cid,"integer");
$query = $db->bindVars($query,":currency",$cur,"string");
$result = $db->Execute($query);
while(!$result->EOF) {
        echo $result->fields["customers_id"];
        $result->MoveNext();
}

Why it is inconvenient? First of all binding variables to query string takes too many characters to write AND You have to bind one value at a time. Imagine that You have to bind ten variables – it would take ten lines of code just to prepare the query!

Also writing manually each time type of variable (‘integer’,'string’,'date’,'double’) is error prone and inconvenient. I find myself from time to time writing ‘int’ instead of ‘integer’. This is an error that goes unnoticed until the line of code is executed.

The way I want to bind variables is like:

$query = new DDQuery("SELECT * FROM " . TABLE_ORDERS . " WHERE customers_id = :cid AND currency= :currency");
$result = $query->int(":cid",$cid)->string(":currency",$cur)->Query();

It’s just two line of code instead of four and much, much less characters to write. With six variables the code would still take just two lines of code instead of eight. It’s also less error prone to typos since any good IDE will show hint that method ‘sting’ is not defined in DDQuery class.

Iterating through results

I also personally hate the way Zen Cart forces me to iterate through query result. First it’s just hard for me to write !$result->EOF. I would much better prefer to write while($result->next()) (yeah, much like Java java.sql.ResultSet). Second I constantly forgot to write this additional $result->MoveNext() line, especially when while() body has more then five lines of code. So basicly I want to itreate through database results using:

while($result->next()) {
        echo $result->fields["customers_id"])
}

It’s just one line of code less to write but I get rid of ‘!$result->EOF’ which (maybe just to me) is hard to write and in addition I never again will face endless loop.

DDQuery class code

You can download DDQuery class code here. As far as I tested it it works fine. There’s no documentation to the class other then comments in .php file. I don’t have time/I’m too lazy to write it. Still I think that it’s usable piece of code and few of more advanced users of Zen Cart may use it.

Here’s code of the class

<?php

class DDQuery {
    var $query;
   
    public function __construct($query) {
        $this->query = $query;
    }
   
    public function bind($var, $value, $type) {
        global $db;
        $this->query = $db->bindVars($this->query, $var, $value, $type);
        return($this);
    }
   
    public function int($var, $value) { return($this->bind($var, $value, ‘integer’)); }
    public function integer($var, $value) { return($this->bind($var, $value, ‘integer’)); }
   
    public function str($var, $value) { return($this->bind($var, $value, ‘string’)); }
    public function string($var, $value) { return($this->bind($var, $value, ‘string’)); }
   
    public function float($var, $value) { return($this->bind($var, $value, ‘float’)); }
    public function date($var, $value) { return($this->bind($var, $value, ‘date’)); }
    public function currency($var, $value) { return($this->bind($var, $value, ‘currency’)); }
    public function csv($var, $value) { return($this->bind($var, $value, ‘csv’)); }
    public function passthru($var, $value) { return($this->bind($var, $value, ‘passthru’)); }
    public function noquotestring($var, $value) { return($this->bind($var, $value, ‘noquotestring’)); }
    public function enum($var, $value) { return($this->bind($var, $value, ‘enum’)); }
    public function regexp($var, $value) { return($this->bind($var, $value, ‘regexp’)); }
   
    public function getQuery() {
        return($this->query);
    }
   
    public function Execute() {
        global $db;
        return($db->Execute($this->getQuery()));
    }
   
    public function Query() {
        return(new DDQueryResult($this->Execute()));
    }
}

class DDQueryResult {
    var $result;
    var $fields;

    function DDQueryResult($zc_result) {
        $this->result = $zc_result;
    }
   
    function next() {
        if($this->result->EOF) return(FALSE);
        $this->fields = $this->result->fields;
        $this->result->MoveNext();
        return(TRUE);
    }
}
?>

Usage examples

$q = new DDQuery("INSERT INTO " . TABLE_CUSTOMERS_UPLOADS . "(customers_id, original_name, name, last_update, date_added, ip, status, file_size, hash_md5) VALUES(:cid, :o name, :name, NOW(), NOW(), :ip, ‘PENDING’, :size, :md5)");
$q->int(":cid", $cid)->string(":oname",$fileStruct[‘name’])->string(":name",$fileName)->string(":ip",$_SERVER[‘REMOTE_ADDR’])->int(":size",$fileStruct[‘size’])->string(":md5",$md5);
$q->Execute();
$q = new DDQuery("SELECT * FROM " . TABLE_CUSTOMERS_UPLOADS . " WHERE hash_md5 = :md5");
$q = $q->string(":md5", $md5)->Execute();
if($q->EOF) return FALSE;

Installation instructions

  1. Download the class code (DDQuery.zip) and extract it to root folder with Zen Cart installation.
  2. Use it!

Continue Reading

UTI v1.5 released

1

New version of User Tracking Interface (1.5) has been released.  Changes:

  • performance improvements
  • uti table size improvements
  • IP is stored as INT, not as VARCHAR(15)
  • few minor bug fixes
  • support for uti merge functions ( I’m going to write about this feature within few days)

UTI v1.5 has been tested with Zen Cart 1.3.8a but it should also work with newer versions. Update instructions for v1.1 users are included in .zip package.

Download UTI v1.5.

Posted in: UTI, Utils

Continue Reading

UTI 1.5 is coming.

0

User Tracking Interface version 1.5 will be released next week. There are few important bug fixes, some performance improvements and new features. In nutshell:

  • IP is no longer stored in database as VARCHAR(15) but as INT, so each UTI record takes less space
  • uti table prune is no longer triggered on catalog activity. Prune is triggered by admin activity and is executed only once per day.
  • some indexes on uti table might be changed
  • support for custom uti attributes merge functions (I’ll elaborate on it on UTI 1.5 release)

Continue Reading

User Tracking Interface v1.1

8

It’s mainly bug fix release. Update is STRONGLY recommended.

Changelog:

  • Changed file name of includes/auto_loaders/config.utis.php.php to includes/auto_loaders/config.utis.php
  • uti_install.sql missed some inserts
  • Fix to: [DELETE FROM uti_attributes WHERE uti_row_id = ? AND name IN () ]
  • UTI tables now use DB_PREFIX

You can download it from: UTI v1.1


Continue Reading

Bug in User Tracking Interface 1.0

0

It seems that I’ve forgotten to add installation commands to the sql installation file and there are no options under ‘UTI Variables’ in Configuration Menu. To Go to Admin->Tools->Install SQL patches and execute following commands:

SELECT (@group_id:=configuration_group_id) FROM configuration_group WHERE configuration_group_title LIKE ‘UTI Variables’;
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order) VALUES(‘[Recent Products] Max recent products to show’, ‘UTI_RECENT_PRODUCTS_MAX’, ’5′, ‘How many recent products should be displayed in sidebox?’, @group_id, 1);
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order) VALUES(‘[Recent Searches] Max recent searches to show’, ‘UTI_RECENT_SEARCHES_MAX’, ’5′, ‘How many recent search results should be displayed in sidebox?’,@group_id, 2);
Posted in: UTI, Utils

Continue Reading

Randomized Tests version 1.3

2

Randomized tests version 1.3 is here (download). This release brings quite few new features that should be very helpful. I added 95% trust intervals for each statistic. There’s also probability that given experiment will beat control group. You can now also decide when impression occurs. Read below for more.

Installation

Refer to INSTALL.txt in .zip file.

If You already have RT installed refer to INSTALL.txt file for UPGRADE instructions.

Trust intervals

Randomized Tests provides few estimates:

  • order conversion rate
  • visitor to customer conversion rate
  • total items bought
  • total order value

You have to remember that RT presents only estimated values – for example after tracking ten orders RT might calculate that order conversion rate is 0.9%, but You’re almost sure (because You know that from earlier experiments) that Your conversion rate is 1.1%. Unfortunately RT can’t do much better since it hasn’t collected enough data yet. In new version RT will however present also 95% chance intervals for each estimated value. So now instead of conversion rate 0.9% You’ll see 0.9% +/- 0.2% which means that there’s 95% chance that true conversion rate is between 1.1% and 0.7%.

Chance to beat Control Group

Previous versions of RT leaved to You decision if changes You made were actually useful. In version 1.3 RT will give You probability that those changes outpeform original version of Your Zen Cart store. RT displays now ‘Chance to Beat Original’ value. This value should be around 95% or 5% before You decide that new version perform better/worse then original version of Your store.

Custom impression detection functions

This new feature lets You decide if and when to include particular visitor in experiment. You might want for example test performance of Your new customer registration form and include in experiment only those visitors who actually visited customer registration form. Thanks to that You’ll get more accurate results very quickly.

There will be separate post about this new feature – so stay tuned ;)

Download

You can download latests release of RT here:

www.data-diggers.com/contribs/rt/downloads/rt.zip


Continue Reading

New feature in upcoming version of Randomized Tests

0

Yeah, I’m not dead. I don’t have much time but I still work on my modules. I spent most time on Randomized Tests module – sorry to all people counting on new Query Cache version ;) . In upcoming release of Randomized Tests You’ll be able to:

  • decide whether or not impression occurred (and when it happened). For example, You want to count in experiment only visitors that viewed ‘login’ page. Now You can :)
  • get more accurate results. Experiment stats view in Admin area will give You probability that given test group will beat control group. All stats will be also presented with 95% probability range. For example visitor/customer conversion ratio will be presented as: 1% +/- 0.02% which mean that conversion ratio is in [0.98%, 1.02%] with 95% probability.
  • maybe more… for example support for Your own statistics (like ‘search form usage stats’, ‘contact us page stats’ etc).

Would You like to see more features in next version? Let me know through comments / mail.

Sorry, multivariate testing will probably not be included in upcoming version. I’ll add support for it most probably in next next version.


Continue Reading

Randomized Tests V1.2 for Zen Cart Released

0

New version of Randomized Tests for Zen Cart is available. New version introduces few bug fixes and one very important feature – orders filters and customer filter. Order/Customer filters can be used to dynamically remove (not permanently) from statistics certain customers/orders and recalculate statistics without them. Why is it important?

Let’s assume that You have store with many customers. You use Randomized Tests to check how new layout affects sales. Unfortunately after You run Your experiment for few days You notice that one group is biased because it includes one or two top buyers. Those two buyers are different from regular customer – they buy a lot of items, place orders every few days and probably aren’t affected by new layout – they already know Your store and don’t care about it as long as they can place orders.

Group containing these customers is biased since it includes data outliers. You probably would like to remove them from statistics. New version of Randomized Tests can do that using filter functions. Currently there are two types of filter functions:

  • order filter functions – executed for each order in group – allows removal certain orders from statistics.
  • customer filter functions – executed for each customer in group. Orders from rejected customers will not included in statistics.

Randomized Tests V1.2 contain two sample filter functions:

  • ddigers_2std_customer_total_rt_cff() – removes top buyers (formally – customers with total spent in 2+ standard deviation from average) from statistics.
  • ddigers_2std_order_total_rt_off() – removes super big orders from statistics.

To check how they’re implemented view admin/includes/functions/extra_functions/randomized_tests.php

You can implement Your own functions by following below pattern.

Custom Customer Filter Functions

Randomized Tests will recognize any function with name ending with “_rt_cff” as customer filter function and allow You to use it. For example following function will be recognized as filter:

function customer_filter_rt_cff($customer_id) {
        if($cid == 12345) return(FALSE);
        else return(TRUE);
}

Randomized Tests passes to customer filter functions single attribute – id of customer in database. If function returns TRUE customer is accepted in calculation of statistics, and is rejected otherwise.

Order Filter Functions

Order filter functions are similar to customer filter functions – any function with name ending with “_rt_off” will be recognized as order filter function. Example filter function:

function order_filter_rt_off($eid, $gid, $cid, $oid, $itemsBought, $ordersTotal) {
        if($orderTotal > 1000.0) return(FALSE);
        else return(TRUE);
}

Randomized Tests passes few more parameters to order filter functions:

  • $eid – experiment row id in database
  • $gid – group row id in database
  • $cid – customer id in database
  • $oid – order id in database
  • $itemsBought – number of items in order
  • $totalValue – total value of order

As with customer filter functions order filter functions should return TRUE to accept order in statistics and FALSE to reject it.

Making Filter Functions Visible to Randomized Tests Module

Let’s assume that You’ve placed Your custom filter functions in admin/includes/functions/extra_functions/my_custom_filters.php file. The best way to make them visible to RandomizedTests is to include the file through autoloaders interface. Create config.filters.php file in admin/includes/auto_loaders/ and paste there:

$autoLoadConfig[180][] = array(‘autoType’=&gt;‘require’,
‘loadFile’=&gt; DIR_FS_CATALOG . DIR_WS_FUNCTIONS . ‘extra_functions/my_custom_filters.php’);

That’s it – Randomized Tests module will now be able to use Your filters.

Screenshot

Here’s screenshot of experiment with applied filters:

Download

You can download current version of Randomized Tests here.


Continue Reading

Randomized Tests v1.0 Released

0

This module allows You to perform experiments on Your website. You can test for example how Your new ‘Add to Cart’ button performs compared to old one.

I don’t have much time to write long post, so I’ll cut it to most important things. You can download the module here. This module requires UTI to work properly (download it here ). Installation instructions are in .zip file. Here’s how You can use it.

To create experiment follow these steps:

  1. Go to Admin->Tools->[RT] Groupsets.
  2. Create groupset (it’s set of groups). Call it ‘Basic Test Groupset’.
  3. Click on Basic Test Groupset – You will be forwarded to page where You can edit it.
  4. Each groupset must have EXACTLY one control group. I usually call it ‘Control Group’. Create two groups (you can create as many groups in groupset as You want, but for simplicity We use 2 now):
  5. Control Group – with id ‘cg’ (as ControlGroup). Check ‘control group’ checkbox
  6. Test Group – with id ‘tg’. DO NOT check ‘control group’ checkbox.
  7. Go to Admin->Tools->[RT] Experiments.
  8. Create Experiment ‘Exp #1′ using groupset ‘Basic Test Groupset’.

Click on Exp #1 – You will be forwarded to its stats:

  • Impressions - in this version of Randomized Tests its simply number of visitors in each group
  • Customers Registered – (visitor to customer conversion rate) / (change in regard to control group) / (total count of newly registered customers in this group)
  • Orders Placed – (visitor to order conversion rate) / as above / (total number of orders visitors in this group placed)
  • Items Bought – (avg. size of order) / as above / (total number of items bought)
  • Orders Value – (avg. order value) / as above / (total orders value)

Ok, You’ve created experiment, now You have to create two versions of Your page. Let’s assume that You want to change ‘Add to Cart’ button on product_info page. Edit includes/templates/your_template/templates/tpl_product_info_display.php.Find code responsible for displaying ‘Add to Cart’ button – in my case it’s:

echo $display_button;

Change it to:

if($exp->groupID == ‘tg’) {
        // visitor has been assigned to Test Group
        echo $display_my_new_button;
} else {
        // user is in Control Group – We don’t change anything
        echo $display_button;
}

$exp is global variable (it’s instance of ExperimentManager defined in includes/classes/randomized_tests.php) and holds basic information about experiment and group to which has been assigned current visitor
$exp->groupID is ‘id‘ of group that You assigned in step 4. You can use it to detect in which group is current visitor and perform custom action.

You’re done. Wait until experiment becomes statistically significant ( >2000 visitors in each group, many orders ) and check results.


Continue Reading

User Tracking Interface v1.0 Released

3

UTI is tiny module that tracks visitors to Zen Cart site even if they aren’t logged in or even never created any account. It allows developers to keep in UTI memory small amounts of information about each visitor. For example, UTI can be used to store list of recently viewed products by any particular user. UTI will remember this information and when the visitor will come back to Zen Cart store week later UTI will recognize him and present list of previously viewed products.

UTI takes care of:

  • recognizing visitors – even if they never created any account
  • storing and retrieving information about them in database

UTI does not store any personal information in visitors browser.

How to use UTI

UTI is very simple class. It creates global $uti object that contains all information about current visitor. From developer point of view UTI class has four methods:

  • $uti->set($name, $value) - stores in database $value as value for attribute $name
  • $uti->get($name) – retrieves from database value for attribute $name. If there’s no such attribute method returns FALSE.
  • $uti->has($name) – checks if current $uti object has attribute $name set
  • $uti->delete($name) – deletes attribute.

$uti is automatically instantiated through auto_loaders/ mechanism. Developer can use it in any function/class since it’s global variable.

To show how easy it is to use UTI and what it can be used for I’ve created simple contributions that:

  • records and displays in sidebox list of products visitor viewed recently.
  • records and displays in sidebox list of searches visitor performed recently.
  • records when visitor changes currency and saves it as his/hers default. When visitor comes back to Zen Cart contribution automatically switches currency to most recent one.

Example contributions are included in UTI package. You can download it here: current version of UTI.

In next blog post I’ll describe how I’ve created one of those contributions using UTI.


Continue Reading

Social Stuff

Donate

If You like Query Cache, Query Log or Randomized Tests please make a $5 donation.

Search the blog