From f0dd44a7ca0f6410a2235a65b112cdfc325a6a40 Mon Sep 17 00:00:00 2001 From: Moncef Belyamani Date: Sun, 20 Apr 2014 20:20:48 -0400 Subject: [PATCH] Update Google Analytics code and use env vars The GA ID and domain name were hardcoded before. I moved them to environment variables and wrapped the script inside a conditional so that it only gets displayed in production if the env var has been set. I also updated the README with instructions for setting the env vars on Heroku. --- README.md | 14 +++++++++++++- app/views/layouts/application.html.haml | 11 +++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7b1d29d..c876d9a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,19 @@ A successful deployment to Heroku requires a few setup steps: `heroku run bundle exec rake db:seed` -6. Keep in mind that the Heroku free Postgres plan only allows up to 10,000 rows, so if your city has more than 10,000 fire hydrants (or other thing to be adopted), you will need to upgrade to the $9/month plan. +Keep in mind that the Heroku free Postgres plan only allows up to 10,000 rows, +so if your city has more than 10,000 fire hydrants (or other thing to be +adopted), you will need to upgrade to the $9/month plan. + +### Google Analytics +If you have a Google Analytics account you want to use to track visits to your +deployment of this app, just set your ID and your domain name as environment +variables: + + heroku config:set GOOGLE_ANALYTICS_ID=your_id + heroku config:set GOOGLE_ANALYTICS_DOMAIN=your_domain_name + +An example ID is `UA-12345678-9`, and an example domain is `adoptahydrant.org`. ## Contributing In the spirit of [free software][free-sw], **everyone** is encouraged to help diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 64cead4..e78d8cf 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,8 +12,15 @@ = javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" = javascript_include_tag "//ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" = javascript_include_tag "application" - %script{:type => "text/javascript"} - var _gaq=_gaq||[];_gaq.push(["_setAccount","UA-20825280-2"]),_gaq.push(["_setDomainName",".adoptahydrant.org"]),_gaq.push(["_trackPageview"]),function(){var a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src=("https:"==document.location.protocol?"https://ssl":"http://www")+".google-analytics.com/ga.js";var b=document.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)}(); + - if Rails.env.production? && ENV['GOOGLE_ANALYTICS_ID'].present? + %script{:type => "text/javascript"} + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ + (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) + })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); + + ga('create', '#{ENV['GOOGLE_ANALYTICS_ID']}', '#{ENV['GOOGLE_ANALYTICS_DOMAIN']}'); + ga('send', 'pageview'); = stylesheet_link_tag "application" %body = yield