In response to a client’s complaint that the counter associated with ‘track_views’ template variable was not even remotely close to google analytics page view numbers, and they weren’t happy with the standard explanation of the raw count mechanics used by EE, I came up with a modification to improve things.
First, in order to minimize the impact on page load and processing, I modified the CI library ‘User Agent’ (sample below - attachments won’t accept code) rather that make an EE override version. Although my new version is leaner, it is also backwards compatible with CI’s version. The reason for the changes is that the default behavior has too much overhead. I revised the __construct($compile=FALSE) by adding an optional parameter to specify whether or not to execute the _compile_data() script. _compile_data() has too much process overhead and is not necessary to run every time. Even though the default behavior is to not compile the UA data and this seems to not be backwards compatible, the redo of each class method negates this and thereby keeps it backwards compatible and faster. Each UA data attribute requested drives the associated initialization of the UA segment parser. Anyway, most of the script needed some tweaks to make this all work together. Please review and consider this update back up the CU chain.
Next, to use this addition, I added this code to the EE modules/channel/mod.channel.php (lines 818 ff):
$this->EE->load->library(‘user_agent’); /* For some weird reason, CI remaps ‘user_agent’ to ‘agent’ (Loader.php: $_ci_varmap) */ if ($this->EE->agent->is_robot()) { return; }
Please consider this improvement to the view count mechanics.
-Jim
Partial code due attachment and message limitations:
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. * @license http://ellislab.com/codeigniter/user-guide/license.html * @link http://codeigniter.com * @since Version 1.0 * @filesource */
// ————————————————————————
/** * User Agent Class * * Identifies the platform, browser, robot, or mobile devise of the browsing agent * * IMPORTANT NOTE: Contrary to normal operations, when CI loads this particular library via * $CI->load->library(‘user_agent’), it is usable via $CI->agent->method() * * @package CodeIgniter * @subpackage Libraries * @category User Agent * @author ExpressionEngine Dev Team * @link http://ellislab.com/codeigniter/user-guide/libraries/user_agent.html */ class CI_User_agent {
public $agent = NULL;
public $is_browser; /* unset by default - loaded on demand */
public $is_robot; /* unset by default - loaded on demand */
public $is_mobile; /* unset by default - loaded on demand */
public $is_platform; /* new flag: unset by default - loaded on demand */
public $languages = array();
public $charsets = array();
public $platforms = array();
public $browsers = array();
public $mobiles = array();
public $robots = array();
public $platform = '';
public $browser = '';
public $version = '';
public $mobile = '';
public $robot = '';
/**
* Constructor
*
* Sets the User Agent and runs the compilation routine if requested
*
* @access public
* @return void
*/
public function __construct($compile=FALSE)
{
if (isset($_SERVER['HTTP_USER_AGENT']))
{
$this->agent = trim($_SERVER['HTTP_USER_AGENT']);
}
if ( ! is_null($this->agent))
{
/* Do not auto-compile. Instead, compile on-demand */
if ($this->_load_agent_file() AND $compile !== FALSE)
{
$this->_compile_data();
}
}
log_message('debug', "User Agent Class Initialized");
}
// --------------------------------------------------------------------
/**
* Compile the User Agent Data
*
* @access private
* @return bool
*/
public function _load_agent_file()
{
if ( ! @include(APPPATH.'config/user_agents'.EXT))
{
log_message('debug', "User Agent configuration file failed to be loaded");
return FALSE;
}
log_message('debug', "User Agent configuration file loaded");
$return = FALSE;
if (isset($platforms))
{
$this->platforms = $platforms;
unset($platforms);
$return = TRUE;
}
if (isset($browsers))
{
$this->browsers = $browsers;
unset($browsers);
$return = TRUE;
}
if (isset($mobiles))
{
$this->mobiles = $mobiles;
unset($mobiles);
$return = TRUE;
}
if (isset($robots))
{
$this->robots = $robots;
unset($robots);
$return = TRUE;
}
return $return;
}
// --------------------------------------------------------------------
: : : /** * Set the Robot * * @access private * @return bool */
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.