Adding google analytics to a rails app

I wanted to add google analytics code to my rails app which I think a bit more work compared to just adding javascript code directly to a static HTML site. After some digging I found out it can be done two ways.

1. Just add the javascript code to app/helpers/application_helper.rb

2. Or by installing a gem from github

A. By adding the javascript code

1. Add the following javascript code to app/helper/application_helper.rb

 <% def google_analytics_js %>

'<script type="text/javascript">

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-xxxxxxxx-y']);


(function() {

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

ga.src = ('https:' == document.location.protocol ? '<a href="https://ssl/">https://ssl</a>' : '<a href="http://www/">http://www</a>') + '.<a href=""></a>';

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);



<% end %>

2. Replace the ‘UA-xxxxxxxx-y’ in the code above with the tracking ID. Save and close application_helper.rb.

3. Add  <%= google_analytics_js %>  to the the file, just before the closing “</head>” HTML tag on each page.

This is ok if there are few pages in an app. But it will be tedious to go and change in each file for a larger application. That is where the gem come handy.

 B. By adding a gem in rails app

1. Add the following to your Gemfile:

gem ‘google-analytics-rails’
Then run:

bundle install

2. Go to file config/environments/production.rb:

# replace this with your tracker code
GA.tracker = “UA-xxxxxx-x”

3. And go to app/views/layout/application.html.erb, in the <head> tag :


<%= analytics_init if Rails.env.production? %>

That should do the trick.

Proper Disposal of Unused Medicines

I doubt there are many people among us who have not taken any medicines in their life time. Apart from three basic necessities (Food, shelter & clothes), medicine is the fourth one.  Most people who take it there is a good number who never complete their entire course of medication. In most household  we will find our cabinet filled with expired or unused medications that we no longer use. So how to deal with them? FDA has a nice guidelines how to deal with unused and expired prescription. If you like to read the longer version then you can read it here. If you do not have much time to read the entire article but want to have enough basic ideas then read the summary (important facts, Do & Don’t) below.

Basic steps/precautions needs to be taken:-

Do not:-
1. Do not flush prescription drugs down the toilet or throw in the trash unless the information on the vial specifically instructs you to do so.
2. Do not give your medications to your friends or relatives. The drug is prescribed according to your personal requirements which could be dangerous for others.

1. Before throwing your medication along with container, scratch out your name and personal information on the prescription label to protect the privacy of your personal health information.
2. If there is no instructions on the drug label or take-back program information, then throw the drug in trash. But do it as follows:
a. Take the pills/medications from their original containers and mix them with used coffee grounds or kitty litter. Put them in a sealable bag or container and then discard into trash. Dispose the trash bag as soon as you can.

Call your city or County  government’s recycling service to find out if there is a community drug take-back programs available in your area.

And the last thing is when in doubt, ask to your local pharmacist regarding proper disposal method. The disposal methods for prescription drugs could apple to over-the-counter drugs as well.

Update to BLOC

Due to several reasons ( travel, conferences and fellowship commitment) I have dropped out of BLOC program. In addition to travel, I will be attending three conferences, ASM2012 to be held in June 16-19, Kaufmann’s Life Science Venture Summit in June 21-22, 2nd Food and Drug Conference  August 1st and final exam for Fundamental Epidemiology for Commissioner’s Fellow Program which I forced me to choose one or the other. After much thought I came to conclusion that it is just a lot on my plate to digest. The program demands at least 3-4 hrs per weekdays and 5 hrs on weekends. Initially I tried to hustle but finally came to conclusion that I can always go for the next session in stead of doing it haphazardly.  However, my learning will be continued.

Learn to code or not learn to code

Recently a veteran programmer and blogger Jeff Atwood wrote a blog post titled “Please Do n’t learn to code” on which he cautioned coding is not for everybody.The blog post led to a big debate on  HN  community which showed mixed emotions among entrepreneurs and programmers.

For  a newbie learner like me this is pretty depressing to hear from an experienced hacker that I should not learn to code and keep doing the job what I am doing now.

But there were some interesting blog posts especially from Justin Kan  and  from Zed Shaw which made me think that it is worth to give it a shot how to learn to code. As Peter Norvig says it, it will take at least 10 years or 10000 hr of work (grind) to become a giant in any field. So I may not become a top notch programmer ( 0.1%) given the amount of time I am going to devote. However if I can take an idea that is stinging my head for a while and develop a prototype to test my hypothesis about it’s usefulness then hell  yea, I am going to learn how to code. So I made up my mind and applied for Bloc which is a 8 week immersion in learning Ruby on rails. Recently I got selected to and have started the journey. I know this will not be a smooth ride but I am going to give it a full shot as Wayne Gretzky has said it “You miss 100% of the shots that you do not take”.

MySQL installation for Ruby on Rails environment on OS X Lion

There are several good resources on learning Ruby on Rails and one of the best tutorials I can find is at Of course there are several other resources but after reading several posts on HN and elsewhere, I found out the is the place to start. Some other resources include peepcode screencast for Rails Meet Rails 3 Part I and Meet Rails 3 Part II. Back to the point, in the installation and set up chapter of, Michael Hartl’s has shown to use how to use sqlite 3 for starting a new project. However, to simulate more or less real server environment I thought of installing MySQL for the Ruby on Rails.

Following are the steps:

1. Download MySQL from here.

2. Install and change your .bash_profile accordingly.

For me the command was

export PATH="/usr/local/mysql/bin:$PATH"

3. Change username and password

Then when I initiated rail server, I found the following errrors:

 1="dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle," 2="9):" 3="Library" 4="not" 5="loaded:" 6="libmysqlclient.16.dylib" 7="(LoadError)" 8="Referenced" 9="from:" 10="/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle" 11="Reason:" 12="image" 13="not" 14="found" 15="-" 16="/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle" 17="from" 18="/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2.rb:7" 19="from" 20="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in" 21="`require'" 22="from" 23="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in" 24="`require'" 25="from" 26="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in" 27="`each'" 28="from" 29="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in" 30="`require'" 31="from" 32="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in" 33="`each'" 34="from" 35="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in" 36="`require'" 37="from" 38="/Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:112:in" 39="`require'" 40="from" 41="/Users/jontybrook/Dropbox/CODING/simple_cms/config/application.rb:7" 42="from" 43="/Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28:in" 44="`require'" 45="from" 46="/Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28" 47="from" 48="/Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27:in" 49="`tap'" 50="from" 51="/Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27" 52="from" 53="script/rails:6:in" 54="`require'" 55="from" 56="script/rails:6" 57="user$" 58="

I tried to tweak around it for a while without any success. I believe it is a OS-X Lion issue.  I came across with some information here and there and found several command lines to change the file path and structures out of which following worked.

Open command prompt and type following.

$ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

Then export the library path by typing


export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"

Though I am not sure what is happening under the hood but it fixed the issue.

MySQL root user password change in MAMP

Recently I installed MAMP on my Mac (XAMPP equivalent on PC) for a local testing server environment development and came to know that the user and password both are root by default. As I am a newbie on this server and database environment, I wanted to change the user and password. So just like I have done on PC beore, I went ahead and changed the password on phpMyAdmin page. Big mistake!! The whole thing got messed up.

After some digging on web, I found a few posts on how to change root password of MySQL (here) and here . But when I tried changing MySQL root password on command prompt MySQL did not respond at all. There is not enough instructions on MAMP’s site. However I found out another blog post which has described how to fix it in detail here.  However, for changing the root password the following command did not work.

/Applications/MAMP/Library/bin/mysqladmin -u root -p password

I do not know whether it is due to OS-X Lion.

But following step fixed the password.

Start MAMP server and log into MySQL. Use the following command to change the password:

$ mysqladmin -u user -p oldpassword password newpassword

Here comes the important step. You need to change the old password occurrences in a bunch of files present in bin folder of MAMP Library.

Following are the files needed to be changed from -u root -proot to -pnewpassword






And two important files

File: /Applications/MAMP/bin/mamp/index.php

$port = '3306';
$link = @mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'user', 'newpassword');

File: Applications/MAMP/bin/phpMyAdmin/

$cfg['Servers'][$i]['user']          = 'user';      // MySQL user
 $cfg['Servers'][$i]['password']      = 'newpassword';          // MySQL password (only needed
 // with 'config' auth_type)

These fixed my password setup problems.