-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_api_calls.R
115 lines (84 loc) · 4.07 KB
/
01_api_calls.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Packages installieren; nur einmal nötig
# install.packages(c("googleAuthR",
# "googleAnalyticsR",
# "searchConsoleR",
# "lubridate",
# "jsonlite"))
library(googleAuthR)
library(googleAnalyticsR)
library(searchConsoleR)
library(lubridate)
library(jsonlite)
options(googleAuthR.client_id = "XXX.apps.googleusercontent.com",
googleAuthR.client_secret = "XXX",
googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/analytics.readonly",
"https://www.googleapis.com/auth/webmasters.readonly"))
gar_auth()
# Konfiguration -------------------------------------------------------
# Google Analytics View-ID ermitteln und eintragen
# ga_account_list() %>% View()
GA_VIEW_ID <- "XXX"
# GSC Property ermitteln und eintragen
# list_websites() %>% View()
GSC_PROP <- "XXX"
# Sisitrix
SISTRIX_DOMAIN <- "XXX"
SISTRIX_API_KEY <- "XXX"
# Start- und Endzeitpunkt der Datenabfragen definieren
# Hier: Sechs Monate rückwirkend vom Beginn des aktuellen Monats bis zum
# aktuellen Datum
START_DATE <- floor_date(today(), "month") %m-% months(6)
END_DATE <- today()
# Google Analytics ----------------------------------------------------
# Organic Search / Direct Sessions abfragen
channel_filter_organic <- dim_filter(dimension = "channelGrouping",
operator = "EXACT",
expressions = "Organic Search")
channel_filter_direct <- dim_filter(dimension = "channelGrouping",
operator = "EXACT",
expressions = "Direct")
filter_clause <- filter_clause_ga4(list(channel_filter_organic,
channel_filter_direct),
operator = "OR")
ga_sessions <- google_analytics(viewId = GA_VIEW_ID,
date_range = c(START_DATE, END_DATE),
metrics = c("sessions"),
dimensions = c("date", "channelGrouping"),
dim_filters = filter_clause,
anti_sample = TRUE)
# GSC -----------------------------------------------------------------
# GSA: Date
gsa_dim_date <- search_analytics(siteURL = GSC_PROP,
startDate = START_DATE,
endDate = END_DATE,
searchType = "web",
dimensions = "date")
# GSA: Date ~ Query
gsa_dim_date_query <- search_analytics(siteURL = GSC_PROP,
startDate = START_DATE,
endDate = END_DATE,
searchType = "web",
dimensions = c("date", "query"))
# GSA: Date ~ Page
gsa_dim_date_page <- search_analytics(siteURL = GSC_PROP,
startDate = START_DATE,
endDate = END_DATE,
searchType = "web",
dimensions = c("date", "page"))
# Sistrix -------------------------------------------------------------
# API-URLs zusammenbauen
si_url_desktop <- paste0("https://api.sistrix.com/domain.sichtbarkeitsindex?history=true&format=json&api_key=",
SISTRIX_API_KEY,
"&domain=",
SISTRIX_DOMAIN)
si_url_mobile <- paste0("https://api.sistrix.com/domain.sichtbarkeitsindex?history=true&format=json&api_key=",
SISTRIX_API_KEY,
"&domain=",
SISTRIX_DOMAIN,
"&mobile=true")
# API abfragen
api_response_desktop <- fromJSON(si_url_desktop)
api_response_mobile <- fromJSON(si_url_mobile)
# JSON-Antwort in DataFrame überführen
si_desktop <- api_response_desktop$answer$sichtbarkeitsindex[[1]]
si_mobile <- api_response_mobile$answer$sichtbarkeitsindex[[1]]