本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rで解析:Googleサーチコンソールを利用したhtmlレポートの出力

SearchConsoleRパッケージを利用したhtmlレポートの出力です。htmlファイルのサイズはデータが約700ほどで約1MBなのでメールで送付できる範囲かと思います。

出力例は仮想データとなります。過去記事を参考に実行環境を整えていただければと思います。

Rで解析:RStudioを使ったmarkdownでGoogleAnalytics! https://www.karada-good.net/analyticsr/r-228/

htmlでの出力を念頭に置いているので、javaを使用するDTパッケージを利用しています。インタラクティブに表を操作できます。

実行コマンドはR version 3.2.2で確認しています。

準備

初めに作業フォルダを指定し、下記のコマンドを実行することでエラーが出ません。冒頭に紹介した過去記事を参照ください。


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

#グーグルアカウントの許可
#実行するとウェブブラウザが起動します
gar_auth()
スポンサーリンク

実行コマンド

コマンドはRStudioでR Markdownのファイルを作成し利用することを想定しています。


---
title: "SearchConsoleRを利用したhtmlレポートの出力"
output:
  html_document: default
  pdf_document: default
  word_document: default
header-includes:
- \usepackage{zxjatype}
- \setjamainfont{HiraKakuProN-W3}
---

```{r, global_options, include = FALSE}
knitr::opts_chunk$set(fig.width = 12, fig.height = 8,
                      include = TRUE, echo = FALSE, warning = FALSE, message = FALSE)
```

```{r, echo = FALSE, include = TRUE, results = "hide"}
###データの準備#####
#パッケージの読み込み
library("searchConsoleR")
library("googleAuthR")
#install.packages("DT")
library("DT")

#取得期間の指定
#実行日の前日
EndData <- (Sys.Date() -1)
#実行日の前日から31日前
StartData <- EndData - 31

#グーグルアカウントの許可
#実行するとウェブブラウザが起動します
gar_auth()

#データの取得
SiteURL <- "対象となるURLを入力"
#デバイス別のデータ
DeviceGetData <- search_analytics(siteURL = SiteURL,
                                  startDate = StartData, endDate = EndData,
                                  dimensions = c("query", "page", "device"),
                                  searchType = "web", rowLimit = 1000)
#デバイス統合のデータ
AllGetData <- search_analytics(siteURL = SiteURL,
                               startDate = StartData, endDate = EndData,
                               dimensions = c("query", "page"),
                               searchType = "web", rowLimit = 1000)

#データの整形
#SiteURLの部分を削除
#DeviceGetData[, 2] <- gsub(SiteURL, "", DeviceGetData[, 2])
#AllGetData[, 2] <- gsub(SiteURL, "", AllGetData[, 2])
#列名の修正
#colnames(DeviceGetData) <- c("クエリ", "ページ", "デバイス", "クリック数", "表示回数", "CTR (%)", "掲載順位")
#colnames(AllGetData) <- c("クエリ", "ページ", "クリック数", "表示回数", "CTR (%)", "掲載順位")

#データの丸め
#DeviceGetData[, 2] <- gsub(SiteURL, "", DeviceGetData[, 2])
#AllGetData[, 2] <- gsub(SiteURL, "", AllGetData[, 2])
```


「`r SiteURL`」の`r StartData`から`r EndData`の情報です。

---

### 結果です。
query;検索キーワード,page;検索で表示されるページ,clicks;ページのクリック数,impressions;検索キーワードでのページ表示回数,ctr;クリック率,position;検索による表示順位
```{r, results = "asis"}
AllGetData[, 2] <- paste0('<a href=', AllGetData[, 2], '>', gsub(SiteURL, "", AllGetData[, 2]), '</a>')
datatable(AllGetData, rownames = FALSE, escape = FALSE, extensions = 'TableTools',
          filter = 'top', options = list(pageLength = 15, autoWidth = TRUE)) %>%
          formatPercentage('ctr', 2) %>% formatRound('position', 1) %>% 
          formatStyle('ctr', color = styleInterval(0.035, c('black', 'red')),
                      backgroundColor = styleInterval(0.035, c('white', 'yellow'))) %>%
          formatStyle('impressions', background = styleColorBar(DeviceGetData[, 5], 'lightblue'),
                      backgroundSize = '100% 90%', backgroundRepeat = 'no-repeat', backgroundPosition = 'center') %>%
          formatStyle('clicks', background = styleColorBar(DeviceGetData[, 4], 'lightgreen'),
                      backgroundSize = '100% 90%', backgroundRepeat = 'no-repeat', backgroundPosition = 'center')
```

---

### デバイス別の結果です。  
query;検索キーワード,page;検索で表示されるページ,device;使用環境,clicks;ページのクリック数,impressions;検索キーワードでのページ表示回数,ctr;クリック率,position;検索による表示順位
```{r, results = "asis"}
DeviceGetData[, 2] <- paste0('<a href=', DeviceGetData[, 2], '>', gsub(SiteURL, "", DeviceGetData[, 2]), '</a>')
datatable(DeviceGetData, rownames = FALSE, escape = FALSE,
          filter = 'top', options = list(pageLength = 15, autoWidth = TRUE)) %>%
          formatPercentage('ctr', 2) %>% formatRound('position', 1) %>% 
          formatStyle('ctr', color = styleInterval(0.035, c('black', 'red')),
                      backgroundColor = styleInterval(0.035, c('white', 'yellow'))) %>%
          formatStyle('impressions', background = styleColorBar(DeviceGetData[, 5], 'lightblue'),
                      backgroundSize = '100% 90%', backgroundRepeat = 'no-repeat', backgroundPosition = 'center') %>%
          formatStyle('clicks', background = styleColorBar(DeviceGetData[, 4], 'lightgreen'),
                      backgroundSize = '100% 90%', backgroundRepeat = 'no-repeat', backgroundPosition = 'center')
```

出力例

インタラクティブに操作が可能です。データは仮想です。

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

このコードの続きをAIに聞く

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.