Expanding Statcomm: statcomm_info action

While Stacomm 1.7.0 is around the corner (planned for June 1st.) let’s talk about a new feature already included in the current version. Since version 1.6.90 it is possible to use data provided from Statcomm to do a lot of things like redirect traffic, send email when a user get a 404 (page not found) or manage advertising depending on user browser’s platform.

This is a long post, but we promise you’ll learn new ways to expand your site control beyond the plugin capabilities.

The statcomm_info action internals

This action fires inmediately after Statcomm saves the incoming visitor data to the database.

First, we want to make clear about when statcomm_info action is NOT triggered:

  • If Maxmind database is deactivated, this action won’t be triggered. (already improved in the next Statcomm release)
  • If you are logged to your wordpress site and the setting ‘Also collect data about logged users’ is deactivated. In this case yourself will be unable to trigger the action from the same browser session.
  • The incoming visitor is filtered because its IP is banned (listed in banips.dat file)
  • The incoming traffic is filtered (because is a filtered extension, or a filtered path)

We recommend test this setting accessing as anonymous visitor to your site or activate logged users tracking option.

The Basics

Let’s start with a simple example. Paste this code into the functions.php in your current WordPress theme:

function currentUserInfo($currentUser)
{
    echo "";
}

add_action('statcomm_info','currentUserInfo');

When the action is triggered, you receive a $currentUser object with detailed user information. What we are doing here is appending the current user information in the WordPress site as a comment. It is a trickier, but fast way to display the information available.
After pasting and saving, go to your site and refresh any page. Then select view the page source and you will find something similar to this structure:

< ! - -  statcommCurrentUser Object (
    [_date:private] =--> 20120529
    [_time:private] => 09:39:02
    [_ip:private] => 200.125.16.130
    [_urlrequested:private] =>
    [_agent:private] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; iOpus-Web-Automation; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; FDM)
    [_referrer:private] =>
    [_search:private] =>
    [_nation:private] => uy
    [_os:private] => Windows XP
    [_browser:private] => IE
    [_searchengine:private] =>
    [_spider:private] =>
    [_feed:private] =>
    [_user:private] =>
    [_threat_score:private] => 0
    [_threat_type:private] => 0
    [_timestamp:private] => 1338284342
    [_statuscode:private] => 200
    [_userAgent:private] => Array
        (
            [typ] => Browser
            [ua_family] => IE
            [ua_name] => IE 7.0
            [ua_version] => 7.0
            [ua_url] => http://en.wikipedia.org/wiki/Internet_Explorer
            [ua_company] => Microsoft Corporation.
            [ua_company_url] => http://www.microsoft.com/
            [ua_icon] => uas-msie-png
            [ua_info_url] => http://user-agent-string.info/list-of-ua/browser-detail?browser=IE
            [os_family] => Windows
            [os_name] => Windows XP
            [os_url] => http://en.wikipedia.org/wiki/Windows_XP
            [os_company] => Microsoft Corporation.
            [os_company_url] => http://www.microsoft.com/
            [os_icon] => os-windowsxp-png
        )

    [_geoLocation:private] => geoiprecord Object
        (
            [country_code] => AR
            [country_code3] => ARG
            [country_name] => Argentina
            [region] => 10
            [region_name] => Puerto Madero
            [city] => Buenos Aires
            [postal_code] =>
            [latitude] => -34.8581
            [longitude] => -56.1708
            [area_code] => 0
            [dma_code] => 0
            [metro_code] => 0
            [continent_code] => SA
            [continent_name] => South America
        )

)
-->

 

Quite a lot stuff, so let’s split what do we have here. Basically you get one object with visitor information and also one array (getUserAgent), with explicit OS and Browser information and other object with geoLocation (getGeolocation) data. All this information is available at your disposal.

The statcommCurrentUser Object

This object contains a detail of the last visitor on you web,wether is a user or a spider. the statcommCurrentUser handles all the information saved in the database , along with a detail of the userAgent (getUserAgent) and geoLocation additional data(getGeoLocation). The following methods are available:  

getDate – returns current date  

getTime– return visitor’s time  

getIp – return visitor’s ip    

getUrlrequested – return url requested , without domain  

getAgent – User Agent raw version 

getReferrer– return where the visitor came from.  

getSearch– visitor search , if it had one    

getNation– nation from the visitor (stripped from host ip address)  

getOs – visitor’s Operative System  

getBrowser – visitor’s Browser  

getSearchengine – visitor’s search engine who came for (if it had one)    

getSpider – spider if the visitor is an spider. If not, this is empty  

getFeed – visitor’s feed type information  

getUser – user if logged user options is activated    

getThreatScore – not enabled in 1.6.90, reserved for future development 

getThreatType – not enabled in 1.6.90, reserved for future development

  getTimestamp – timestamp in Unix format    

getLanguage – (available in 1.7.0) – default user language    

getStatuscode – return status code for current user    

getUserAgent  -information available as array    

getGeoLocation – object with geolocation information

 

getUserAgent will return an array with the following information:

[typ] => Browser -type of agent (Browser,Robot,Offline Browser, Email client,Library,Wap Browser, Validator, Feed Reader, Multimedia Player, Other, Useragent Anonymizer)

[uafamily] => IE – browser family [uaname] => IE 7.0 – Browser name

[uaversion] => 7.0 – Browser version

[uaurl] => http://en.wikipedia.org/wiki/InternetExplorer – browser’s wikipedia url

[uacompany] => Microsoft Corporation. – browser company

[uacompanyurl] => http://www.microsoft.com/ browser url company

[uaicon] => uas-msie-png – associated browser icon

[uainfourl] => http://user-agent-string.info/list-of-ua/browser-detail?browser=IE – url browser user agent info

[osfamily] => Windows – os family

[osname] => Windows XP – os name

[osurl] => http://en.wikipedia.org/wiki/WindowsXP – url to wikipedia about os

[oscompany] => Microsoft Corporation. – os company

[oscompanyurl] => http://www.microsoft.com/ – os company url

[osicon] => os-windowsxp-png – associated os icon    

 

getGeoLocation will return an object with the following information

[countrycode] => AR – Country code (2 chars)

[countrycode3] => ARG – Country code (3 chars) [countryname] => Argentina – Country name

[region] => 10 – region Number

[regionname] => Puerto Madero – region Name

[city] => Buenos Aires – region City

[postalcode] =>  Postal code

[latitude] => -34.8581 – Latitude

[longitude] => -56.1708 – Longitude

[areacode] => 0 – Area Code

[dmacode] => 0 – DMA code

[metrocode] => 0 – Metro Code

[continentcode] => SA – Continent code

[continent_name] => South America – Continent name

Examples:

Let’s see what we could do with this information and couple of lines on functions.php:

Sending an email when a 404 error is reached

 

function email404($currentUser)
{
    if ($currentUser->getStatuscode()=='404')
    {
        $message="Error trying to access: " .
                  $currentUser->getUrlrequested();
        wp_mail ("destination-mail@domain.com",
                  "404 Error on site", $message);
    }

}
add_action('statcomm_info','email404');

Redirect visitors to custom page(s) if  the browser has some conditions

//This example redirects any user which
//has IE previous version 7 to a custom page
//Notice we are using permalinks,
//but other options are available
 function redirectIE($currentUser)
{
//Warning: no data can be echoed here
//(headers already being written error)
$page='ie-not-supported';
//If the user is already in this page, abort.
if (stripos($currentUser->getUrlrequested(),
            $page)!==false) return; 

    $ua=$currentUser->getUserAgent();
    if ($ua['ua_family'] =='IE'
            and intval($ua['ua_version'])<7)
    {
        wp_redirect($page);
        die();
    }
}
add_action('statcomm_info','redirectIE');

 

Ban user(s) from some IP

//redirect banned ip to a specific page.
function banIP($currentUser)
{
$ip='189.99.220.15'; //an example
$page='user-banned';
if (stripos($currentUser->getUrlrequested(),
    $page)!==false) return;
    if ($currentUser->getIp() == $ip)
    {
        wp_redirect($page);
    }
}
add_action('statcomm_info','banIP');

Create a shortcode to display ad advertising for iphone users

//Create a shortcode to display advertising
//only if the user uses an iphone.
//Register a new shortcode: [iphone_ad]
//The solution has two parts:
//1)detecting the iPhone users and saving a flag
//2)display image depending on the flag
//Use the shortcode [iphone_ad] in a post or page to display
//an image . In this example, image are on
//wp-content/uploads folder.
//Notice also statcomm_info triggers before shortcode processing

add_shortcode( 'iphone_ad', 'iphone_image' ); 

function iphone_image()
{
  //get the options
  $options = get_option('statcomm_options');
  $iphone_option=(bool)$options["iphone_ad"];
  $contUrl= content_url() . "/uploads/";
  $image=((bool)$iphone_option)?"iphone-image-ad.jpg":"other-image-ad.jpg";
  echo  "";
} 

//Detect iphone users
//Notes:
//1- we reuse statcomm_options but an independent option can be used
//2- 'iPhone OS' is extracted from uasdata.ini file
//3- Details about boolean handling on
//http://www.php.net/manual/en/language.types.boolean.php
function iphone_detect($currentUser)
{
  //Get Statcomm options.
  $options = get_option( 'statcomm_options' );
  //If we got an iphone, set a option value to true or false
  $ua=$currentUser->getUserAgent();
  $options["iphone_ad"] =($ua['ua_family'] == "iPhone OS")?true:false;
  update_option('statcomm_options',$options);
}

add_action('statcomm_info','iphone_detect');

This examples just are scratching the surface of new Statcomm’s features. We hope you get an idea of the new capabalities and take advantage of this new feature to make smarter sites.

Enjoy the day!

2 Comments


  • Hi, I have Just find this type of post today. Its great List, hope you can update and give us more do-follow blogs. http://www.cpcindia.in Thanks!!!

    • admin
      Reply Cancel Reply
    • July 9, 2012

    Thanks for your comments. We'll continue improving the plugin in daily basis and let you know the news.

Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*