Skip to content

Instantly share code, notes, and snippets.

@stim371
Created September 10, 2011 02:42
Show Gist options
  • Save stim371/1207859 to your computer and use it in GitHub Desktop.
Save stim371/1207859 to your computer and use it in GitHub Desktop.
app helper to place google analytics
module ApplicationHelper
def google_analytics_js
content_tag(:script, :type => 'text/javascript') do
"var _gaq || [];
_gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = document.getElementByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();"
end if Rails.env.production?
end
end
#this function allows you to input your account id when you call the function
module ApplicationHelper
def google_analytics_js(id = nil)
content_tag(:script, :type => 'text/javascript') do
"var _gaq || [];
_gaq.push(['_setAccount', '#{id}']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = document.getElementByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();"
end if !id.blank? && Rails.env.production?
end
end
<%= google_analytics_js 'XX-XXXXXXXX-X' %>
<%= google_analytics_js %>
@stim371
Copy link
Author

stim371 commented Sep 10, 2011

Replace the XX-XXXXXXXX-X with your specific google analytics account number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment