Skip to content

Instantly share code, notes, and snippets.

@remibacha
Forked from voltek62/api-similarweb.R
Created July 19, 2018 09:07
Show Gist options
  • Save remibacha/28fe10e2ea7a4f15b390ca5d305cace3 to your computer and use it in GitHub Desktop.
Save remibacha/28fe10e2ea7a4f15b390ca5d305cace3 to your computer and use it in GitHub Desktop.
get Web Traffic Data from SimilarWeb API with R
library(httr)
library(jsonlite)
# https://dataseolabs.com
# Doc : https://www.similarweb.com/corp/developer/
# Create your key here : https://pro.similarweb.com/#/account/api-management
# You can have freely 3 Months of Web Traffic Data
# conf
myList <- c("cuisineaz.com","marmiton.org","odelices.com","allrecipes.fr")
myKey <- "YOURKEY"
dateStart <- "2018-03"
dateEnd <- "2018-05"
# create empty dataframe
results <- data.frame(site=character(), date=character(), visits=integer())
for (site in myList) {
# query similarweb
url <- paste0("https://api.similarweb.com/v1/website/",site,"/total-traffic-and-engagement/visits?api_key=",myKey,"&start_date=",dateStart,"&end_date=",dateEnd,"&main_domain_only=false&granularity=monthly")
result <- GET(url)
text <- content(result,as = "text", encoding = "UTF-8")
json <- fromJSON(text)
# add lines if no error
if (grepl("Success", json$meta$status)) {
tmp <- cbind(site, json$visits)
results <- rbind(results, tmp)
}
}
# delete tmp objects
rm(json)
rm(result)
rm(tmp)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment