# Google Analytics and partials

**URL:** <https://www.ruby-forum.com/t/google-analytics-and-partials/184748>\
**Category:** Rails\
**Created:** [March 7, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/google-analytics-and-partials/184748 "2010-03-07T23:01:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 7, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/google-analytics-and-partials/184748/1 "2010-03-07T23:01:00Z")

</div>

I have been tracking users with Google Analytics via the javascript code  
in the header of my main template and that works very well.

I’ve come to realize that there are a few places where I have  
will\_paginate and some cascading collections where there are partials  
that update but the entire page does not refresh and thus, even though  
the content is changed, the click activity is never recorded by Google  
and I am essentially cheating myself from the intelligence of what users  
actually are doing.

Obviously I can re-locate the google javascript code for sending the  
reports to Google Analytics from the main template to the inner-most  
partial for each of my pages but that seems to be very redundant and not  
very dry so I’m wondering how do other people handle this? Is there a  
way to just execute when the partial is redrawn? Is it possible to use  
observers to refresh 2 divs on the same page? (albeit one in the main  
template and one in a partial)?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 8, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/google-analytics-and-partials/184748/2 "2010-03-08T22:45:05Z")

</div>

> actually are doing.
> 
> Obviously I can re-locate the google javascript code for sending the  
> reports to Google Analytics from the main template to the inner-most  
> partial for each of my pages but that seems to be very redundant and  
> not  
> very dry so I’m wondering how do other people handle this? Is there a  
> way to just execute when the partial is redrawn? Is it possible to use  
> observers to refresh 2 divs on the same page? (albeit one in the main  
> template and one in a partial)?

Leave it where it is and use their API to “make a call”.

[http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55521](http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55521)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 9, 2010, 12:02am UTC](https://www.ruby-forum.com/t/google-analytics-and-partials/184748/3 "2010-03-09T00:02:57Z")

</div>

On Mon, 2010-03-08 at 13:43 -0800, Philip H. wrote:

> > actually are doing.  
> > Leave it where it is and use their API to “make a call”.
> 
> [http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55521](http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55521)

* * *

yeah - thanks - I sort of came up with a better solution than that but I  
still was eager to see what others are doing.

If I were to do what was suggested, that would unnecessarily inflate the  
numbers for the partial since it would send an update to GA even when  
the entire page was refreshed which was clearly what I didn’t want to  
do.

So what I ended up doing was to take advantage of the session flash  
variable - for those who might search and find this thread later on…

in the view (actually the deepest nested partial)…

\<% if flash[:analytics] -%\>

\<% end -%\>

and then I can properly provide flash[:analytics] value in the  
controller method that ultimately renders partial updates…

flash[:analytics] = “/content/menu\_topic=” + @menu\_topic + “&page=” +  
params[:page].to\_s

which completely gives me the detail I am looking for in Google  
Analytics and without false reports when the entire page is loaded.

On the whole, will\_paginate is a very useful tool but when you want to  
paginate inside of partials, it requires you to be inventive.

Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
