Johnathon22 Johnathon22 - 1 year ago 95
Ruby Question

Send google analytics event in ruby

I have the following code for google analytics real time, in ruby:

# Execute the query
sa_visitCount = client.execute(:api_method =>, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:activeVisitors",

Followed with:
send_event('sa_realtimevisitors', current:

This works great and returns an active visitor count.
Now I am trying to change this to return an event I have created on google analytics. Here is what the event looks like. You find this under Real-Time and Events. I am trying to use the Event Category "Ad" Listed as #2 in the screenshot.
Google analytics Event Ad

The code I am using to attempt this in ruby is as follows.

phonehub_visitCount = client.execute(:api_method =>, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:sessionsWithEvent",

Followed with:

send_event('phonehub_event', current:

This returns nothing. I'm still using the original GA ID, however I'm trying to tell it to look at the event I created. Is there a way in ruby to make the GA realtime return the results from the event? Ideally the solution I'm looking for is to return real time active visitors on the event I have made.

Answer Source

After further research I found that I had to specify v3 in the api method.

So instead of phonehub_visitCount = client.execute(:api_method =>, :parameters => {

I had to use api_method = client.discovered_api('analytics','v3') Then add phonehub_visitCount = client.execute(:api_method => api_method, :parameters => {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download