2016-03-15 31 views
0

我想紅寶石:如何讓數位訪客頁面與谷歌分析

opts = YAML.load_file("ga_config.yml") 

## Update these to match your own apps credentials in the ga_config.yml file 
service_account_email = opts['service_account_email'] # Email of service account 
key_file = opts['key_file']       # File containing your private key 
key_secret = opts['key_secret']      # Password to unlock private key 
profile_id = opts['profileID'].to_s     # Analytics profile ID. 


client = Google::APIClient.new(
    :application_name => opts['application_name'], 
    :application_version => opts['application_version']) 

## Load our credentials for the service account 
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret) 

visitors = [] 


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, 
    :signing_key => key) 

# Start the scheduler 

    # Request a token for our service account 
    client.authorization.fetch_access_token! 

    # Get the analytics API 
    analytics = client.discovered_api('analytics','v3') 

    # Execute the query 
    response = client.execute(:api_method => analytics.data.realtime.get, :parameters => { 
    'ids' => "ga:" + profile_id, 
    'metrics' => "ga:activeVisitors", 
    }) 

    puts response.data.rows.count 

時運行代碼來算客人。 response.data.row.count = 0 但我去https://analytics.google.com/analytics/web/#realtime/rt-content

內容

現在:2

顯然有我的代碼中的任何錯誤? 如何解決這個問題?

,我想通過顯示頁面 範例中得到訪客:

ActivePage       activeUser 
/page1         1 
/page2         3 

如何獲得上述數據?

感謝

+0

GA:activeVisitors甚至不是一個有效的指標。我真的很驚訝,請求的作品。 – DaImTo

+0

我閱讀並按照http://stackoverflow.com/questions/19189610/ruby-real-time-google-analytics-api中的說明 如何解決錯誤? –

+0

這取決於您嘗試訪問實時數據的方式。該教程使用analytics.data.ga.get你使用analytics.data.realtime.get – DaImTo

回答

1

您請求從實時API的數據,但沒有使用有效的實時API指標。您還需要增加一個維度

試試這個

'metrics' => "rt:activeVisitors", 'dimensions' => "rt:pagePath", 
+0

'response = client.execute(:api_method => analytics.data.realtime.get,:parameters => { 'ids'=>「ga: 「+ PROFILE_ID, '指標'=> 」RT:activeUsers「, '尺寸'=> 」RT:PAGEPATH「, })' 或 ' '指標'=> 」RT:activeVisitors「,'尺寸'=>「rt:pagePath」,「 都不工作。 如何使用有效的實時api矩陣? –

+0

的鏈接:https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/ 我沒有找到指標=「rt:activeVisitors」,只有'metrics'=>「rt:activeUsers」 –

+0

tks,我做到了:D。 'reult = [['/ t/danh-muc/dong-ho-nam「,」1「],[」/ t/danh-muc/dong-ho-nu「,」1「]] 現在。我想通過pagePath進行過濾,當pagePath包含'string ='dong-ho-nam''的結果可以是: reult = [[「/ t/danh-muc/dong-ho-nam」,「1」 ]]我使用'filter =>''rt:pagePath == * dong-ho-nam *'''但不工作。我應該寫在過濾器中的東西 - –