diff --git a/R/speed-insights-package.R b/R/speed-insights-package.R index 69c5ead..b3ee7bc 100644 --- a/R/speed-insights-package.R +++ b/R/speed-insights-package.R @@ -6,14 +6,15 @@ #' @param strategy Whether the URL should be evaluated in a mobile or desktop context. Accordingly, acceptable values #' are either the string "mobile" or the string "desktop". #' @param key A unique key obtainable from Google by registering for free as a Google developer. +#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE) #' @examples #' \dontrun{ #' speedfinder("https://www.cars.com","mobile",key) #' } #' @export -speedfinder <- function(url,strategy,key) { - pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key)) +speedfinder <- function(url,strategy,key,filter_third_party_resources=FALSE) { + pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key,"&filter_third_party_resources=",filter_third_party_resources)) frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6])) rbind.data.frame(data.frame(), frame1,make.row.names=FALSE) } @@ -26,10 +27,11 @@ speedfinder <- function(url,strategy,key) { #' @param strategy Whether the list of URLs should be evaluated in a mobile or desktop context. Accordingly, acceptable values #' are either the string "mobile" or the string "desktop". #' @param key A unique key obtainable from Google by registering for free as a Google developer. +#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE) #' @export -speedfinder2 <- function(url,strategy,key) { - pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key)) +speedfinder2 <- function(url,strategy,key,filter_third_party_resources=FALSE) { + pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key,"&filter_third_party_resources=",filter_third_party_resources)) frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6])) } @@ -42,14 +44,15 @@ speedfinder2 <- function(url,strategy,key) { #' @param strategy Whether the list of URLs should be evaluated in a mobile or desktop context. Accordingly, acceptable values #' are either the string "mobile" or the string "desktop". #' @param key A unique key obtainable from Google by registering for free as a Google developer. +#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE) #' @examples #' \dontrun{ #' speedlist(listofURLs,"mobile",key) #' } #' @export -speedlist <- function(pagelist,strategy,key) { - list1 <- lapply(pagelist,speedfinder2,strategy,key) +speedlist <- function(pagelist,strategy,key,filter_third_party_resources=FALSE) { + list1 <- lapply(pagelist,speedfinder2,strategy,key,filter_third_party_resources) suppressWarnings(do.call(gtools::smartbind,list1)) } diff --git a/man/speedfinder.Rd b/man/speedfinder.Rd index 079eaec..d5057ef 100644 --- a/man/speedfinder.Rd +++ b/man/speedfinder.Rd @@ -4,7 +4,7 @@ \alias{speedfinder} \title{Speed results for 1 URL} \usage{ -speedfinder(url, strategy, key) +speedfinder(url, strategy, key, filter_third_party_resources = FALSE) } \arguments{ \item{url}{A URL on which to run the Google Page Speed Insights test.} @@ -13,6 +13,8 @@ speedfinder(url, strategy, key) are either the string "mobile" or the string "desktop".} \item{key}{A unique key obtainable from Google by registering for free as a Google developer.} + +\item{filter_third_party_resources}{A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)} } \description{ The speedfinder function returns the Google Page Speed Insights test results for a single URL as a dataframe. diff --git a/man/speedfinder2.Rd b/man/speedfinder2.Rd index f3ad7e7..5898e93 100644 --- a/man/speedfinder2.Rd +++ b/man/speedfinder2.Rd @@ -4,7 +4,7 @@ \alias{speedfinder2} \title{Function within speedlist} \usage{ -speedfinder2(url, strategy, key) +speedfinder2(url, strategy, key, filter_third_party_resources = FALSE) } \arguments{ \item{url}{A list of URLs on which to run the Google Page Speed Insights test.} @@ -13,6 +13,8 @@ speedfinder2(url, strategy, key) are either the string "mobile" or the string "desktop".} \item{key}{A unique key obtainable from Google by registering for free as a Google developer.} + +\item{filter_third_party_resources}{A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)} } \description{ Speedfinder2 is a function utilized by speedlist to allow a list of URLs to be submitted to the Google Page Speed diff --git a/man/speedlist.Rd b/man/speedlist.Rd index 5dee73e..7f88923 100644 --- a/man/speedlist.Rd +++ b/man/speedlist.Rd @@ -4,7 +4,7 @@ \alias{speedlist} \title{Speed results for a list of URLs} \usage{ -speedlist(pagelist, strategy, key) +speedlist(pagelist, strategy, key, filter_third_party_resources = FALSE) } \arguments{ \item{pagelist}{A list of URLs on which to run the Google Page Speed Insights test.} @@ -13,6 +13,8 @@ speedlist(pagelist, strategy, key) are either the string "mobile" or the string "desktop".} \item{key}{A unique key obtainable from Google by registering for free as a Google developer.} + +\item{filter_third_party_resources}{A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)} } \description{ The speedlist function submits a list of URLs to the Google Page Speed Insights API, and returns a dataframe in which diff --git a/tests/testthat/testthat.R b/tests/testthat/testthat.R index 2c1a450..b14e3d0 100644 --- a/tests/testthat/testthat.R +++ b/tests/testthat/testthat.R @@ -5,16 +5,18 @@ context("gpagespeed functions") test_check("gpagespeed") +gspeed_key = 'YOUR_API_KEY' + test_that("speedfinder function", { - expect_equal(class(speedfinder("https://www.cars.com","mobile",key)), + expect_equal(class(speedfinder("https://www.cars.com","mobile",key=gspeed_key,filter_third_party_resources = TRUE)), class(data.frame())) - expect_equal(nrow(speedfinder("https://www.cars.com","mobile",key)), 1) + expect_equal(nrow(speedfinder("https://www.cars.com","mobile",key=gspeed_key)), 1) }) test_that("speedlist function", { expect_equal(class(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"), - "mobile",key)), + "mobile",key=gspeed_key)), class(data.frame())) expect_equal(nrow(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"), - "mobile",key)), 3) + "mobile",key=gspeed_key)), 3) })