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

Easy and effective way to take control of your header and update it through one single file - thus making it easy to update statistics codes, javascript injection etc.

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>


Don't like it? There are lots of published articles, pick a random one.

oCricket

Brajeshwar posted this article on Thu, Feb 8th, 2007 at 12:41 am
Categorized under Technology and has the following tags

Suggest 1 or more tags for this article

Use a comma (,) to separate 2 or more tags.

[?]

Prev Article: I’m perhaps the 2nd Top Indian Digger

Next Article: Do you want to be part of the Feedburner Ad Network?


Possibly Related Articles

Archives: Visit the Archives for more articles.

Comments Post Yours

There are 5 responses so far. You can follow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site.

  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. This is exactly what I was looking for. Thank you.

    (Your config.php seems to missing

  3. @Lloyd - It is included in the Download file. I am still trying to find a nice way of showing codes on my blog.

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

  1. Pings & Trackbacks Sites, articles & blog posts linking back to this article.

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

Spawning does Django

Our team had a discussion yesterday why Spawning might be a good solution for our Python-Django specific Web server. The discussion is still hot on the table and have not come to a conclusion; nonetheless, ...2nd Aug, 2008

Take the A List Apart 2008 Survey

It's A List Apart's second annual survey for people who make websites. I took it! And so should you. The Survey for People Who Make Websites. This year's survey corrects many of last year's mistakes, with ...29th Jul, 2008

Django 1.0 alpha released

Django Project have released Django 1.0 Alpha today (22nd Jul, 2008 IST). If this is the timeline Django would stick to; then, Django 1.0 final release should hit the web on 2nd Sep, 2008. Alpha ...22nd Jul, 2008

The Continuum, a massive Flash Game

[flv:http://media.brajeshwar.com/v/games/the-continuum-trailer.flv 640 360] Watch The Continuum Trailer on YouTube Saw this at TechCrunch -- The Continuum: Flash Gaming Goes Hardcore. The Continuum is a new Flash game, featuring an extremely polished interface and deep ranking system. The ...12th Jul, 2008

Meez got a $20,000 cash prize for Flash Game Developers

Graphics from MeezMeezCreate your own 3D experience. Meez is organizing a Avatar Games contest for Flash Game Developer. There's $20,000 worth of cash in prize money. Meez has partnered with FlashGameLicense to run this contest starting ...9th Jul, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.
  • Forum Oinam’s technical discussion forum where developers and designers can discuss all technical topics.
  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • Not Safe for Work Ever clicked a link and felt embarrassed with the content in front of your co-workers? Ever caught unaware because the funny link your friend sent was a little beyond funny? Let’s minimize that with NSWF.
  • Oinam The official conglomerate of the Oinam related companies, institutes and holdings.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • Downloads All downloads, Free and Open Source.
  • Financial services is piggyback paid commercial blogs. Brajeshwar neither endorses nor takes responsibility of the contents.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications.

Want to know more about me?

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

"Brajeshwar" has 836 Articles and 5,873 comments, contained within 20 categories and 1,145 tags.

This site is in operation since 11th June, 2001.