-
Notifications
You must be signed in to change notification settings - Fork 1
/
RAdwords.r
83 lines (77 loc) · 4.35 KB
/
RAdwords.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
# Install and load required packages
if (!require("RAdwords")) install.packages("RAdwords")
library(RAdwords)
if (!require("dplyr")) install.packages("dplyr")
library(dplyr)
# Define Client ID (available in Google Console)
clientId <- c("")
# Define Client Secret (available in Google Console)
clientSecret <- c("")
# Define Account ID (available in Google Adwords)
accountId <- c("")
# Define time period of report, i.e. last 3 complete days
min_date <- as.Date(Sys.Date() - 3)
max_date <- as.Date(Sys.Date() - 1)
# Authentication
setwd("O:/path/to/your/access/tokens")
adwordsAuth <- doAuth(T)
# Loop over account IDs
report <- NULL
for (a in 1:length(accountId)) {
print(paste0("Calling AW API for account ", accountId[a], "..."))
# Download FINAL_URL_REPORT
report <- rbind.fill(report,
getData(clientCustomerId = accountId[a],
google_auth = adwordsAuth,
statement = statement(select = c('AccountDescriptiveName',
'AdGroupId',
'AdGroupName',
'AdGroupStatus',
'CampaignId',
'CampaignName',
'CampaignStatus',
'EffectiveFinalUrl',
'EffectiveTrackingUrlTemplate',
'Date',
'Impressions',
'Clicks',
'Cost',
'Conversions',
'AveragePosition'),
report = "FINAL_URL_REPORT",
start = min_date,
end = max_date),
transformation = TRUE,
changeNames = TRUE,
includeZeroImpressions = FALSE,
verbose = FALSE))
# Download DESTINATION_URL_REPORT
report <- rbind.fill(report,
getData(clientCustomerId = accountId[a],
google_auth = adwordsAuth,
statement = statement(select=c('AccountDescriptiveName',
'AdGroupId',
'AdGroupName',
'AdGroupStatus',
'CampaignId',
'CampaignName',
'CampaignStatus',
'EffectiveDestinationUrl',
'Date',
'Impressions',
'Clicks',
'Cost',
'Conversions',
'AveragePosition'),
report = "DESTINATION_URL_REPORT",
start = min_date,
end = max_date),
transformation = TRUE,
changeNames = TRUE,
includeZeroImpressions = FALSE,
verbose = FALSE))
}
# Store reports in CSV file
write.csv(x = report,
file = "O:/path/to/your/csv/file/adwords.csv",
row.names = FALSE)