ericbrownaustin
ericbrownaustin

Reputation: 1300

Using rga to run multiple Google Analytics API Calls Using a Data Table to Assign Filters and Metrics

I have multiple GA API calls that I would like to run in a single script. The calls will differ based on the "source" (technically just a place holder name) and specific filters and metrics for each source.

I was using the RGA package with a script that allowed me to specify all of the profile ids I wanted to use. The lapply function loops through the profile ids, but I would like to also loop through the metric and filter values for each "source" listed in a data table. This would be much more efficient than listing out 40 different calls in the script with 40 different filters.

Here is the script I was using:

ids <- c(123456,12345679)

start <- "2015-12-01"
end <- "2015-12-31"

res <- lapply(ids, function(id) {
  ans <- get_ga(id, start.date = start, end.date = end,
        dimensions = "ga:yearMonth",
        metrics = "ga:sessions",
        filters = "ga:medium==organic;ga:landingPagePath!~gppc|sm003|refer=")
        ans$id <- id
        return(ans)
  })

res <- do.call(rbind, res)  
organic <- cbind("organic", res)

The string "organic" would be place holder name for the row that would contain the metrics returned.

What I would rather do is use this data frame to assign values:

a <- c(123456,12345679,123456,12345679,123456,12345679)
b <- c("organic","organic","cpc","cpc","banner","banner")
c <- c("ga:medium==organic;ga:landingPagePath!~gppc|sm003|refer=","ga:medium==organic;ga:landingPagePath!~gppc|sm003|refer=","ga:landingPagePath!~gppc|sm003|refer=","ga:landingPagePath!~gppc|sm003|refer=","ga:medium==banner","ga:medium==banner")
check <- cbind(a,b,c)
colnames(check)[1:3] <- c("profile","source","filters")

Then use some sort of apply function or loop to return each row based on the profile, filter combination.

Upvotes: 1

Views: 517

Answers (2)

ericbrownaustin
ericbrownaustin

Reputation: 1300

I'm not sure I correctly explained what I'm trying to do. Your reply included an expand.grid which, I believe, creates a row for every combination of the included variables. This isn't exactly what I am looking for. I am looking to run the script for only the data frame I listed in my original question. I made some changes to the script and got it working by converting the check object to a data.frame and renaming some of the other object calls.

id <- c(123456, 12345678)
source <- c("organic", "cpc", "banner")
filter <- c("ga:medium==organic;ga:landingPagePath!~gppc|sm003|refer=","ga:medium==organic;ga:landingPagePath!~gppc|sm003|refer=","ga:landingPagePath!~gppc|sm003|refer=","ga:landingPagePath!~gppc|sm003|refer=","ga:medium==banner","ga:medium==banner")

check <- cbind(id,source,filter)
colnames(check)[1:3] <- c("profile","source","filters")
check <- data.frame(check, stringsAsFactors = FALSE)

res <- lapply(1:nrow(check), function(i) {
  filter <- sprintf("ga:medium==%s;%s", check$source[i], check$filters[i])
  ga_data <- get_ga(check$profile[i], start.date = start, end.date = end,
                    dimensions = "ga:yearMonth",
                    metrics = "ga:sessions",
                    filters = filter)
  cbind(id = check$profile[i], source = check$source[i], ga_data)
})
res <- data.table::rbindlist(res)

Upvotes: 0

Artem Klevtsov
Artem Klevtsov

Reputation: 9423

You should form the input variables data.frame. Then pass it to the get_ga() row by row. Something like this:

library(RGA)
authorize()

id <- c(123456, 12345679)
source <- c("organic", "cpc", "banner")
filter <- c("ga:landingPagePath!~gppc|sm003|refer=")

check <- expand.grid(id, source, filter)

res <- lapply(1:nrow(check), function(i) {
    filter <- sprintf("ga:medium==%s;%s", check$Var2[i], check$Var3[i])
    ga_data <- get_ga(check$Var1[i], start.date = start, end.date = end,
                      dimensions = "ga:yearMonth",
                      metrics = "ga:sessions",
                      filters = filter)
    cbind(id = check$Var1[i], source = check$Var2[i], ga_data)
})
res <- data.table::rbindlist(res)

Note: you can replace lapply with mclapply from the parallel package.

Upvotes: 1

Related Questions