Example are provided for Conversion module version now lower than 15.5.6.
Counters
Conversion module performs counts number of visitors per day as follows:
$context = Bitrix\Conversion\DayContext::getInstance(); // current day and user context
$context->addDayCounter('conversion_visit_day', 1); // add 1 to the counter 'conversion_visit_day' in this context
For example, let's overview task of counting number of visits to the root '/' site page.
Code must be placed to bitrix/php_interface/init.php.
Attention! Error in file
init.phpinit.php - optional file within Bitrix Framework file structure. It automatically connects in prolog. Learn more...
results in a complete site performance failure. That's why any modifications and updates must be subjected by competent developer.
class MyConversionHandlers
{
// driving up your counter numbers
static function addCounters()
{
if ($_SERVER['REQUEST_URI'] == '/' && Bitrix\Main\Loader::includeModule('conversion'))
{
$context = Bitrix\Conversion\DayContext::getInstance();
$context->addDayCounter('my_page_day', 1); // add 1 to counter my_page_day once per day
$context->addCounter ('my_page_all', 1); // add 1 to counter my_page_all
}
}
// data on your counters for Conversion module
static function getCounterTypes()
{
return array(
'my_page_day' => array('MODULE' => 'my', 'NAME' => 'Visits per day', 'GROUP' => 'day'), // day group informs about action counter
'my_page_all' => array('MODULE' => 'my', 'NAME' => 'Total visits'),
);
}
}
// register counters in the system
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler('main' , 'OnProlog' , array('MyConversionHandlers', 'addCounters' ));
$eventManager->addEventHandler('conversion', 'OnGetCounterTypes', array('MyConversionHandlers', 'getCounterTypes'));
Method addDayCounter adds a single counter for user once per day, independently from how many times it is called. It is used for calculating conversion.
Method addCounter increases counters for the specific number of calls. Use it for data accumulation.
Now, go to the Conversion module settings and make sure that for
'my'
Indicate the module you require supported by the Conversion module instead of 'my'. См. урок Module settings
that conversion accounting has been checked. That's all is needed for starting counting visits on your site root page. You need to add one more method to our class to calculate and display conversion by these counters:
// data for calculating the conversion
static function getRateTypes()
{
return array(
'my_page' => array(
'NAME' => 'Main page visits',
'MODULE' => 'my',
'SORT' => 100, // sequence for conversion display
'SCALE' => array(0.5, 1, 1.5, 2, 5), // scale: Bad 0% - Excellent 5%
// counters to be passed to conversion calculation function
'COUNTERS' => array('conversion_visit_day', 'my_page_day', 'my_page_all'),
// conversion calculation function
'CALCULATE' => function (array $counters)
{
$denominator = $counters['conversion_visit_day'] ?: 0; // denominator
$numerator = $counters['my_page_day' ] ?: 0; // numerator
$quantity = $counters['my_page_all' ] ?: 0;
return array(
'DENOMINATOR' => $denominator,
'NUMERATOR' => $numerator,
'QUANTITY' => $quantity,
'RATE' => $denominator ? $numerator / $denominator : 0, // conversion formula
);
},
),
);
}
Now conversion by counters will be calculated and displayed at the Conversion pulse page.
Context
Context is a unique set of attributes.
These attributes that are registered by counters are set for each user and active during a day. For example, Conversion module sets context attributes as follows:
$context = Bitrix\Conversion\DayContext::getInstance();
$сontext->setAttribute('conversion_site', $siteId); // specifies attribute 'conversion_site' with the variable value $siteId
Thanks to the attribute 'conversion_site' you can filter all counters only by a specific site.
For example, the objective is to determine the mood or gender for a person who visited the site. For example, let's generate this data at random. Let's add two new methods to the class MyConversionHandlers:
// setting our context attributes
static function setDayContextAttributes(Bitrix\Conversion\DayContext $context)
{
// set attribute 'my_mood' with the value from 1 to 3
$context->setAttribute('my_mood', rand(1, 3));
// set attribute 'my_male_gender' or 'my_female_gender' without values
$context->setAttribute('my_'.(rand(0, 1) ? '' : 'fe').'male_gender');
}
// information about our attributes for the Conversion module
static function getAttributeTypes()
{
return array(
'my_mood' => array(
'NAME' => 'Mood',
'MODULE' => 'my',
'SORT' => 100, // set of attributes
'GET_VALUES' => function (array $ids) // information about attribute values
{
$values = array();
$moods = array(
1 => array('NAME' => 'Bad' ),
2 => array('NAME' => 'Normal'),
3 => array('NAME' => 'Good' ),
);
foreach ($ids as $id)
if (isset($moods[$id]))
$values[$id] = $moods[$id];
return $values;
},
),
'my_male_gender' => array(
'NAME' => 'Males',
'MODULE' => 'my',
'SORT' => 200,
'GROUP' => 'gender', // attribute group
'BG_COLOR' => '#be6ac4', // attribute color at the Conversion pulse
'SPLIT_BY' => 'my_mood', // split by mood at the detailed page
),
'my_female_gender' => array(
'NAME' => 'Females',
'MODULE' => 'my',
'SORT' => 300,
'GROUP' => 'gender',
'BG_COLOR' => '#4bbedb',
'SPLIT_BY' => 'my_mood',
),
);
}
Now context attributes will be set for each user everyday.
Now user that already visited our site at a specific day before new attributes were added - the context has been already set for this user without these attributes. To re-define context, without waiting the next day, you need to clear the cookies PHPSESSID and BITRIX_CONVERSION_CONTEXT for the site in browser.
If you enter the Conversion pulse page, the dropdown menu that appears with the list of attribute groups will have a new item 'gender'. To define a more clear name to the created group, add one more method:
// information about groups for the Conversion module
static function getAttributeGroupTypes()
{
return array(
'gender' => array('NAME' => 'Пол', 'SORT' => 300),
);
}
As can be noticed, attributes may or may not have values. All depends on how data must be organized or displayed.
For example, you can skip displaying two attributes without values: 'my_male_gender' and 'my_female_gender', and use only one: 'my_gender', with values: 'male' and 'female'. But, in this case, there are several limitations.
In case a creator of another module decides that he or she requires another attribute variant in the 'gender' group, the creator cannot just simple add one more attribute 'his_neuter_gender' in his/her module, all setup must be updated.
Highlights
Attributes having GROUP will be available at the Conversion pulse page.
Details page presently has a selection by attribute having values, specified in the field SPLIT_BY, but it will have an option to build complex filters with different variants of attributes and values.
If several attributes with a single group are set, the system uses the attribute with the lesser SORT field.