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.

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

Prev Article:

Next Article:

Archives: Visit the Archives for more articles.

Comments Post Yours

There are View Comments 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

blog comments powered by Disqus

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.

12 Hottest Geek Girls on Twitter

So, you have seen the 12 Hottest Geek Girls (via Digg). However, they forgot to link them to their Twitter profiles so you can follow them. Well, here they are -- the 12 Hottest Geek Girls ...13th Oct, 2009

Great Indian Developer Summit 2009

I got a Press Release of the upcoming GIDS '09 and here is an excerpt. The summit's program covers Java, REST, Unit testing, Groovy, Spring, Struts 2.0, SOA, Cloud Computing, Web Services, JRuby, RoR, Ruby, JVM, ...21st Jan, 2009

The flourishing gun market in Pakistan

VICE Travel: Darra, Pakistanby Top-Notch112 (Via: Deep Green Crystals) 20th Jan, 2009

Angry Ringtone for iPhone and others

[audio:http://audio.brajeshwar.com/angry-ring-ring.mp3] The ANGRY RINGTONE for iPhone. (Click the PLAY button above!) Download * iPhone Ringtone (.m4r) * MP3 Ringtone (.mp3) * Zipped (both .m4r and .mp3) To use it as an iPhone Ringtone; just double click the file "angry-ring-ring.m4r" and it ...15th Jan, 2009

IIM Ahmedabad's Leverage 2009

Leverage, the Venture Capital and Private Equity Club of IIM Ahmedabad and the Centre for Innovation Incubation and Entrepreneurship bring to you the 1st edition of the Venture Capital and Private Equity Conference on the ...12th Jan, 2009

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • 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.
  • Downloads All downloads, Free and Open Source.
  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.
  • ActionScript 3.0 Reference Flash/Flex ActionScript 3.0 Reference.
  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • Ode to Apple Dedicated to Apple – Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.

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. More about me.

Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 995 Articles and 5,934 comments, contained within 17 categories and 1,639 tags.