Johnathon22 Johnathon22 - 5 months ago 18
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 => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:activeVisitors",
})


Followed with:
send_event('sa_realtimevisitors', current: sa_visitCount.data.rows)


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 => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + saprofileID,
'metrics' => "ga:sessionsWithEvent",
})


Followed with:

send_event('phonehub_event', current: phonehub_visitCount.data.rows)


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

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

So instead of phonehub_visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {

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

Comments