Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull Google Analytics stats?

Is Google API Ruby client the best option?

I have a site example.com with users and I want them to see their google analytics stats on example.com, how can I do it ?

I can see the example but I'm not able to figure out how to begin.

like image 979
iCyborg Avatar asked Sep 24 '13 11:09

iCyborg


People also ask

How do I get the data from Google Analytics?

Querying data For querying the Analytics data, we use the service's Get method that populates the GetRequest object. This method takes the four parameters, the website code with the "ga:" prefix, the start date, the end date, and the metrics.

How long does it take for Google Analytics data to populate?

If you just installed Google Analytics, you need to wait at least 24-48 hours to allow data to populate in reports—although I recommend waiting at least 3-4 weeks before making a decision based on reports. Below are three simple ways you can use the data in Google Analytics to improve your website.

What is the most useful Google Analytics report?

CJ Xia of Boster Biological Technology adds, “I believe that the Goal Overview Report is the most useful Google Analytics report. With this report, a user can set up specific interactions such as download and submit forms, track as a goal. This report is valuable for the user to see what pages are driving conversions and set a time frame.

How often does Google Analytics track website traffic?

Bruce Hogan of SoftwarePundit adds, “Other reports in Google Analytics only show you traffic on a daily, weekly or monthly basis. As a result, the Audience Overview report can be used to monitor website traffic performance in almost real-time. Personally, we use it to compare today’s traffic performance to the same day in the prior week.”


1 Answers

I also use the google-api-ruby-client gem and set it up about the same way that is outlined in the link you provided (https://gist.github.com/joost/5344705).

Just follow the steps outlined in the link to set up a Google Analytics client:

# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like [email protected]
PATH_TO_KEY_FILE              = '...' # the path to the downloaded .p12 key file
PROFILE                       = '...' # your GA profile id, looks like 'ga:12345'


require 'google/api_client'

# set up a client instance
client  = Google::APIClient.new

client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience             => 'https://accounts.google.com/o/oauth2/token',
  :scope                => 'https://www.googleapis.com/auth/analytics.readonly',
  :issuer               => SERVICE_ACCOUNT_EMAIL_ADDRESS,
  :signing_key          => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret')
).tap { |auth| auth.fetch_access_token! }

api_method = client.discovered_api('analytics','v3').data.ga.get


# make queries
result = client.execute(:api_method => api_method, :parameters => {
  'ids'        => PROFILE,
  'start-date' => Date.new(1970,1,1).to_s,
  'end-date'   => Date.today.to_s,
  'dimensions' => 'ga:pagePath',
  'metrics'    => 'ga:pageviews',
  'filters'    => 'ga:pagePath==/url/to/user'
})

puts result.data.rows.inspect

To display statistics for a user's page in your app, you have to adjust the metrics and filters parameters when making the query. The query above for example will return a result object containing all pageviews for the page with url example.com/url/to/user.


Caveat: this answer was written a long time ago and Google released a new, incompatible version of the gem. Please consult https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md

like image 84
severin Avatar answered Sep 17 '22 23:09

severin