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.
This commit is contained in:
Moncef Belyamani 2014-04-20 20:20:48 -04:00
parent c20139f046
commit f0dd44a7ca
2 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -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