Rでウェブ解析:Google Analyticsからデータを取得「RGA」パッケージの紹介

RでGoogle Analytics

Google Analyticsからデータを取得する「RGA」パッケージの新しいバージョンが登場したので、記事の内容を更新です。Google Analyticsからデータを取得するパッケージはいろいろありますが、使用方法が簡単な「RGA」パッケージはウェブ解析に必須だと思います。

RGAパッケージはデータを取得するコマンドだけでなく、DimensionとMetricsの一覧を取得するコマンドやShinyでインタラクティブ確認するコマンドも収録されています。

DimensionとMetricsの一覧をcsvに保存するコマンドを紹介します。ぜひ、活用ください。リアルタイムのアクセス状況を取得する「get_realtime」コマンドはかなり使い道があると思います。

RからSearch Consoleを操作できる「searchConsoleR」パッケージがあります。ぜひ、合わせて利用してください。

・Rでウェブ解析:Google Search Consoleを操作!「searchConsoleR」パッケージ
 https://www.karada-good.net/analyticsr/r-215/

パッケージバージョン0.4.2。実行コマンドはwindows 7およびOS X 10.11.2のR version 3.2.3で確認しています。


スポンサーリンク
スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("RGA")

実行コマンドの紹介

詳細はコメントまたはパッケージヘルプを確認してください。

Google Analyticsからデータを取得するのに必要な設定は下記の目次より確認ください。
・初心者でもできる!RでGoogle Analytics解析の目次
 https://www.karada-good.net/rgoogleanalyticsindex/

#パッケージの読み込み
library("RGA")

#使用可能なDimensionとMetricsを取得
GaDimMet <- list_dimsmets()
#CSVでデータを保存
library(&quot;tcltk&quot;)
setwd(paste(as.character(tkchooseDirectory(title = &quot;保存場所を選択&quot;), sep = &quot;&quot;, collapse =&quot;&quot;)))
write.csv(GaDimMet, &quot;GA_DimensionとMetrics.csv&quot;)

###以下,Google Analyticsからデータを取得する方法##### 
#リクエストの許可を申請
#googleアカウントにログインした状況で実行:authorizeコマンド
#変数を用意することで複数アカウントの制御が可能です
authorize()

#Google Analyticsに登録しているプロファイルを取得:list_profilesコマンド
#データを読み込みidの確認です
list_profiles()
#下記情報は数値を改変していますのでidなどは利用できません
        id account.id webproperty.id                  name  currency   timezone
1 35464948   65284930  UA-36356274-1  からだにいいものみんな       JPY   Asia/Tokyo
                  website.url  type           created             updated   e.commerce.tracking
1 https://www.karada-good.net/  WEB 2014-04-20 05:46:28 2015-05-20 22:46:08                FALSE
  permissions
1 COLLABORATE,EDIT,MANAGE_USERS,READ_AND_ANALYZE

#データの取得:get_gaコマンド
#profileIdオプションにはlist_profilesコマンドで取得したidを使用します
#入力形式は&quot;ga:取得したid&quot;です
#実行日の前日から1週間のユーザー数,セッション数,ページビュー数を取得
#start.date,end.dateオプションはYYYY-MM-DD(例:2015-09-01)やtoday,yesterday,XdaysAgoで指定が可能です
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       metrics = &quot;ga:users, ga:sessions, ga:pageviews&quot;, dimensions = NULL)

Obtained data.frame with 1 rows and 3 columns.
   users sessions pageviews
1  88357    99352    123647

#セッション数とページビュー数の取得
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       metrics = &quot;ga:sessions,ga:pageviews&quot;)

Obtained data.frame with 1 rows and 2 columns.
 sessions pageviews
1    99352    123647

#新規ユーザーと再訪問者数の取得
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       dimensions = &quot;ga:userType&quot;, metrics = &quot;ga:sessions&quot;)
Obtained data.frame with 2 rows and 2 columns.
          user.type sessions
1       New Visitor    11727
2 Returning Visitor     9939

#使用ブラウザとOSに関する情報の取得
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       dimensions = &quot;ga:operatingSystem, ga:operatingSystemVersion, ga:browser, ga:browserVersion&quot;,
       metrics = &quot;ga:sessions&quot;)

operating.system operating.system.version       browser browser.version sessions
1          (not set)                (not set)    Chrome  27.0.1453        65430
2          (not set)                (not set)    Opera   507              4353

#キーワードの取得
#検索数で降順:sortオプション;先頭に-(マイナス)で降順
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       dimensions = &quot;ga:keyword&quot;, metrics = &quot;ga:sessions&quot;, filters = &quot;ga:medium == organic&quot;,
       sort = &quot;-ga:sessions&quot;)

              keyword sessions
1   (not provided)        1534
2   シャーロット 9話       444
3   シャーロット op 文字   318
4   bravely you ジャケット 301

#ページビュー数が多いコンテンツを取得
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       dimensions = &quot;ga:pagePath&quot;,
       metrics = &quot;ga:pageviews, ga:uniquePageviews, ga:timeOnPage, ga:bounces, ga:entrances, ga:exits&quot;,
       sort = &quot;-ga:pageviews&quot;)

Obtained data.frame with 364 rows and 7 columns.
             page.path pageviews unique.pageviews time.on.page bounces entrances exits
1   /analyticsr/r-218/      2329             1307         6608     271      1291   600
2   /analyticsr/r-156/      2319             1223         7790     111      1208   564

#訪問数の多いランディングページを取得
get_ga(profileId = &quot;ga:35464948&quot;, start.date = &quot;7daysAgo&quot;, end.date = &quot;yesterday&quot;,
       dimensions = &quot;ga:landingPagePath&quot;,
       metrics = &quot;ga:entrances,ga:bounces&quot;,
       sort = &quot;-ga:entrances&quot;)

Obtained data.frame with 247 rows and 3 columns.
    landing.page.path entrances bounces
1   /analyticsr/r-218/      1291    600
2   /analyticsr/r-156/      1208    564

#リアルタイムの情報を取得:get_realtimeコマンド
#リアルタイムAPIの説明はこちらから
#https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/
while (TRUE) {
  cat(&quot;&#92;&#48;14&quot;)
  print(get_realtime(profileId = IDSet, metrics = &quot;rt:activeUsers&quot;,
                     dimensions = c(&quot;rt:referralPath&quot;, &quot;rt:browser&quot;, &quot;rt:userType&quot;, &quot;rt:pageTitle&quot;)))
  Sys.sleep(3)
}

###参考:トークンファイルを利用する場合client.id,client.secretオプションを使用します#####
#client.id,client.secretの取得は紹介記事を参考ください
#クライアントIDを設定
client.id <- &quot;クライアントID&quot;

#クライアントシークレットを設定
client.secret <- &quot;クライアントシークレット&quot;

#トークンファイルの保存
library(&quot;tcltk&quot;)
setwd(paste(as.character(tkchooseDirectory(title = &quot;保存場所を選択&quot;), sep = &quot;&quot;, collapse =&quot;&quot;)))
authorize(client.id = client.id, client.secret = client.secret,
          cache = &quot;RGA.token&quot;)

#トークンファイルの利用
library(&quot;tcltk&quot;)
options(rga.cache = paste0(as.character(tkgetOpenFile(title = &quot;RGA.tokenファイルを選択&quot;,
                                                      filetypes = '{&quot;RGA.tokenファイル&quot; {&quot;.token&quot;}}',
                                                      initialfile = c(&quot;*.token&quot;)))))
authorize(client.id = client.id, client.secret = client.secret)
########
&#91;/code&#93;
<hr/>

<h2>DimensionとMetricsの出力内容</h2>
<a href="https://www.karada-good.net/wp/wp-content/uploads/2015/07/GA_Dimension_Metrics.png"><img src="https://www.karada-good.net/wp/wp-content/uploads/2015/07/GA_Dimension_Metrics-320x169.png" alt="GA_Dimension_Metrics" width="320" height="169" class="aligncenter size-medium wp-image-5574" /></a>

<h2>shinyでDimensionとMetricsを確認</h2>
下記コマンドを実行することでインタラクティブにDimensionとMetricsを確認することができます。

[code language="R"]
shiny_dimsmets()

RGAShiny


少しでも、あなたのウェブや実験の解析が楽になりますように!!

タイトルとURLをコピーしました