What I want to do is use google analytic’s custom event tracking from within my controllers, where the logic is done. I’m not quite sure how to put javascript code in my controller or if it is possible at all. How would I go about putting something like this inside my controller:
_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
or
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);
thanks! And which one of these should I be using?
EDIT:
Here’s how I set it up based on the suggestion:
tracking code partial:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-ACCOUNT']);
_gaq.push(['_setDomainName', 'sitename.com']);
_gaq.push(['_trackPageview']);
<%= render "layouts/ga_events" %>
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
_ga_events.html.erb (I added in the “value” parameter)
<% unless session[:events].nil? %>
<% session[:events].each do |event|%>
_gaq.push(['_trackEvent', '<%= event[:category]%>', '<%= event[:action]%>', '<%= event[:label]%>', '<%= event[:value]%>']);
<% end %>
<% end %>
<% session[:events] = Array.new %>
application controller (added “value param)
protected
# GA event logger
def log_event(category, action, label = nil, value = nil)
session[:events] ||= Array.new
session[:events] << {:category => category, :action => action, :label => label, :value => value}
end
testing it in my Tasks controller:
def create
@task = @user.tasks.build(params[:task])
@task.author = current_user unless @user == current_user
if @task.save
log_event("Tasks", "Created", current_user.email, "123")
redirect_back tasks_path, :notice => t('tasks.created')
else
redirect_back tasks_path, :alert => @task.errors.full_messages
end
end
EDIT: here my GA output code, looks like everything is correct:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA_CODE']);
_gaq.push(['_setDomainName', 'SUBDOMAIN']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackEvent', 'Priority', 'Created (day)', 'Label info', '']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Here’s how I do it, it’s quite similar to how RoR implements
flash. Essentially, on the server side you store up the events that you eventually want to push to GA via Javascript. When a page is eventually rendered, you render Javascript to push the events, and reset your store.So…
.. call that method from your individual controller methods as many times as you please.
.. and the little partial referenced there is: