in Technology

Effective way to update your blog’s header scripts – MyBlogLog, Google Analytics, Mint

With a plethora of tracking services, blog tags and your own tracking softwares; you need to insert scripts to your blog’s header now and then. Most scripts give you the option to tie easily with commonly used CMS and Blogging Tools like WordPress, Movable Type, TypePad, Blogger etc.

However, the problem is that most of them have to deal with change in your theme and if you change your theme, you have to update the scripts again. And all of them do not follow the same approach; a typical implementation is different amongst all of them.


Let me tell you how I do mine and thus make it hassle free once you set it up. We’ll need to play with Apache’s .htaccess and PHP’s auto_prepend_file. These options are very common on most *nix based Web Host and are enabled by default, if not, request your hosting provider to enable it for you. This is a PHP script and will work with PHP (and html, htm instructed to act like PHP). The procedure is independent of WordPress or any Blogging Tool or CMS and thus will not affect your themes, styles.

Create a new “.htaccess” file or edit your current one if you already have one. Most wordpress blogs will have one for sure. Remember to create a back-up copy just in case you need to revert back. A typical “.htaccess” will look something like (at least in my case)

# set "utf-8" as the default character encoding
AddDefaultCharset utf-8

# prevent directory listing entirely, use + to enable
Options -Indexes

<IfModule mod_rewrite.c>
# Rewrite www.domain.com request to domain.com
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]

# BEGIN WordPress Redirect for customized permalinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Download the above code.

Edit the “.htaccess” to include the following line (you can ignore the comments). This will allow “.htaccess” to prepend the file, “config.php” in this case wherever this “.htaccess” is valid. It is best to have this before the mod_rewrite module. Before that canonical URL redirect in this case (that www to non www thingy).

# auto append the config, includes mint, google analytics and the config settings
# this is the physical of the file location of your config setting
php_value auto_prepend_file /home/MyDomain/public_html/config.php

Now, for the file that is being prepended – “config.php”.

function Config($page)  {

$googleAnalytics = '
//your google analytics script goes here
';

$quantcast = '
//the quantcast script goes here
';

$mybloglog = '
//your MyBlogLog Script here
';

$pages	= array(); // Add pages (relative to the public site root) that should be ignored
$ip		= array(); // Add IP addresses that should be ignored, for instance you don't want to track yourself or your company's visit to your blog.

if (
	strpos($page,'frameset') !== false || 
	(!empty($ip) && in_array($_SERVER['REMOTE_ADDR'], $ip)) || 
	(!empty($pages) && in_array((isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF']))?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME'], $pages))
	)
{ return $page; }
$replace = array
	(
		'</head>',
		'</HEAD>'
	);
	return str_replace($replace, "\n{$googleAnalytics}\n\n{$quantcast}\n\n{$mybloglog}\r</head>", $page);
}
ob_start("Config");
?>

Download the above code.

So, your actual tag in your html is replaced with the following scripts, here it is – Google Analytics, MyBlogLog and Quantcast. Next time, you need to add a script or update, you just have to change this “config.php”.

What if I want to ignore some directories?

Drop another “.htaccess” in the root of a directory or sub-domain to deactivate the auto_prepend_file. The content of the “.htaccess” will be
php_value auto_prepend_file none

My Blogging tool spit out html and not PHP?
If your blogging tool or CMS churns out html, htm and not PHP even though your host is PHP enabled. Then add this line in your “.htaccess” file (it will allow html and htm to act like PHP)
AddType application/x-httpd-php .html .htm

It does not work as soon as I changed my MyBlogLog insert script!
MyBlogLog script uses single quotes and thus it conflict with this PHP script, so just change that to double quotes. The example in this case, have the changed double quote instead of MyBlogLog’s single quote.

However, if you are with the new Grid Server on Media Temple like me, then that would be a bit different and should be like

<FilesMatch "\.(html|htm)$">
SetHandler php4-script
</FilesMatch>
  1. Great php mixed headers article! I just del.icio.us'd you!
    check out: Force Apache to output any HTTP Status Code with ErrorDocument, Ultimate htaccess article

  2. Great php mixed headers article! I just del.icio.us'd you!
    check out: Force Apache to output any HTTP Status Code with ErrorDocument, Ultimate htaccess article

  3. Awesome post! Inspired me to write a similar article: Use php.ini to replace html with anything

  4. Awesome post! Inspired me to write a similar article: Use php.ini to replace html with anything

Comments are closed.

Webmentions

  • Google updates Analytics - Brajeshwar Apr 10, 2007

    [...] Dec, 2007 — My own method of auto_prepend_file of a header script through .htaccess is conflicting with the new Google Analytics script and thus is not working properly on this site. [...]