Magento Customer and Visitor Logging

by Kirk Madera

Engineers reusable modular solutions to maintain environments and deployment strategies across teams. Works for food.

Magento Customer and Visitor Logging

Related to the issue in my previous post about log cleaning, I would like to take some time to discuss customer and visitor logging in a little more detail.

The log_url_info table can bloat your database quickly. It logs every single unique visited URL. I have commonly seen this grow to 5GB or more, even when storing logs for only a short time period. As a general rule, database size has a direct impact on performance.

So what is this data really used for and is there a safe way to reduce it without affecting site functionality?

It seems Magento's primary purpose for this data is analytics. In my opinion, there are much more robust platforms for this like Google Analytics or Omniture which should be used. If anyone sees analytics value in Magento's data over other platforms, please share your experience. Also note that if you are using a proxy cache like Squid or Varnish, a frontend analytics platform like Google Analytics will track data more accurately since requests are not always sent to the server; Varnish will serve a page directly, the Magento app will not run, and the customer visit will not be logged.

These logs also drive the "Online Customers" page in the admin. When you load the "Online Customers" page, the log_visitor table is read and the log_visitor_online table is populated with the users who have visited within the threshold you have set; by default, 15 minutes.

  1. /**
  2.  * Mage_Log_Model_Resource_Visitor_Online
  3.  *
  4.  * This is the method that prepares log_visitor_online for viewing
  5.  * on the "Online Customers" page
  6.  */
  7. publicfunction prepare(Mage_Log_Model_Visitor_Online $object)
  8. {
  9.     // Check if log_visitor_online was recently updated. If so, exit
  10.     if(($object->getUpdateFrequency()+
  11.         $object->getPrepareAt())>time()){
  12.         return$this;
  13.     }
  14.  
  15.     //...
  16.     // Delete existing data from log_visitor_online
  17.     $writeAdapter->delete($this->getMainTable());
  18.  
  19.     //...
  20.     // Build online visitors based on online interval
  21.     $lastDate= Mage::getModel('core/date')->gmtTimestamp()
  22.         -$object->getOnlineInterval()*60;
  23.  
  24.     $select=$readAdapter->select()
  25.         ->from(
  26.             $this->getTable('log/visitor'),
  27.             array('visitor_id','first_visit_at',
  28.                   'last_visit_at','last_url_id'))
  29.         ->where('last_visit_at >= ?',
  30.             $readAdapter->formatDate($lastDate));
  31.  
  32.     // Add additional visitor data
  33.     // ...
  34.  
  35.     // Save the prepared date
  36.     $object->setPrepareAt();
  37.  
  38.     return$this;
  39. }

The time that this was calculated gets stored in cache and prevents the data from being calculated again until a specified amount of time has passed. This time threshold defaults to 60 seconds and is dictated by the "log/visitor/online_update_frequency" config setting. Clearing the cache resets this and causes log_visitor_online to rebuild. Oddly, Magento does not make this variable visible in the admin via a system.xml file. You can edit this in a config file, the database directly, or add your own system.xml definition.

  1. /**
  2.  * Mage_Log_Model_Visitor_Online
  3.  *
  4.  * The time log_visitor_online was built is stored in cache.
  5.  */
  6. publicfunction getPrepareAt()
  7. {
  8.     return Mage::app()->loadCache('log_visitor_online_prepare_at');
  9. }
  10.  
  11. publicfunction setPrepareAt($time=null)
  12. {
  13.     if(is_null($time)){
  14.         $time=time();
  15.     }
  16.     Mage::app()->saveCache($time,'log_visitor_online_prepare_at');
  17.     return$this;
  18. }

I have not found any other direct uses of this data within Magento. My review of this has determined that it is safe to disable this entirely, if you are ok with losing the "Online Customers" functionality. However, I don't recommend this unless you have achieved similar functionality in another analytics platform; seeing online customers is pretty useful. Should you want to do so, here is a good post on how to disable customer and visitor logging. This might even result in a performance improvement since Magento is no longer writing to the log tables on every request.

Recommendation

My recommendation is to reduce the number of days logs are saved to one. This keeps the "Online Customers" list functional, while keeping the table sizes relatively small. You can do this at System > Configuration > Advanced > System > Log Cleaing. If you have a high traffic site, you should also apply the patch mentioned in my previous post that speeds up log cleaning; otherwise, it can become slow, fail to run, and begin to grow larger every day.

Magento Customer and Visitor Logging

If you are reading this, you probably already have huge log tables. You can feel relatively safe about nuking this data, unless you have specific modifications around this. You were only planning on keeping it 180 days anyways. Always do your own research though and always test before you do anything in Production.

How to clear this data:

  1. TRUNCATE log_customer;
  2. TRUNCATE log_quote;
  3. TRUNCATE log_summary;
  4. TRUNCATE log_url;
  5. TRUNCATE log_url_info;
  6. TRUNCATE log_visitor;
  7. TRUNCATE log_visitor_info;
  8. TRUNCATE log_visitor_online;

Magento Customer and Visitor Logging

Magento Customer and Visitor Logging

by Kirk Madera

Engineers reusable modular solutions to maintain environments and deployment strategies across teams. Works for food.

Magento Customer and Visitor Logging

Related to the issue in my previous post about log cleaning, I would like to take some time to discuss customer and visitor logging in a little more detail.

The log_url_info table can bloat your database quickly. It logs every single unique visited URL. I have commonly seen this grow to 5GB or more, even when storing logs for only a short time period. As a general rule, database size has a direct impact on performance.

So what is this data really used for and is there a safe way to reduce it without affecting site functionality?

It seems Magento's primary purpose for this data is analytics. In my opinion, there are much more robust platforms for this like Google Analytics or Omniture which should be used. If anyone sees analytics value in Magento's data over other platforms, please share your experience. Also note that if you are using a proxy cache like Squid or Varnish, a frontend analytics platform like Google Analytics will track data more accurately since requests are not always sent to the server; Varnish will serve a page directly, the Magento app will not run, and the customer visit will not be logged.

These logs also drive the "Online Customers" page in the admin. When you load the "Online Customers" page, the log_visitor table is read and the log_visitor_online table is populated with the users who have visited within the threshold you have set; by default, 15 minutes.

  1. /**
  2.  * Mage_Log_Model_Resource_Visitor_Online
  3.  *
  4.  * This is the method that prepares log_visitor_online for viewing
  5.  * on the "Online Customers" page
  6.  */
  7. publicfunction prepare(Mage_Log_Model_Visitor_Online $object)
  8. {
  9.     // Check if log_visitor_online was recently updated. If so, exit
  10.     if(($object->getUpdateFrequency()+
  11.         $object->getPrepareAt())>time()){
  12.         return$this;
  13.     }
  14.  
  15.     //...
  16.     // Delete existing data from log_visitor_online
  17.     $writeAdapter->delete($this->getMainTable());
  18.  
  19.     //...
  20.     // Build online visitors based on online interval
  21.     $lastDate= Mage::getModel('core/date')->gmtTimestamp()
  22.         -$object->getOnlineInterval()*60;
  23.  
  24.     $select=$readAdapter->select()
  25.         ->from(
  26.             $this->getTable('log/visitor'),
  27.             array('visitor_id','first_visit_at',
  28.                   'last_visit_at','last_url_id'))
  29.         ->where('last_visit_at >= ?',
  30.             $readAdapter->formatDate($lastDate));
  31.  
  32.     // Add additional visitor data
  33.     // ...
  34.  
  35.     // Save the prepared date
  36.     $object->setPrepareAt();
  37.  
  38.     return$this;
  39. }

The time that this was calculated gets stored in cache and prevents the data from being calculated again until a specified amount of time has passed. This time threshold defaults to 60 seconds and is dictated by the "log/visitor/online_update_frequency" config setting. Clearing the cache resets this and causes log_visitor_online to rebuild. Oddly, Magento does not make this variable visible in the admin via a system.xml file. You can edit this in a config file, the database directly, or add your own system.xml definition.

  1. /**
  2.  * Mage_Log_Model_Visitor_Online
  3.  *
  4.  * The time log_visitor_online was built is stored in cache.
  5.  */
  6. publicfunction getPrepareAt()
  7. {
  8.     return Mage::app()->loadCache('log_visitor_online_prepare_at');
  9. }
  10.  
  11. publicfunction setPrepareAt($time=null)
  12. {
  13.     if(is_null($time)){
  14.         $time=time();
  15.     }
  16.     Mage::app()->saveCache($time,'log_visitor_online_prepare_at');
  17.     return$this;
  18. }

I have not found any other direct uses of this data within Magento. My review of this has determined that it is safe to disable this entirely, if you are ok with losing the "Online Customers" functionality. However, I don't recommend this unless you have achieved similar functionality in another analytics platform; seeing online customers is pretty useful. Should you want to do so, here is a good post on how to disable customer and visitor logging. This might even result in a performance improvement since Magento is no longer writing to the log tables on every request.

Recommendation

My recommendation is to reduce the number of days logs are saved to one. This keeps the "Online Customers" list functional, while keeping the table sizes relatively small. You can do this at System > Configuration > Advanced > System > Log Cleaing. If you have a high traffic site, you should also apply the patch mentioned in my previous post that speeds up log cleaning; otherwise, it can become slow, fail to run, and begin to grow larger every day.

Magento Customer and Visitor Logging

If you are reading this, you probably already have huge log tables. You can feel relatively safe about nuking this data, unless you have specific modifications around this. You were only planning on keeping it 180 days anyways. Always do your own research though and always test before you do anything in Production.

How to clear this data:

  1. TRUNCATE log_customer;
  2. TRUNCATE log_quote;
  3. TRUNCATE log_summary;
  4. TRUNCATE log_url;
  5. TRUNCATE log_url_info;
  6. TRUNCATE log_visitor;
  7. TRUNCATE log_visitor_info;
  8. TRUNCATE log_visitor_online;