Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:27:04+00:00 2026-05-29T19:27:04+00:00

What I want to do is use google analytic’s custom event tracking from within

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:27:07+00:00Added an answer on May 29, 2026 at 7:27 pm

    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…

    # application_controller.rb
    protected
    def log_event(category, action, label)
        session[:events] ||= Array.new
        session[:events] << {:category => category, :action => action, :label => label}
    end
    

    .. call that method from your individual controller methods as many times as you please.

    # application.html.erb
    <head>
    ... other stuff...
    <!-- Google Analytics -->
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', '<%= CONFIG['ga_account_id'] %>']);
        _gaq.push(['_trackPageview']);
        <%= render "layouts/ga_events" %>
    </script>
    

    .. and the little partial referenced there is:

    # _ga_events.html.erb
    <% unless session[:events].nil? %>
      <% session[:events].each do |event|%>
         _gaq.push(['_trackEvent', '<%= event[:category]%>', '<%= event[:action]%>', '<%= event[:label]%>']);
      <% end %>
    <% end %>
    
    <% session[:events] = Array.new %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Google analytic, i want to retrieve the last 100 search string from
I want to use Google spreadsheets to store data online so multiple people can
I want to use google chrome as a control. I think Enso does this
I want to use the google maps hand cursor in Python but I don't
I want to use telnet to google mail server on linux. I want to
A client wants to use Google Apps for employees but they want to offer
I want to use the thumbnail of the Google video's. In PHP or JavaScript
i want use some data from a website with web service. i have a
I use the next code from Google for Analytics: <script type='text/javascript'> var _gaq =
We're about to deploy a new Django website, and we want to use Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.