# Google\_analytics gem problems

**URL:** <https://www.ruby-forum.com/t/google-analytics-gem-problems/145691>\
**Category:** Rails\
**Created:** [August 13, 2008, 12:19am UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691 "2008-08-13T00:19:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 12:19am UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/1 "2008-08-13T00:19:11Z")

</div>

I’ve honestly never had any serious issues with a gem under RoR until  
now!

I installed mark catley’s google analytics plugin ([http://github.com/](http://github.com/)  
markcatley/google\_analytics/tree/master) and it doesn’t seem to work  
at al! I have the gem in my environments.rb (I am running Rails 2.1),  
and it is installed. But it is like it is not loaded in. There is no  
call added and if I try and hand add an after\_filter to call  
add\_google\_analytics\_code I get a no such method error.

Has anyone used this gem?

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 3:40am UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/2 "2008-08-13T03:40:31Z")

</div>

A few things to check:

1. If you’re testing in development, make sure your config/  
initializers/analytics.rb file includes a line like this:

Rubaidh::GoogleAnalytics.environments = [‘production’, ‘development’]

(By default, the Analytics code is only added in the production  
environment)

1. Make sure that you’re testing with a page that goes through a  
layout with tags - the code depends on seeing the body  
tags to know where to insert code. Don’t be fooled by “view source”  
showing you body tags that aren’t really there if you’re simply  
rendering a view without a layout.

2. You may want to pick up from the master repo at  
[GitHub - rubaidh/google\_analytics: Automatically insert Google Analytics code into your pages](http://github.com/rubaidh/google_analytics/tree/master) , which I  
believe is somewhat more up-to-date than Mark Catley’s branch.

I just ran a test using the current master code as a gem in a Rails  
2.10 app, and it worked fine for me.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/3 "2008-08-13T12:39:57Z")

</div>

All I did was add the line to the config file, run rake gems:install,  
and run with it. So I’m not sure why you had issues with the gem  
approach.

As to whether it’s better than just dropping static code in through a  
partial - that depends, I think, on whether you need to use any of the  
other capabilities of the plugin. If all you need to do is just hook  
pages into Google Analytics, the partial approach is fine. The two  
areas where I’ve found the plugin most useful are (1) The helpers that  
let me use link\_to\_tracked and so on to easily get outgoing link  
tracking (2) The overrides to claim a different site/path for  
different pages when making the call back to Google (I’m using that  
heavily on a server where one Rails app serves multiple sites).

Of course, since I wrote a bunch of that code, I’m prejudiced.

Mike

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 9:59am UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/4 "2008-08-13T09:59:53Z")

</div>

Thanks Mike!

I installed the code from git://github.com/rubaidh/google\_analytics.git  
as a plugin and that seemed to work. Using it as a gem I had no  
success… how did you do it?

Now the question is, is it any better than just having the code in a  
partial and dropping it at the bottom of my layout?!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/5 "2008-08-13T13:02:48Z")

</div>

Ok,  
The line in the readme, though, installs the mark catley one… so I  
guess what I am asking is what line exactly did you add!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 27, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/6 "2008-08-27T18:13:24Z")

</div>

I added [http://gems.github.com](http://gems.github.com) to my gem sources, then did a ‘gem list  
-r | grep analytics’, which only turned out the  
markcatley-google\_analytics (1.0.20080720) version.

Following the instructions here  
([GitHub - rubaidh/google\_analytics: Automatically insert Google Analytics code into your pages](http://github.com/rubaidh/google_analytics/tree/master#)) and using  
‘rake gems:install’ tells me that the gem does not exist.

Hmmm. So I tried installing the markcatley version ‘gem install  
markcatley-google\_analytics’, which goes ok, but when I add 'require  
‘rubaidh/google\_analytics’ to my application controller and then  
‘Rubaidh::GoogleAnalytics.tracker\_id = ‘xxxx’’ to the bottom of  
environment.rb I get a 500 Server Error with nothing in my development  
log.

How exactly am I supposed to use this gem? I can’t find any information  
anywhere which works, though if there is then please tell me where.

Alternatively how am I supposed to install it as a plugin? All the  
sources I can find on the net to pass to script/plugin install seem to  
be dead, or old code (not the latest google javascript).

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/google-analytics-gem-problems/145691/7 "2008-08-13T14:20:09Z")

</div>

I used Mark’s branch when testing the gem integration; he’s the one  
who’s been working on that part of the code. I mainly run with the  
plugin myself, though.

Mike
