0
(0)
[統計] Google Analytics For The R 導入方法(途中

R の導入は https://www.fxfrog.com/?p=1932 (R 32bit) / https://www.fxfrog.com/?p=1908 (R 64bit) を見てください。R-GoogleAnalytics は http://code.google.com/p/r-google-analytics/ をご覧ください。

普段のカジュアルな Google API 取得方法と異なる、、、そこで「少し・・・」はまっていますが、解決の日は近いです。根拠としてはソースの意味がわかるから。

Windows環境です】

ここではグーグルさんが提供しているRパッケージを使用します。

Package: RGoogleAnalytics
Title: R Handler for importing Google Analytics information from the
Google Analytics API
Version: 1.1
Date: 2010-10-11

===

■ R-GoogleAnalytics を動作させるために依存パッケージを RGui から導入します。> 行を入力ください。

> install.packages(“RCurl”, repos = “http://www.omegahat.org/R“)
(略)downloaded 2.6 Mb

> install.packages(“XML”, repos = “http://www.omegahat.org/R“)
(略) downloaded 3.4 Mb

■R-GoogleAnalytics パッケージを導入します。

http://r-google-analytics.googlecode.com/files/RGoogleAnalytics_1.1.tar.gz が実体なので、RGUI から

> install.packages(“http://r-google-analytics.googlecode.com/files/RGoogleAnalytics_1.1.tar.gz”,repos=NULL,type=”source“)

この方法ではライブラリパスが処理出来ないと言われるので、RGoogleAnalytics_1.1.tar.gz をデスクトップへ保存し、さらにエスケープシーケンス対策を考慮し次のコマンドで導入します。 > 行を入力ください。

> install.packages(“C:UsersyukioDesktopRGoogleAnalytics_1.1.tar.gz”,repos=NULL,type=”source”)

パッケージを ‘C:UsersyukioDocuments/R/win-library/2.12’ 中にインストールします
(‘lib’ が指定されていないので)
* installing *source* package ‘RGoogleAnalytics’ …
** R
** inst
** preparing package for lazy loading
Loading required package: bitops
** help
*** installing help indices
** building package indices …
** testing if installed package can be loaded

* DONE (RGoogleAnalytics)
>

問題なく導入可能でした。念のためパッケージ(ライブラリ)参照状況を確認しましょう。

RGoogleAnalytics 選択(利用)可能です。

===

■では実際に参照してみましょう。

> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})

 次のパッケージを付け加えます: ‘RGoogleAnalytics’

The following object(s) are masked _by_ ‘.GlobalEnv’:

    QueryBuilder, RGoogleAnalytics

> ga <- RGoogleAnalytics()
> ga$SetCredentials(“GAアカウント”,”GAパスワード”)

 以下にエラー postForm(“https://www.google.com/accounts/ClientLogin“, Email = username,  :
  SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

なんだよ、CAルートキーの確認出来ないエラーとな。

RGoogleAnalytics.R の後は、QueryBuilder.R へ遷移してからも RCurl でgetURLするので、 RCurl へ対して https://www.google.com/ の SSL は無条件に確認しない設定は無いのか?RCurl FAQを確認したところ、

getURLContent(“https://www.google.com”, ssl.verifypeer = FALSE) 
↑けっこう強引だw

他にも、http://ademar.name/blog/2006/04/curl-ssl-certificate-problem-v.html なんてものがある。

RGUI はローカル IE をhttp proxy としているっぽい(DLL 共有)ので、IE から CA 証明書をpem にエクスポートし、それを RGoogleAnalytics.R へ読ませる方法はどうなんだろ?(要確認1)

いや、pem は最新のものが含まれている。

C:UsersyukioDocumentsRwin-library2.12RCurlCurlSSL ここに。

懲りずにデスクトップ上へ cacert.pem を持ってきて google.com を https 参照時のファイルに宛がおうと直接 RGui 上に

> x = getURLContent(“https://www.google.com”,cainfo = “C:\Users\yukio\Desktop\cacert.pem”)

このようなモノを書いても無駄。書くなら、ライブラリに書くか、あるいは変数 ga に対しては 前述通りベリファイア無効と書くのが無難そうである。

仕方ない、パッケージを直接修正かまそうかな。(要確認2)

RGoogleAnalytics.R を読むと、クレデンシャルをセットして Googleさんとお話しし、API から14日間有効なAPIトークンを発行してもらえるとな。トークン情報をAPIを叩く都度に一緒にパースして受け渡しをすると書いてあるので、ここに行くまでに蹴られてしまっている。

  SetCredentials <- function(username, pass) {
    # Authorizes this script to access the users Google Analytics Account.
    # Passes the username and password to the Google Accounts API ClientLogin
    # routine to get an authorization token which can be used to access
    # the user’s Google Analytics data. This token is stored in the auth.token
    # private member and stays valid for 14 days. The token can later be used
    # with queries to the Google Analytics API. Note: We do not store the
    # username or password for security reasons.
    #
    # Args:
    #   username: A valid user name for a Google Analytics account.
    #   pass: A valid password for the Google Analytics account.
    # Returns:
    #   Returns the authorization token of the users account.

    # Error handling.
    # Ensure that both user name and password have been entered.
    if (is.null(username) || is.null(pass)) {
      stop(“Please supply a user name and password”)
    }

    auth.resp <- postForm(“https://www.google.com/accounts/ClientLogin“,
                          Email = username,
                          Passwd = pass,
                          accountType = “GOOGLE”,
                          source = “r-google-analytics”,
                          service = “analytics”)
    gtoken <- unlist(strsplit(auth.resp, “n”))
    parsed.gtoken <- unlist(strsplit(gtoken[3], “Auth=”))
    if (length(parsed.gtoken) >= 2) {
      auth.token <<- unlist(strsplit(gtoken[3], “Auth=”))[[2]]
    } else {
      stop(“Authentication failed.”)
    }

    return(invisible())
  }

おや?!この書き方って教科書通りやん。普段、ガシガシ叩いている API の呼び方と違うというか・・・。

もしや?

続く・・・。

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

投稿者 斉藤之雄 (Yukio Saito)

Global Information and Communication Technology OTAKU / Sports volunteer / Social Services / Master of Technology in Innovation for Design and Engineering, AIIT / BA, Social Welfare, NFU / twitter@yukio_saitoh

コメントは受け付けていません。