Bitte zuerst lesen!

Dies ist eine etwas andere Version als im Buch, da ich noch einige Sachen hinzugefügt habe. Selbst wenn Sie keinen Zugriff auf eine eigene Webanalyse-Plattform haben, können Sie dieses Beispiel ausprobieren, da ich Ihnen einen Datensatz von meiner eigenen Website zur Verfügung stelle.

Libraries & Authentifizierung

Die folgenden Befehle auskommentieren (das “#” entfernen), um die Libraries zu installieren. Das muss nur einmal passieren.

#install.packages("tidyverse")
#install.packages("googleAuthR")
#install.packages("googleAnalyticsR")
#install.packages("digest")

Am bequemsten werden Daten aus Google-APIs mit einem Service User abgefragt. Andere Möglichkeiten sind hier nachzulesen: http://code.markedmondson.me/googleAuthR/articles/google-authentication-types.html

Ich habe die Autorisierungsdaten in meinem R-Stammverzeichnis in der Datei .Renviron gespeichert; in der Datei steht nur

GA_AUTH_FILE = “/Users/tom/Shiny-d1739b8e7809.json”

Das muss natürlich an die eigenen Gegebenheiten angepasst werden (und natürlich benötigt man auch eine eigene JSON-Datei).

library(googleAuthR)
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/analytics")
library(googleAnalyticsR)
## 2019-07-04 18:47:27> Default Google Project for googleAnalyticsR is now set.  
##  This is shared with all googleAnalyticsR users. 
##  If making a lot of API calls, please: 
##  visit: https://bit.ly/2Evk6hn 
##  for instructions on setting your own Google Project
## Setting scopes to https://www.googleapis.com/auth/analytics.readonly
## Registered S3 method overwritten by 'openssl':
##   method      from
##   print.bytes Rcpp
## Successfully auto-authenticated via /Users/tom/Shiny-d1739b8e7809.json
library(tidyverse)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## ── Attaching packages ────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.1     ✔ dplyr   0.8.1
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(digest)

Die eigentliche Abfrage

ga_id= 1952687
start_date <- "2019-06-01"
end_date <- "2019-06-30"
ga_data <- google_analytics(ga_id,
      date_range = c(start_date,end_date),
      metrics = c("ga:users"),
      dimensions = c("ga:dimension1","ga:dimension2","ga:dimension3",
"ga:dimension4","ga:sourceMedium","ga:pagePath","ga:sessionCount"),
max = -1
)
## 2019-07-04 18:47:32> Downloaded [10000] rows from a total of [34938].
## 2019-07-04 18:47:39> Downloaded [24938] rows from a total of [34938].
## 2019-07-04 18:47:39> All data downloaded, total of [34938] - download time: 11.22536 secs
ga_data2 <- google_analytics(ga_id, date_range = c(start_date,end_date),
metrics = c("ga:eventValue"),
dimensions = c("ga:dimension1","ga:dimension3","ga:deviceCategory","ga:eventCategory","ga:eventAction","ga:eventLabel","ga:sessionCount"),
max = -1
)
## 2019-07-04 18:47:42> Downloaded [10000] rows from a total of [26961].
## 2019-07-04 18:47:48> Downloaded [16961] rows from a total of [26961].
## 2019-07-04 18:47:48> All data downloaded, total of [26961] - download time: 8.762336 secs

Transformation der Daten

ga_data_id <- ga_data %>%
  mutate(id = paste(dimension1,dimension3,sep="_")) %>%
  select(id,dimension1,dimension2,dimension3,dimension4,sourceMedium,pagePath,sessionCount)

ga_data_id2 <- ga_data2 %>%
  mutate(id = paste(dimension1,dimension3,sep="_")) %>%
  select(id,deviceCategory,eventCategory,eventAction,eventLabel,eventValue, sessionCount)
      
ga_data_final <- merge(ga_data_id,ga_data_id2,by="id",all=T)

Wir säubern und anonymisieren die Daten erst einmal, bevor wir sie zeigen:

ga_data_final <- ga_data_final %>%
  select(dimension1, dimension2, dimension3, sourceMedium, pagePath, sessionCount.x, deviceCategory, eventCategory,eventAction,eventLabel,eventValue)
i = 1
for (i in i:nrow(ga_data_final)) {
  ga_data_final$dimension1[i] <- digest(paste(ga_data_final$dimension1[i]))
  i <- i+1
}

Analyse und Ergebnis

top_pages <- ga_data_final %>%
  filter(dimension2 == "pageview") %>%
  group_by(pagePath) %>%
  summarize(n=n()) %>%
  arrange(desc(n))

cv <- ga_data_final %>%
  group_by(pagePath) %>%
  filter(eventLabel == "YARPP Visibility") %>%
  summarize(cv = n()) %>%
  arrange(desc(cv))

new_ga_data <- merge(top_pages,cv)
new_ga_data %>%
  mutate(cvr = cv/n) %>%
  filter(n>49) %>%
  arrange(desc(cvr))