I am having some difficulties unpacking what looks like a list within a list in Google's Page Speed API response.
Ideally, I want only audit results exported as a CSV file. So I can compare the website load times and performance of my client's website.
'''
library(httr)
library(tidyverse)
library(tidyr)
#URL to submit GET request to
url <- "https://www.googleapis.com/pagespeedonline/v5/runPagespeedurl=https://www.google.com/"
# GET request returned as list
raw_list <- url %>%
httr::GET() %>%
httr::content()
#turning the list into a dataframe
df_pagespeed <- as.data.frame(do.call(rbind, raw_list))
#attempted unpack list in audit results with no luck
df_pagespeed <- tidyr::unnest(df_pagespeed, cols = audits)
# select only the audit results.
df_pagespeed_final <- df_pagespeed[c(audits)]
#export to csv file
write.csv(df_pagespeed_final,"test-pagespeed.csv", row.names = FALSE)
'''
Ideally I want the second dataframe (df_pagespeed_final) to contain information related to pagespeed audit results. Meaningful insights like my first-contentful-paint
Hopefully that is clear enough for someone to understand. If not, please let me know and I will revise the question.
Thanks for your help.
I managed to figure it out. Probably not the best solution or cleanest, but it works.
Hopefully this helps someone else out, who is working with the Google Pagespeed API and R.