Archive

Archive for October, 2009

CRE Loaded Block unwanted countries traffic by IP

October 31st, 2009 Comments off

One of site i worked on, was having a lot of crawler and normal visitor activities, from non targeted countries. For example it was having a lot of visitors and un friendly bots (crawlers) from India. This traffic was useless for the site because site was to serve Europe and US, still it was consuming a lot of processing and bandwidth.

The solution was to block all the traffic from such countries but could not find and extension to block specified countries from our site. Finally we decided to write a simple extension that can detect country of visitor and block the visitor if its country is in our block list.

The site was based on CRE Loaded so i thought it would be useful extension for other people who experiences similar issues. The extension is very simple and created using RCI, so it dose not get disturbed by any CRE Loaded updates.

The extension requires free country to IP database from MaxMind and its PHP API. These can be found on http://www.maxmind.com/app/geoip_country and http://geolite.maxmind.com/download/geoip/api/php/ respectively.

Installation of this extension is easy and include following steps

Step 1: Copy file GeoIP.dat (IP to country database) and geoip.inc (API) in includes/ directory under your installation.
Setp 2: Run following SQL queries on your database. This will add configuration to enable/disable this extension and will let you specify list of countries to block.

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Enable Block Countries', 'BLOCK_COUNTRIES_ENABLE', 'false', 'Block traffic and spiders from specific countries. by http://blog.wasimasif.com', '1', '999', NOW(), NOW(), '', 'tep_cfg_select_option(array(\'true\', \'false\'),') ;
INSERT INTO configuration (configuration_title, configuration_key, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Blocked Countries List', 'BLOCK_COUNTRIES_LIST', 'Comma separated list of country codes to block traffic from. For example AL,AM will block all traffic from Albania and Armenia. By http://blog.wasimasif.com', '1', '1000', NOW(), NOW(), '', 'tep_cfg_textarea(') ;

Step 3: Create a new file blockcountry_applicationtop_bottom.php in includes/runtime/applicationtop/ folder and paste the following code in this file.

<?php
/**
* Block contries by http://blog.wasimasif.com/
* Blocks unwanted traffic on your site
* @since 2009-10-28
*/
if(BLOCK_COUNTRIES_ENABLE == 'true' && BLOCK_COUNTRIES_LIST != ''){ // check if block countries is enabled
if(!isset($_SESSION['cok']) || intval($_SESSION['cok']) == 0){ // We have not checked country yet
$ip_address = tep_get_ip_address(); // get IP
include(DIR_FS_INCLUDES."geoip.inc"); // include geoIP API
$gi = geoip_open(DIR_FS_INCLUDES."GeoIP.dat",GEOIP_MEMORY_CACHE); // open GeoIP database
$country_code = strtoupper(geoip_country_code_by_addr($gi, $ip_address)); // Get country code
geoip_close($gi); // Close the IP database

$blocked_countries = explode(‘,’,strtoupper(BLOCK_COUNTRIES_LIST));

for($i=0; $i < count($blocked_countries);$i++)
$blocked_countries[$i] = trim($blocked_countries[$i]); // avoid any trailing spaces etc

if($country_code != “” && in_array($country_code,$blocked_countries)){ // country code is in block list
header(‘HTTP/1.1 403 Forbidden’);
die(“<h1>Forbidden</h1>”);
}
else{
$_SESSION['cok'] = 1; // register this in session
}
}
}
// end block countries
?>

Now you are done with the installation. Now enable the extension and provide list of blocked country in Admin > Configuration > My Store. You have to specify a list of comma separated two latter country codes to block.

Use the code on your own risk no warranties what so ever is provided. The code is tested with CRE Loaded 6.4 Pro on linux but should work with 6.3.x and windows.

Categories: CRE Loaded Tags:

Excluding traffic stats from specific countries in Google Analytics

October 25th, 2009 2 comments

Google Analytics (GA) provides comprehensive web traffic stats and being used by a huge number of websites. For many small and medium size sites traffic other then specific countries is not very useful. For example while calculating conversion ratio you may not want to include traffic from all parts of the world if you are only targeting one or few countries. It is a tough ask to manually exclude non targeted traffic stats from such reports.

Google Analytics provide option to exclude traffic stats based on the region or country of visitor. In this post I will show how to exclude traffic stats using this feature.

To start login to your GA account and click “Edit” link on profile in which you want to exclude traffic stats. You will see a section “Filters Applied to Profile” on next page. This will show any existing filter you already have setup on website profile. Click on “+ Add Filter” link to add exclude filter for countries. On this screen choose “Custom filter” in filter type field,the screen should look like image below.

google_analytics-01

Here you can provide name of the filter as you like, i have used “Exclude Countries” for this example filter. Now you all have to do is to put country names you want to exclude separated with | sign, and enclose it with ( ) in Filter Pattern field. For example if you want to exclude traffic stats from China, Australia and India theĀ  filter pattern will be (China|Australia|India) . And that’s all, now save this filter by clicking “Save Changes” button. The screen should look similar to screen below. If you only want to exclude specific county then the pattern will be simply the name of the country.

google_analytics-02

Note: This will only exclude stats from all GA reports, it will not block any visitors to visit your website.

Categories: SEO Tags:

Moved to blog.wasimasif.com domain

October 25th, 2009 No comments

Hello, I have moved my blog to my own domain and hosting. The main reason to move on own domain was restrictions applied by wordpress.com on advertising, plugins etc. So all new posts from now on will be posted on this domain. All of my old posts will be available on the previous blog and they eventually will be imported here or will get discarded.

Categories: Uncategorized Tags: